How to implement intervals in protractor/selenium

2019-08-01 15:20发布

问题:

I'm writing a series of automated tests and get intermittent timeout/synchronization errors. I looked online and was told that using intervals would solve this problem

https://github.com/angular/angular.js/blob/master/src/ng/interval.js

I was wondering how do I incorporate the interval.js file with the rest of the files? Do I add a line to protractor-config.js, or do I link it elsewhere. If so, how do I link it?

回答1:

The $interval service comes bundled with Angular; you don't need to download any new files or integrate anything with Protractor. Your Protractor tests are most likely just fine the way they are.

What everyone means when they tell you to use $interval is that you (or your company's developers) need to look through the source code of your app for $timeout or $http requests that are happening repeatedly (in a recursive function, a loop, or two functions that mistakenly call each other), or possibly an $http request that is failing (this would show up in bright red in Chrome's developer console, under the Network tab).

The reason why looping $timeouts (and failing $http requests) are bad is that Protractor, by design, will wait for every $timeout callback and $http request to finish completely before it will do anything. However, it will not wait for $interval callbacks to complete. $interval has almost exactly the same syntax as $timeout, so it's not difficult to change from one to the other--the only difference is that $timeout executes once, and $interval executes on a continuous loop. Official docs are below:

$timeout

$interval

A few more possibilities are listed at another answer I posted, along with a link to Protractor's official list of reasons why this might happen: Timed out waiting for Protractor to synchronize with the page after 50001ms

I give you my 90% guarantee that it is one of these issues, but if you provide more information about your situation, I can give some more specific tips.