如何在angular2中实现间隔/轮询以使用量角器? [英] How to implement intervals/polling in angular2 to work with protractor?

查看:75
本文介绍了如何在angular2中实现间隔/轮询以使用量角器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个angular2应用程序,我想用量角器进行测试。

I have an angular2 app I want to test with protractor.

在这个应用程序中,我有一个页面,其图表定期更新,并带有自动生成的数据。

In this app I have a page with a graph that is being updated in regular intervals with autogenerated data.

显然,量角器的一个特性是在执行测试代码之前等待脚本和http调用完成。但是,如果有一个永远不会完成的轮询脚本,量角器会在一段时间后永远等待并超时。

Apparently one feature of protractor is waiting for scripts and http calls to finish before executing test code. However, if there is a constantly polling script that never finishes, protractor will wait forever and time out after a certain time.

在angular1中,这可以通过实施轮询来解决使用 $ interval ,量角器不会等待。不幸的是,在angular2中没有 $ interval ,实现轮询的正确方法似乎是 Observable.interval ,所以这个是我的代码:

In angular1 this could be solved by implementing the polling with $interval, which protractor does not wait for. Unfortunately in angular2 there is no $interval and the correct way to implement polling seems to be Observable.interval, so this is what my code looks like:

Observable.interval(500)
          .map(x => this.getRandomData())
          .subscribe(data => this.updateGraph(data));

在测试运行此代码的页面时,量角器将超时。它等待页面完成加载并认为此脚本将在某个时间退出(实际上它会永远运行)。

When testing a page where this code is running, protractor will time out. It waits for the page to finish loading and thinks this script will exit sometime (when in fact it runs forever).


  • 是在角度2中有一个间隔机制,量角器会识别,因此在运行UI测试之前它不会等待轮询完成吗?

  • Is there an interval mechanism in angular2 that protractor recognizes, so that it doesn't wait for the polling to finish before running the UI tests?

如果没有,如何在执行更多测试代码之前,我可以告诉量角器不要等待这个间隔完成吗?

If not, how can I tell protractor not to wait for this interval to finish before executing more test code?

编辑:澄清一下,使用angular1在量角器中已存在超时问题,但可以使用 $ interval 修复,请参阅:

To clarify, the timeout problem already existed in protractor with angular1, but could be fixed by using $interval, see:

  • Timed out waiting for Protractor to synchronize with the page after 50001ms
  • How to implement intervals in protractor/selenium

这在angular2中不起作用,因为没有 $ interval

This doesn't work in angular2 because there is no $interval.

推荐答案

经过一番调查后,我发现了两种可能的解决方案:

After some investigation, I found two possible solutions:


  1. browser.ignoreSynchronization = true 指示量角器停止等待http调用和间隔脚本。但是,这可能会使编写e2e测试变得更加困难,因为现在您必须在测试之前手动等待元素和页面加载。

  2. protractor-xhr-only插件基本上与 ignoreSynchronization ,但仅适用于区间脚本。量角器仍会等待 $ http 来电完成。

  1. browser.ignoreSynchronization = true instructs protractor to stop waiting for http calls and interval scripts. However, this will likely make writing e2e tests much harder, because now you have to manually wait for elements and pages to load before testing them.
  2. The protractor-xhr-only plugin basically does the same thing as ignoreSynchronization, but only for interval scripts. Protractor will still wait for $http calls to finish.

两者都没有是一个完美的解决方案,但总比没有好。

Neither of both are a perfect solution, but better than nothing.

这篇关于如何在angular2中实现间隔/轮询以使用量角器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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