gpt4 book ai didi

javascript - Protractor ,我什么时候应该在点击后使用 then()

转载 作者:数据小太阳 更新时间:2023-10-29 03:49:11 26 4
gpt4 key购买 nike

我正在运行一个 Angular 应用程序,当在 Protractor 上测试 click() 时,我不知道什么时候应该用 then() 解决 promise .

我在 Protractor API 上找到了这个:

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

那么,我是否应该在每次点击时都使用click().then()

最佳答案

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

绝对不是。

不需要,因为 Protractor/WebDriverJS 有这个机制叫做 "Control Flow"这基本上是一个需要解决的 promise 队列:

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);

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

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

还有Explicit Waits在这些情况下进行救援,但这与此处无关。

关于javascript - Protractor ,我什么时候应该在点击后使用 then(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33441331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com