2

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. This is what I got TypeError: Cannot read property 'map' of undefined. Any ideas?
@TanSu do you have instructors variable defined as an array? Thanks.
Thanks. I can't really define an array of instructors. This test is supposed to run after beforeAll(function(){instructors = saltare.getUsers('instructor');}); which getUsers will return an array of instructors. GetUsers depends on the previous test cases. I suspect since instructors.map is not within an it() block, it won't run after beforeAll, that's why the data is not prepared. Any suggestions?
I tried to move instructors = saltare.getUsers('instructor'); from beforeAll function to right before instructors.map but get the same TypeError: Cannot read property 'map' of undefined.
@alecxe .map() was in mind but, this post really solved my problem. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.