JWP
JWP

Reputation: 6963

Protractor in Node: How do I order my tests to run synchronously?

I'm wondering how to execute multiple "describe" blocks in synchronous order?

describe('Go To Home Page', function () {  
    browser.ignoreSynchronization = true;
    it("Url should be on Login Page", function ()  {
        browser.get(HomePageUrl).then(function () {
            browser.wait(urlChanged(loginPageUrl), 2000).then(
                function (newurl){
                    var url = browser.getCurrentUrl().then(function (url) {
                        expect(url).tobe(loginPageUrl);
                        //I know I'm at Login page url... how do I kick off next test?                 
                    });                     
                }
            )          
        });
    });
}); 

This test goes to Home page, then if it is redirected to Login page I want to execute all my Login Tests using new Describe blocks. Problem is that if I put the next Describe block at same level as the first, node executes all of them in parallel.

I'd prefer not to get into callback-hell... code above is already getting too deep in my opinion.

Upvotes: 2

Views: 2080

Answers (1)

Dziamid
Dziamid

Reputation: 11581

Jasmine has Asyncronous support exacly for that.

describe('Go To Home Page', function () {  
    browser.ignoreSynchronization = true;
    it("Url should be on Login Page", function (done)  {
        browser.get(HomePageUrl).then(function () {
            browser.wait(urlChanged(loginPageUrl), 2000).then(
                function (newurl){
                    var url = browser.getCurrentUrl().then(function (url) {
                        expect(url).tobe(loginPageUrl);
                        done();      
                    });                     
                }
            )          
        });
    });

   it("next", function ()  {
      //will start this block after previous done callback has been called
   });
}); 

PS Promises can and should be chained in a single line to avoid nesting:

it("Url should be on Login Page", function (done)  {
    browser
        .get(HomePageUrl)
        .then(function () {
            return browser.wait(urlChanged(loginPageUrl), 2000);
        })
        .then(function () {
            return browser.getCurrentUrl();
        })
        .then(function (currentUrl){
            expect(currentUrl).tobe(loginPageUrl);
            done();         
        });
});

Upvotes: 5

Related Questions