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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33441331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:28:46  来源:igfitidea点击:

Protractor, when should I use then() after a click()

javascriptangularjsautomated-testsprotractor

提问by Joao Falcao

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().

我正在运行一个 Angular 应用程序,当在量角器 a 上进行测试时click(),我不知道什么时候应该用then().

I found this on Protractor API:

我在 Protractor API 上找到了这个:

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

单击命令完成后将解决的承诺。

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

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

采纳答案by alecxe

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

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

Definitely not.

当然不。

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:

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

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

WebDriverJS 维护一个挂起的承诺队列,称为控制流,以保持执行有组织。

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

和 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.

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

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


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:

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

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

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

在这些情况下也有显式等待来救援,但它在这里不相关。

回答by Broda Noel

Yes, you should. Maybe right now it's not necessary, but maybe in next versions it is. So, if clickreturn a promise, you should use it.

是的你应该。也许现在没有必要,但也许在下一个版本中是必要的。所以,如果click返回一个承诺,你应该使用它。

http://www.protractortest.org/#/api?view=webdriver.WebElement.prototype.click

http://www.protractortest.org/#/api?view=webdriver.WebElement.prototype.click