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

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Tan Su
  • 71
  • 7

1 Answers1

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.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thank you. This is what I got `TypeError: Cannot read property 'map' of undefined.` Any ideas? – Tan Su Sep 02 '15 at 19:51
  • @TanSu do you have `instructors` variable defined as an array? Thanks. – alecxe Sep 02 '15 at 19:54
  • 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? – Tan Su Sep 02 '15 at 20:00
  • 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.` – Tan Su Sep 02 '15 at 20:02
  • @alecxe .map() was in mind but, this post really solved my problem. Thanks! – DDave Aug 30 '16 at 10:22