When using protractor, the following operations are possible:
beforeEach(function() {
browser.get(myLoginUrl);
this.username = element(by.model('username'));
this.password = element(by.model('password'));
this.loginButton = element(by.css('[ng-click="login()"]'));
this.username.sendKeys(...);
this.password.sendKeys(...);
this.loginButton.click();
// i'm logged in
});
All of the above methods will execute sequentially without any issues.
I have created a PageObject model to represent my login page for testing other pages. I am currently working on making my PageObject model methods perform similar to protractor methods, running in a 'series'.
During the login process, a session id is generated which is required for subsequent HTTP requests. After logging in, I wish to test the home page by utilizing a Login PageObject:
beforeEach(function() {
var sessionId = loginPage.loginAs('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="35415046414046504775504d54584559501b565a58">[email protected]</a>', 'mypassword');
homePage = new HomePage(sessionId);
});
it('is accessible', function() {
homePage.get();
expect(browser.getCurrentUrl()).toMatch(homePage.homePageRegex);
});
I want the line
homePage = new HomePage(sessionId)
to wait until
var sessionId = loginPage.loginAs(...)
has completely executed and returned a valid sessionId, similar to how protractor elements work.
One attempt utilizing promises:
loginPage.loginAs('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f68293858283859384b6938e979b869a93a69693969782bd828e8c">[email protected]</a>', 'mypassword').then(function(sessionId) {
homePage = new HomePage(sessionId);
});
This approach fails as the beforeEach function completes before setting the sessionId, causing errors during execution.
Experimenting with:
browser.driver.controlFlow().execute(function() {...});
in an attempt to synchronize code execution has not been successful yet.
If anyone knows how to achieve what I'm attempting, please share your insights!
I am aiming to avoid excessive chaining of then() functions as mentioned in the Control Flow section:
https://code.google.com/p/selenium/wiki/WebDriverJs#Understanding_the_API