With protractor we can do this:
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
});
And this will all just work, because each of the above methods, will for all practical purposes run in serial waiting each in turn to run.
Now I've built a PageObject model object to model my login page which needs to be used to test other pages. I'm trying to figure out how to get my PageObject model methods to work like the protractor methods, running in 'series'.
As part my login process, a session id is returned that is needed for subsequent HTTP gets/posts. After login, I want to test the home page, so I have test code like this using a Login PageObject:
beforeEach(function() {
// code from above has been refactored into a page object. here i can just
// call loginPage.loginAs(...) and it will log the user in and return the
// session id
var sessionId = loginPage.loginAs('testuser@example.com', 'mypassword');
// construct the home PageObject with a sessionId because homePage.get() will
// need to put the sessionId in the url
homePage = new HomePage(sessionId);
});
// test we can access the page
it('is accessible', function() {
homePage.get();
expect(browser.getCurrentUrl()).toMatch(homePage.homePageRegex);
});
I want
homePage = new HomePage(sessionId)
to wait and not run until
var sessionId = loginPage.loginAs(...)
has successfully completely and returned a valid sessionId. Similar to how the protractor elements themselves work.
Using something like:
loginPage.loginAs('testuser@example.com', 'mypassword').then(function(sessionId) {
homePage = new HomePage(sessionId);
});
fails because this will the beforeEach function will exit and the first it() will run and try to perform homePage.get() before the sessionId has been set.
I've been toying around with:
browser.driver.controlFlow().execute(function() {...});
to somehow get the code syncing up and executing in parallel but I have quite got the recipe down.
Does anyone know to accomplish what I'm trying to do?
I'm trying to avoid a lot of then() chaining per the Control Flow section of the doc:
https://code.google.com/p/selenium/wiki/WebDriverJs#Understanding_the_API