Khaja Mohammed
Khaja Mohammed

Reputation: 779

Protractor Javascript not executing in sequence

I have the below function for login, which would console "successful" on success and "Failed" for files ones.

function login() {

return new Promise(function (resolve, reject) {
    var username = element(by.name('txtLogin'));
    var password = element(by.id('txtPassword'));
    var signInButton = element(by.id('btnSignIn'));

    for (var i = 0; i < testData.length; i++) {
        if (testData[i].env === appConfig) {
            username.sendKeys(testData[i].user);
            password.sendKeys(testData[i].pass);
            signInButton.click();
            console.log("Successfully Clicked on the Sign In Button!");
            break;
        }
    }
    browser.getTitle().then(function (title) {
        if (title == "Page Title") {
            resolve("Successfull");
        } else {
            reject("Failed");
        }
    });
});
};

And the following test

describe('Login Scenarios', function () {

beforeEach(function () {
    login();
});

it('Valid Credentials, Should login successfully from util class', function () {
    console.log('Successfully logged in!');
});
});

I am seeing very strange behavior here. This line executes and consoles output even before the page is fully loaded.

console.log("Successfully Clicked on the Sign In Button!");

and the below code never gets executed.

browser.getTitle().then(function (title) {
        if (title == "Page Title") {
            resolve("Successfull");
        } else {
            reject("Failed");
        }
    });

And in the end i see the following error.

failed: error while waiting for protractor to sync with the page: "cannot re ad property '$$testability' of undefined"

I am pretty sure, i have messed up something here. But unable to figure out what's wrong that am doing here.

Upvotes: 1

Views: 173

Answers (1)

Joe Clay
Joe Clay

Reputation: 35837

login returns a promise, but you're not signalling to Jasmine/Protractor that it needs to wait for it to finish. You can do this using the done callback:

beforeEach(function (done) {
    login().then(function () {
       done();
    });
});

See the Jasmine documentation (which seems to have been inexplicibly hidden away in their new website layout...) for more info.

Upvotes: 2

Related Questions