Context: End-to-end testing in Angular JS using Protractor
Goal: I want to run the same test against many different values. Ideally looping through an array of values and running the same it
block for each.
Problem: Since Protractor will run the tests asynchronously any test inside a for
loop will be executed after the loop finishes.
For example,
var values = ['...','...'];
for (var i = 0; i < values.length; i++) {
it('should do something good', function() {
// some test using this particular value in the values array
});
}
However (say there are 10 items in the array), the result is the the test runs 10 times where the value of i
is 9
each time, instead of 0,1,2,3...
as expected. This has to do with the asynchronous nature of the tests.
What I've tried: Using a done()
callback according to the answer of this stack overflow question.
for (var i = 0; i < values.length; i++) {
it('should do something good', function (done) {
// some test using this particular value in the values array
setTimeout(done, 1000);
});
}
Why that didn't work: I get the following error:
/node_modules/protractor/jasminewd/index.js:44
throw new Error('Do not use a done callback with WebDriverJS tests
^
Error: Do not use a done callback with WebDriverJS tests. The tests are patched to be asynchronous and will terminate when the webdriver control flow is empty.
at null._onTimeout (/Users/drew/apps/hotrod/node_modules/protractor/jasminewd/index.js:44:19)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
Any ideas on how to loop through an array of values and run the same tests against each? Thanks