Tan Su
Tan Su

Reputation: 71

How to loop consecutive actions sequentially in protractor test?

I wish to login -> verify -> logout with multiple users, so I put them in a loop.

I followed the following post to do this to force synchronous execution. Looping on a protractor test with parameters

Here are the scripts:

 it('instructor setup personal profile', function(){
                var data = [];
                for (var i = 0; i < instructors.length; i++) {
                    (function(instructor) {
                        console.log('instructor in test '+instructor);
                        common.login(url, instructor, password);
                        password = saltare.resetPassword();
                        data.push({'Username':instructor});
                        saltare.setProfile();
                        browser.waitForAngular();
                        expect(element(home_objects.SIGNOUT).isDisplayed).toBeTruthy();
                        home.logout();
                    })(instructors[i]);
                }

                data.push({'Password':password});
                saltare.saveToFile('instructors',data);
            });

Here are the printouts:

instructor in test instructor14412186328231
instructor in test instructor14412186328232

Instead of printing out the first instructor, completing the sequential actions and then proceeding to the second, it prints out both instructors at once and then completes the actions for first and throws exceptions.

Expect:

  1. print 1 user -> login -> verify -> logout
  2. print 2 user -> login -> verify -> logout

Actual:

  1. print 1, 2 user
  2. 1 user -> login -> verify -> logout
  3. 2 user -> exception

I also tried asynchronized support in jasmine, but it did not workout either

    it("takes a long time", function(done) {
          setTimeout(function() {
          //test case
            done();
          }, 9000);
        });

Is there something can guarantee a sequential execution ? Something simpler like https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

Upvotes: 2

Views: 674

Answers (1)

alecxe
alecxe

Reputation: 474131

A cleaner approach would be to dynamically create a separate spec for every user:

describe("Testing instructors", function () {
    var instructors = [
        "user1",
        "user2"
    ];

    instructors.map(function(instructor) {
        it("setup personal profile for instructor '" + instructor + "'", function() {
            // test logic here
        });
    });
});

One of the benefits of this approach is error-handling: if a test fails for a particular instructor, you would immediately see it from the spec description.

Plus, it() blocks are guaranteed to be executed sequentially.

Upvotes: 1

Related Questions