量角器,当我应该使用则()点击后() [英] Protractor, when should I use then() after a click()

查看:132
本文介绍了量角器,当我应该使用则()点击后()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行的应用程序角度和量角器测试点击()的时候,我不知道什么时候我应该可以解决了<$ C $的承诺C>则()。

我发现这对量角器API:


  

当点击命令完成,将解决的希望。


所以,我应该使用点击()。那么()在每个点击


解决方案

  

所以,我应该在每次点击使用点击()。那么()?


非也。

这是没有必要的,因为量角器/ WebDriverJS有这种机制称为控制流这基本上是一个需要解决的承诺队列:


  

WebDriverJS保持未决承诺队列,称为控制
  流动,以保持组织执行


和量角器等待角自然和外的开箱:


  

您不再需要添加等待,可供您的测试。量角器
  可以在测试自动执行下一步骤的时刻
  网页完成尚未完成的任务,所以您不必担心
  等待您的测试和网页同步。


这导致了相当直接的测试code:

  VAR elementToBe present =元素(by.css(anotherelementclass。))是present()。期待(elementToBe present.is present())TOBE(假)。
元件(by.css(#myButton的))点击()。
期待(elementToBe present.is present())TOBE(真)。


虽然有时,如果你遇到同步/时序问题,或在测试您的应用程序是无棱角,你可以通过用<解析点击()明确解决code>则(),继续点击回调里面:

 预期(elementToBe present.is present())TOBE(假)。
元素(by.css(#则myButton))。点击()。然后(函数(){
    期待(elementToBe present.is present())TOBE(真)。
});

有也明确等待来在这些情况下的救援,但它不是与此有关。

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

I found this on Protractor API:

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

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

解决方案

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

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:

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

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


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.

这篇关于量角器,当我应该使用则()点击后()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆