protractor icon indicating copy to clipboard operation
protractor copied to clipboard

Calling element.sendKeys() seems to not send all the keys

Open neosavvy opened this issue 12 years ago • 54 comments

I am doing the following two tests:

   it("Should be able to populate the firstname field", function () {

        expect(capturePageModel.findFirstNameFieldValue()).toEqual("");
        capturePageModel.setFirstNameFieldValue("Adam");
        expect(capturePageModel.findFirstNameFieldValue()).toEqual("Adam");

    });

    it("Should be able to populate the lastname field", function () {
        expect(capturePageModel.findLastNameField()).toEqual("");
        capturePageModel.setLastNameFieldValue("Parrish");
        expect(capturePageModel.findLastNameField()).toEqual("Parrish")
    });

When I execute both tests in the same describe block I definitely am seeing the second test only send the "sh" value of the "Parrish" value I am passing in. Thus the test fails.

However each test runs fine by itself. I am attempting to use PageObjects to write a few documents on how this could work but curious if you have seen anything like this happen.

Simple example of my setter and getter functions in my PageObjects

this.setLastNameFieldValue = function( value ) {

    var lastNameElement = element(by.model('presubmitUser.lastName'))
    lastNameElement.sendKeys( value );

}

this.findLastNameField = function() {
    var firstNameElement = element(by.id('lastName'))
    return firstNameElement.getAttribute('value');
}

The first name field is identical to the last name field.

neosavvy avatar Apr 11 '14 01:04 neosavvy

Not sure what's going on here - could you share the test output?

juliemr avatar Apr 11 '14 18:04 juliemr

Starting selenium standalone server...
Selenium standalone server started at http://192.168.1.16:59995/wd/hub
....F

Failures:

  1) As a user I want to be able to register as a new candidate Should be able to populate the lastname field
   Message:
     Expected 'sh' to equal 'Parrish'.
   Stacktrace:
     Error: Failed expectation
    at null.<anonymous> (/NEOSAVVY/src/<snip>/frontend/src/test/resources/integration/specs/register-new-candidate-spec.js:37:54)

Finished in 2.69 seconds
5 tests, 7 assertions, 1 failure

Notice that in the sendKeys test I am sending the text "Parrish" but the only value that makes it into the input is "sh" I was seeing that sometimes it would be "rrish" or "rish" which implies a race condition of sorts. I tried a few sleep() / wait() type approaches but it just didn't work.

neosavvy avatar Apr 11 '14 18:04 neosavvy

Huh, that's very strange - I've never seen something like this. I would guess that something funny is going on in the app - when they're in the same describe block, there's no page reload in between the tests?

The closest thing I can remember to seeing is we had an issue testing docs.angularjs.org where sendKeys wouldn't send anything after a '/'. It turned out that the search bar was stealing focus on '/', so this was actually a real bug with the page. Is it possible that something like that is going on?

juliemr avatar Apr 11 '14 19:04 juliemr

I can definitely simplify my page there is quite a lot, we are attempting to save the data as a user types it. We have an ng-blur style directive that sends data on keypress. I would still expect to see the first keys get sent vs the last keys. You are probably right I'll dig more into what we are doing an eliminate variability on my page to be sure about this.

neosavvy avatar Apr 11 '14 19:04 neosavvy

I think I may be having the same issue: sendKeys() seems to be interpreting the letters e and r as control characters rather than regular characters:

var email = $('input#email');

// Sending 'e' or 'E' deletes the previous character
email.sendKeys('abcde'); // => Types 'abcd' then deletes the 'd'
email.sendKeys('ABCDE'); // => Types 'ABCD' then deletes the 'D'

// Sending 'r' moves the focus to the next input element
email.sendKeys('abcrabc'); // => Types 'abc' in the email input, switches focus to the password input, and types 'abc'

// Sending 'R' does nothing
email.sendKeys('ABCR'); // => Types 'ABC'

Here's a simple page I used to isolate the problem:

<!DOCTYPE html>
<html ng-app>
  <head>
    <title>Sign In</title>
    <meta http-equiv="content-type" content="charset=UTF8"></meta>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.6/angular.min.js"></script>
  </head>
  <body>
    <div id="main">
      <form>
        <div>
          <input id="email" type="email" title="Email" placeholder="Email" required="" name="email"></input>
        </div>
        <div>
          <input id="password" type="password" title="Password" placeholder="Password" name="password"></input>
        </div>
        <div>
          <button type="submit">Sign In</button>
        </div>
      </form>
    </div>
  </body>
</html>

My system:

ubuntu 13.10 protractor 0.22.0 selenium-webdriver 2.41.0 chromedriver 2.9 chromium-browser 33.0.1750.152

ihough avatar Apr 23 '14 11:04 ihough

Interesting, I have the same setup but Mac and chrome Version 34.0.1847.116, and I don't see this crazy behavior. Let me try on a linux box.

juliemr avatar Apr 23 '14 17:04 juliemr

Tried it on a Mac with chrome Version 34.0.1847.116 and it worked fine. I'm upgrading to Ubuntu 14.04 and will see if the issue persists.

ihough avatar Apr 24 '14 16:04 ihough

@ihough and @neosavvy I tried this a couple months ago and couldn't reproduce. Now that there have been a few updates to webdriver and protractor, can you try this again and let us know if this behavior is still happening?

hankduan avatar Jun 23 '14 22:06 hankduan

@hankduan I think in my case the issue is actually a problem with X11 forwarding over ssh. My Ubuntu box is a headless vagrant machine based on chef/ubuntu-13.10. Here's my setup and test:

Ubuntu 13.10 (headless vagrant machine based on chef/ubuntu-13.10) protractor 1.0.0-rc1 selenium-webdriver 2.42.1 chromedriver 2.10 chromium-browser 34.0.1847.116

describe('sendKeys()', function() {
  browser.get('http://localhost:80');

  var email = element(by.id('email'));
  var password = element(by.id('password'));

  beforeEach(function() {
    email.clear();
    password.clear();
  });

  it('should be able to type "abcde"', function() {
    email.sendKeys('abcde');
    expect(email.getAttribute('value')).toEqual('abcde');
    expect(password.getAttribute('value')).toEqual('');
  });

  it('should be able to type "ABCDE"', function() {
    email.sendKeys('ABCDE');
    expect(email.getAttribute('value')).toEqual('ABCDE');
    expect(password.getAttribute('value')).toEqual('');
  });

  it('should be able to type "abcrabc"', function() {
    email.sendKeys('abcrabc');
    expect(email.getAttribute('value')).toEqual('abcrabc');
    expect(password.getAttribute('value')).toEqual('');
  });

  it('should be able to type "ABCRABC"', function() {
    password.sendKeys('ABCRABC');
    expect(password.getAttribute('value')).toEqual('ABCRABC');
    expect(email.getAttribute('value')).toEqual('');
  });
});

I can reproduce the issue if forward X11 over ssh to my local machine:

vagrant@vagrant:/vagrant$ node_modules/protractor/bin/protractor protractor-conf.js 
Using ChromeDriver directly...
FFFF

Failures:

  1) sendKeys() should be able to type "abcde"
   Message:
     Expected 'abc' to equal 'abcde'.
   Stacktrace:
     Error: Failed expectation
    at null.<anonymous> (/vagrant/spec.js:14:41)

  2) sendKeys() should be able to type "ABCDE"
   Message:
     Expected 'ABC' to equal 'ABCDE'.
   Stacktrace:
     Error: Failed expectation
    at null.<anonymous> (/vagrant/spec.js:20:41)

  3) sendKeys() should be able to type "abcrabc"
   Message:
     Expected 'abc' to equal 'abcrabc'.
   Stacktrace:
     Error: Failed expectation
    at null.<anonymous> (/vagrant/spec.js:26:41)

  4) sendKeys() should be able to type "abcrabc"
   Message:
     Expected 'abc' to equal ''.
   Stacktrace:
     Error: Failed expectation
    at null.<anonymous> (/vagrant/spec.js:27:44)

  5) sendKeys() should be able to type "ABCRABC"
   Message:
     Expected 'ABC' to equal 'ABCRABC'.
   Stacktrace:
     Error: Failed expectation
    at null.<anonymous> (/vagrant/spec.js:32:44)

  6) sendKeys() should be able to type "ABCRABC"
   Message:
     Expected 'ABC' to equal ''.
   Stacktrace:
     Error: Failed expectation
    at null.<anonymous> (/vagrant/spec.js:33:41)

But the issue does not occur if I use xvfb to run chromium-browser headlessly:

vagrant@vagrant:/vagrant$ export DISPLAY=:10 && Xvfb :10 -screen 0 1366x768x24 -ac &
[1] 2044
vagrant@vagrant:/vagrant$ node_modules/protractor/bin/protractor protractor-conf.js 
Using ChromeDriver directly...
....

Finished in 8.108 seconds
4 tests, 8 assertions, 0 failures

ihough avatar Jun 27 '14 19:06 ihough

This might be a problem between X11/webdriver (or some other aspect of X11) instead of protractor.

To test this (and since I dont' have this environment set up), can you try sending commands directly through webdriver instead of through the protractor logic.

i.e.

  it('should be able to type "abcde"', function() {
    var driver = protractor.getInstance();
    var email = driver.findElement(by.id('email')); // This returns a raw WebElement from Webdriver
    email.sendKeys('abcde');
    email.getAttribute('value').then(function(value) { // This does not use the protractor "expect" wrapper
      assert.equal(value, 'abcde');
    });

  });

hankduan avatar Jun 27 '14 20:06 hankduan

@hankduan tried using unwrapped WebDriver and assertions -- all tests fail in the same way. Looks to me like this isn't an issue with Protractor.

ihough avatar Jun 30 '14 10:06 ihough

You might need to chase this up with X11 or webdriver.

Try showing them some code that is purely webdriver. See https://code.google.com/p/selenium/wiki/WebDriverJs.

hankduan avatar Jun 30 '14 17:06 hankduan

Thanks

ihough avatar Jun 30 '14 17:06 ihough

Closing this issue as it's not a protractor issue.

hankduan avatar Aug 08 '14 18:08 hankduan

Just stumbled upon this issue with the latest Protractor and SauceLabs. SauceLabs was receiving the correct instructions, i.e. set value to "The Dark Knight", but only "TheDark" (and sometimes even different character sequences) ended up in the text field, followed by a return instruction which resulted in a page navigation. You can even see this behavior in SauceLabs' recordings (see the last three screenshots) [1].

After fiddling around for quite some time I simply switched over to Firefox from Chrome and wasn't seeing any more issues. While this is not a general solution, it was sufficient for my workshop use case.

[1] https://saucelabs.com/tests/d86fcf7d99924643b20845a153c7d903

bripkens avatar Oct 04 '14 14:10 bripkens

@bripkens Are you sure that's not your app doing it? It looks like "The Dark Knight" is sent (you can see it being typed in), but after you hit post, "TheDark" shows up.

I'm guessing your app is trimming the output to a specific length or something.

hankduan avatar Oct 06 '14 17:10 hankduan

@hankduan It is actually not doing anything interesting with the input [1, 2]. Also, tests are working with Firefox on SauceLabs and locally too with the latest Chrome.

[1] https://github.com/codecentric/movie-database-node/blob/master/client/js/controller.js#L26-L32 [2] https://github.com/codecentric/movie-database-node/blob/master/client/partial/movies/_form.html#L10

bripkens avatar Oct 08 '14 15:10 bripkens

Same problem here, my setup:

OS:        MAC OS X 'Mavericks'
node -v:   v0.10.29
npm -v:    1.4.16

At times, the input gets fully entered, at other times only one or two characters make it.

wayneseymour avatar Oct 09 '14 21:10 wayneseymour

@bripkens Can you try to replace element(...).sendKeys("The Dark Knight"); to something purely webdriver to see if same behavior holds? Also, can you put a trimmed down version of your app/test/setup into its own repo for me to reproduce?

@wayneseymour Can you provide more information? i.e. browser (i.e. is it phantom), specs-code, app-code.

hankduan avatar Oct 09 '14 21:10 hankduan

Sure.

Latest chrome. protractor-1.2.0

Also in the page object pattern, but here is the gist of it:

return $('some css classes').sendKeys('some-text-with-hyphens-and-[brackets,and-a-comma]'); // promise chain

wayneseymour avatar Oct 09 '14 22:10 wayneseymour