简体   繁体   English

如何在angular2中实现间隔/轮询以使用量角器?

[英]How to implement intervals/polling in angular2 to work with protractor?

I have an angular2 app I want to test with protractor. 我有一个angular2应用程序,我想用量角器测试。

In this app I have a page with a graph that is being updated in regular intervals with autogenerated data. 在这个应用程序中,我有一个页面,其中包含一个图表,该图表定期使用自动生成的数据进行更新。

Apparently one feature of protractor is waiting for scripts and http calls to finish before executing test code. 显然,量角器的一个特性是在执行测试代码之前等待脚本和http调用完成。 However, if there is a constantly polling script that never finishes, protractor will wait forever and time out after a certain time. 但是,如果有一个永远不会完成的轮询脚本,量角器会在一段时间后永远等待并超时。

In angular1 this could be solved by implementing the polling with $interval , which protractor does not wait for. 在angular1中,这可以通过使用$interval实现轮询来解决,量角器不会等待。 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: 不幸的是,在angular2中没有$interval ,实现轮询的正确方法似乎是Observable.interval ,所以这就是我的代码:

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). 它等待页面完成加载并认为此脚本将在某个时间退出(实际上它会永远运行)。

  • 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? 在角度2中是否存在量角器识别的间隔机制,以便它在运行UI测试之前不等待轮询完成?

  • If not, how can I tell protractor not to wait for this interval to finish before executing more test code? 如果没有,我怎么能告诉量角器在执行更多测试代码之前不要等待这个间隔完成?

EDIT: To clarify, the timeout problem already existed in protractor with angular1, but could be fixed by using $interval , see: 编辑:澄清一下,有角度1的量角器中已存在超时问题,但可以使用$interval修复,请参阅:

This doesn't work in angular2 because there is no $interval . 这在angular2中不起作用,因为没有$interval

After some investigation, I found two possible solutions: 经过一番调查,我发现了两种可能的解决方案

  1. browser.ignoreSynchronization = true instructs protractor to stop waiting for http calls and interval scripts. browser.ignoreSynchronization = true指示量角器停止等待http调用和间隔脚本。 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. 但是,这可能会使编写e2e测试变得更加困难,因为现在您必须在测试之前手动等待元素和页面加载。
  2. The protractor-xhr-only plugin basically does the same thing as ignoreSynchronization , but only for interval scripts. protractor-xhr-only插件基本上与ignoreSynchronization ,但仅适用于区间脚本。 Protractor will still wait for $http calls to finish. 量角器仍将等待$http调用完成。

Neither of both are a perfect solution, but better than nothing. 两者都不是一个完美的解决方案,但总比没有好。

https://github.com/angular/protractor/issues/3349#issuecomment-232253059 https://github.com/angular/protractor/issues/3349#issuecomment-232253059

resolved this problem with help from juliemr: 在juliemr的帮助下解决了这个问题:

this.ngZone.runOutsideAngular(() => {
  this.timer = Observable.interval(1000)
}

run timeout out of zone, then protractor will not wait for it. 运行超出区域,然后量角器将不会等待它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM