量角器,我什么时候应该在 click() 之后使用 then()

Protractor, when should I use then() after a click()(量角器,我什么时候应该在 click() 之后使用 then())
本文介绍了量角器,我什么时候应该在 click() 之后使用 then()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在运行一个 Angular 应用程序,当在量角器上测试 click() 时,我不知道何时应该使用 then() 解决承诺.

I'm running an Angular app and when testing on protractor a click(), I don't know when should I resolve the promise with a then().

我在 Protractor API 上找到了这个:

I found this on Protractor API:

当点击命令完成时将被解决的承诺.

A promise that will be resolved when the click command has completed.

那么,我应该在每次 click 中使用 click().then() 吗?

So, should I use click().then() in every click?

推荐答案

那么,我应该在每次点击中使用 click().then() 吗?

So, should I use click().then() in every click?

绝对不是.

不需要,因为 Protractor/WebDriverJS 有这种称为 " 的机制控制流",它基本上是一个需要解决的承诺队列:

It's not needed because Protractor/WebDriverJS has this mechanism called "Control Flow" which is basically a queue of promises that need to be resolved:

WebDriverJS 维护一个待处理的 Promise 队列,称为控件流,以保持执行有条理.

WebDriverJS maintains a queue of pending promises, called the control flow, to keep execution organized.

并且 Protractor 会自然地、开箱即用地等待 Angular:

and Protractor waits for Angular naturally and out-of-the-box:

您不再需要在测试中添加等待和睡眠.量角器可以在测试的那一刻自动执行下一步网页完成待处理任务,您无需担心等待您的测试和网页同步.

You no longer need to add waits and sleeps to your test. Protractor can automatically execute the next step in your test the moment the webpage finishes pending tasks, so you don’t have to worry about waiting for your test and webpage to sync.

这导致了一个非常直接的测试代码:

Which leads to a quite straight-forward testing code:

var elementToBePresent = element(by.css(".anotherelementclass")).isPresent();

expect(elementToBePresent.isPresent()).toBe(false);
element(by.css("#mybutton")).click();
expect(elementToBePresent.isPresent()).toBe(true);

<小时>

但有时,如果您遇到同步/计时问题,或者您的被测应用不是 Angular,您可以通过使用 then()<显式解析 click() 来解决它/code> 并在点击回调中继续:


Sometimes though, if you experience synchronization/timing issues, or your app under test is non-Angular, you may solve it by resolving the click() explicitly with then() and continue inside the click callback:

expect(elementToBePresent.isPresent()).toBe(false);
element(by.css("#mybutton")).click().then(function () {
    expect(elementToBePresent.isPresent()).toBe(true);
});

还有显式等待在这些情况下的救援,但在这里不相关.

There are also Explicit Waits to the rescue in these cases, but it's not relevant here.

这篇关于量角器,我什么时候应该在 click() 之后使用 then()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

SCRIPT5: Access is denied in IE9 on xmlhttprequest(SCRIPT5:在 IE9 中对 xmlhttprequest 的访问被拒绝)
XMLHttpRequest module not defined/found(XMLHttpRequest 模块未定义/未找到)
Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
quot;Origin null is not allowed by Access-Control-Allow-Originquot; in Chrome. Why?(“Access-Control-Allow-Origin 不允许 Origin null在铬.为什么?)
How to get response url in XMLHttpRequest?(如何在 XMLHttpRequest 中获取响应 url?)