Question

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

Was it helpful?

Solution

Solution found! The trick is to wrap the contents of the loop in an immediately invoked function and pass in an explicit param with each iteration of the loop.

Check it out:

https://stackoverflow.com/a/23635357/1526027

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top