calling clear() in beforeAll misbehaves with protractor 2.0.0
I've recently upgraded from protractor 1.8 to protractor 2.0.0. I'm seeing strange behavior when using beforeAll to log into my non-anglur app. It seems that the calls to element.clear() are not happening in the right order. I am seeing this happen:
- usernameFld is left blank
- "school01" is typed into institutionFld
- institutionFld is cleared
- "teacherpassword" is typed into passwordFld
- loginBtn is clicked
- Test fails because the username input and institution input are blank
I have found three workarounds when using PageObjects:
- Use this.usernameFld.click().clear().sendKeys(name) instead of this.usernameFld.clear().sendKeys(name).
- Use this.usernameFld.sendKeys(name) instead of this.usernameFld.clear().sendKeys(name).
- Move loginPage.login() from the "beforeAll" block to the "it" block.
I don't understand why these three workarounds work. Is this expected behavior from protractor 2.0.0/selenium-webdriver 2.45.1 or is this a bug?
Note: I posted a simplified example in stackoverflow and @alecxe provided a workaround. Now with PageObjects and onPrepares in the mix, I see the error "Failed: Cannot call method 'sendKeys' of undefined". So I've gone back to my original code in LoginHomePage.login().
HTML:
<input type="text" name="teacherUsername" size="20" value="" tabindex="1" id="username" title="Enter your Username">
<input type="text" name="teacherInstitution" size="20" value="" tabindex="2" id="institution" title="Enter your Institution">
<input type="password" name="teacherPassword" size="20" tabindex="3" id="password" title="Enter your Password">
<button class="submit" tabindex="4" id="loginbtn">Log In</button>
conf.js:
exports.config = {
framework: 'jasmine2',
specs: ['tests/Course\ Management/Smoke_Copy_Course_spec.js'],
onPrepare: function() {
require('require-all')(__dirname + '/helpers');
}
};
helpers/helper.js:
global.pages = require('require-all')(__dirname + '/../pages/');
global.loginPage = new pages.Navigation.LoginHomePage();
pages/Navigation/LoginHomePage.js:
var LoginHomepage = function() {
this.loginBtn = element(by.id('loginbtn'));
this.usernameFld = element(by.id('username'));
this.institutionFld = element(by.id('institution'));
this.passwordFld = element(by.id('password'));
this.get = function() {
browser.get('http://mypage.net/login.html');
};
this.login = function(name, institution, password) {
this.get();
this.usernameFld.clear().sendKeys(name);
this.institutionFld.clear().sendKeys(institution);
this.passwordFld.clear().sendKeys(password);
this.loginBtn.click();
}
}
module.exports = LoginHomepage;
tests/Course Management/Smoke_Copy_Course_spec.js:
describe('Protractor 2.0.0', function(){
beforeAll(function(){
browser.ignoreSynchronization = true;
loginPage.login('teacher01', 'school01', 'teacher01password');
});
it('should work with beforeAll', function() {
expect(browser.getTitle()).toEqual('My Classes');
});
});
I tried reproducing this and I see everything being typed as expected. The one thing I changed was I just copied your helper directly into onPrepare instead of using require-all. I wonder if this is some sort of weird bug with require-all.
It's possible that something odd is going on with your site. Can you try this against a simple page that just has the HTML you've included above?
Since I don't think this is a generic issue, let's close the discussion here and continue on SO.
I'm also seeing some problems with actions not happening in the correct order on my non-angular app. This is after upgrading to 2.0.
I have a test for simply testing our navigation menu. It clicks the links in order from top to bottom. When I had 1-3 clicks it worked as expected. After adding the 4th click action they are now clicking out of order, 2 1 3 4.
I suspect they are not being added to the control flow correctly. I "fixed" it by putting the actions inside of a browser.controlFlow().execute(function(){ // clicky }); Now they execute in order, 1 2 3 4.