Search code examples
angularjsnode.jsseleniumprotractorjasmine2.0

calling clear() in beforeAll misbehaves with protractor 2.0


I've upgraded from protractor 1.8 to protractor 2.0. I'm seeing strange behavior when using beforeAll (jasmine 2.0) to log into my app before executing test scenarios in the it blocks. It seems that the first call to element.clear() is happening last. I am seeing this happen:

  1. "teacher01" is typed into usernameFld
  2. institutionFld is cleared
  3. "school01" is typed into institutionFld
  4. passwordFld is cleared
  5. "teacherpassword" is typed into passwordFld
  6. usernameFld is cleared
  7. loginBtn is clicked
  8. Test fails because usernameFld is blank

Could someone tell me what I'm doing wrong (See code below)?

Notes:

I've tried switching to directConnect. I've tried breaking out the clear() and sendKeys() onto separate lines. Neither seemed to help.

Using the same code and rolling back to protractor 1.8.0, I don't have this problem. usernameFld is cleared first, then the username is typed into usernameFld and the test is able to log in fine. Moving the login code from beforeAll into the "it" block makes the test behave as expected, too.

Note that passwordFld is an input of type password and loginBtn is just a button. This is a test for a non-angular webpage. I'm using jasmine 2.0, chrome browser 41.0.2272.101, firefox 36.0.4 and starting the selenium server from the test script.

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: ['school_spec.js']
};

TEST:

describe('Protractor 2.0', function() {
  browser.ignoreSynchronization = true;
  var loginBtn = element(by.id('loginbtn'));
  var usernameFld = element(by.id('username'));
  var institutionFld = element(by.id('institution'));
  var passwordFld = element(by.id('password'));

  beforeAll(function() {
    browser.get('http://mypage.net/login.html');
    usernameFld.clear().sendKeys('teacher01');
    institutionFld.clear().sendKeys('school01');
    passwordFld.clear().sendKeys('teacherpassword');
    loginBtn.click();
  });

  it('should work with beforeAll', function() {
    expect(browser.getTitle()).toEqual('My Classes');
  });
});

Solution

  • I won't provide you an explanation, but, hopefully, a solution.

    Move the browser.ignoreSynchronization = true; to beforeAll() and call sendKeys() only then clear() is completed:

    beforeAll(function() {
        browser.ignoreSynchronization = true;
        browser.get('http://mypage.net/login.html');
    
        usernameFld.clear().then(function () {
            usernameFld.sendKeys('teacher01');
        });
        institutionFld.clear().then(function () {
            institutionFld.sendKeys('school01');
        });
        passwordFld.clear().then(function () {
            passwordFld.sendKeys('teacherpassword');
        });
    
        loginBtn.click();
    });