量角器,我什么时候应该在点击后使用 then()
Protractor, when should I use then() after a click()
我是 运行 一个 Angular 应用程序,在量角器 click()
上进行测试时,我不知道什么时候应该用 then()
解决承诺.
我在 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?
绝对不是。
不需要它,因为 Protractor/WebDriverJS 有一个叫做 "Control Flow" 的机制,它基本上是一个需要解决的承诺队列:
WebDriverJS maintains a queue of pending promises, called the control
flow, to keep execution organized.
并且 Protractor 等待 Angular 自然且开箱即用:
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.
这导致了一个非常直接的测试代码:
var elementToBePresent = element(by.css(".anotherelementclass")).isPresent();
expect(elementToBePresent.isPresent()).toBe(false);
element(by.css("#mybutton")).click();
expect(elementToBePresent.isPresent()).toBe(true);
但有时,如果您遇到 synchronization/timing 问题,或者您的测试应用不是 Angular,您可以通过使用 [=13= 显式解析 click()
来解决问题] 并在点击回调中继续:
expect(elementToBePresent.isPresent()).toBe(false);
element(by.css("#mybutton")).click().then(function () {
expect(elementToBePresent.isPresent()).toBe(true);
});
在这些情况下也有 Explicit Waits 可以解决,但这里不相关。
是的,你应该。
也许现在没有必要,但也许在下一个版本中是必要的。
所以,如果 click
return 一个承诺,你应该使用它。
http://www.protractortest.org/#/api?view=webdriver.WebElement.prototype.click
我是 运行 一个 Angular 应用程序,在量角器 click()
上进行测试时,我不知道什么时候应该用 then()
解决承诺.
我在 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?
绝对不是。
不需要它,因为 Protractor/WebDriverJS 有一个叫做 "Control Flow" 的机制,它基本上是一个需要解决的承诺队列:
WebDriverJS maintains a queue of pending promises, called the control flow, to keep execution organized.
并且 Protractor 等待 Angular 自然且开箱即用:
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.
这导致了一个非常直接的测试代码:
var elementToBePresent = element(by.css(".anotherelementclass")).isPresent();
expect(elementToBePresent.isPresent()).toBe(false);
element(by.css("#mybutton")).click();
expect(elementToBePresent.isPresent()).toBe(true);
但有时,如果您遇到 synchronization/timing 问题,或者您的测试应用不是 Angular,您可以通过使用 [=13= 显式解析 click()
来解决问题] 并在点击回调中继续:
expect(elementToBePresent.isPresent()).toBe(false);
element(by.css("#mybutton")).click().then(function () {
expect(elementToBePresent.isPresent()).toBe(true);
});
在这些情况下也有 Explicit Waits 可以解决,但这里不相关。
是的,你应该。
也许现在没有必要,但也许在下一个版本中是必要的。
所以,如果 click
return 一个承诺,你应该使用它。
http://www.protractortest.org/#/api?view=webdriver.WebElement.prototype.click