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

I don't think sending a string with hyphens, brackets, and commas will reproduce this on a general basis. Can you provide the app you're running against as well? (Unless you get this behavior sending that string to any angular app --i.e www.angularjs.org, by which case, definitely let me know as well)

hankduan avatar Oct 09 '14 22:10 hankduan

haha, I wish I could send you a link. But, its my company's proprietary app. You know how it goes. I think there may just be a race condition I can pin down. I put in a sleep and that works...but that wont make it past code review! :)

wayneseymour avatar Oct 09 '14 22:10 wayneseymour

@wayneseymour Let me know what you find. There has been a few reports of this issue, but no one thus far has been able to provide an example that allows me to reproduce yet

hankduan avatar Oct 09 '14 23:10 hankduan

will do, thanks man!

wayneseymour avatar Oct 09 '14 23:10 wayneseymour

+1 get this on a proprietary app via chromedriver/SauceLabs when entering '01234569789', and with subdivided portions of that string. Result is generally '0123' or similar, with the latter part of the string missing.

Local chromedriver works without fault. Will attempt to duplicate/trim code and get a failing test over.

sublimino avatar Oct 24 '14 16:10 sublimino

Had the same issue. Somehow by calling element.clear() before element.sendKeys(), even if the input was initially empty, solved it for me.

kuiro5 avatar Oct 24 '14 19:10 kuiro5

Innumerable permutations of sendKeys() failed, so I worked around the failing-on-Saucelabs issue like so:

var inputSelector = '.create-search-input';
browser.executeScript('window.jQuery("' + inputSelector + '").val("12345678A123456")');
browser.executeScript('angular.element(window.jQuery("' + inputSelector + '")).triggerHandler("input")');

sublimino avatar Oct 29 '14 16:10 sublimino

Having similar issue with protractor v1.3.1 with Chrome (38.0.2) or Safari (7.0.5), resulting in timeouts, most likely its their bug, because Firefox and IE both work fine.

The test is quite simple:

browser.get('https://localhost/');
element(by.id('email')).sendKeys('a');

verbose mode shows log for Safari case:

16:01:56.385 INFO - Executing: [find elements: By.id: email])
16:01:56.579 INFO - Done: [find elements: By.id: email]
16:01:56.620 INFO - Executing: [send keys: 0 [[SafariDriver: safari on MAC (null)] -> id: email], [a]])
16:02:24.344 INFO - Executing: [delete session: 280366b7-a77a-4a98-8903-1482987925ff])
16:02:28.302 WARN - Exception thrown
org.openqa.selenium.TimeoutException: Timed out awaiting response to command "sendKeysToElement" after 30001 ms (WARNING: The server did not provide any stacktrace information)

Possible related issue: https://code.google.com/p/selenium/issues/detail?id=6537&q=safaridriver&sort=-summary&colspec=ID%20Stars%20Type%20Status%20Priority%20Milestone%20Owner%20Summary

With Chrome however, its much worse, because it not only doesn't affect the input, it doesn't close the browser (and even regular closing doesn't help, I have to use killall)

16:14:02.971 INFO - Executing: [find elements: By.id: email])
16:14:02.993 INFO - Done: [find elements: By.id: email]
16:14:03.031 INFO - Executing: [send keys: 0 [[ChromeDriver: chrome on MAC (b41eeee0117fb5c3cefa2505115eec09)] -> id: email], [a]])
16:14:25.765 INFO - Executing: [delete session: 8ce3e2be-efc2-4bb8-9970-b55754ded82b])

But executeScript() with jquery suggested by @sublimino works nicely, though that means I can't use native by.model selectors, which is a pity. But, i can emulate that with.. browser.executeScript('return $("input[ng-model=\'credentials.email\']").val("sdfwef");');

tot-ra avatar Nov 05 '14 14:11 tot-ra

Having issues with sendKeys() where 5 is being treated as a backspace and 6 just terminates the process of sending keys to the input all together.

Ex) elem.sendKeys(58801) would yield 8801 in the input elem.sendKeys(85801) would yield 801 in the input elem.sendKeys(88601) would yield 88 in the input

However if I run it on Windows rather than Linux (via Sauce Labs) it works just fine. I think they keys are mapped differently for whatever version of Linux Sauce is using and that was my issue.

Aaron-Hartwig avatar Dec 30 '14 20:12 Aaron-Hartwig

Yes can you check the mapping. This does not seem like a general issue.

hankduan avatar Dec 30 '14 21:12 hankduan

I have this issue using chromedriver regardless of angular in use. phantomjs doesn't cause the same issues.

jdgiotta avatar Apr 10 '15 18:04 jdgiotta

Having trouble with .sendKeys()

element.all(by.tagName('my-text')).get(0) .element( by.tagName('div') ) .element( by.tagName('input') ).sendKeys("Hello");

It gives me Message: ElementNotVisibleError: element not visible

But if I do expect( element.all(by.tagName('my-text')).get(0) .element( by.tagName('div') ) .element( by.tagName('input') ).isPresent() ).toBeTruthy(); it passes the test

Any idea on why I can't sendKeys to the element?? Any help would be greatly appreciated, Thanks!

Haseeb90 avatar Jun 15 '15 22:06 Haseeb90

It might be present but not visible. Regardless, please ask that kind of question on Stack Overflow.

sjelin avatar Jun 15 '15 22:06 sjelin

Workaround: set value using JS (simply execute $("#your-id").val("your-value"))

DenisGorbachev avatar Sep 14 '15 15:09 DenisGorbachev

@DenisGorbachev So I am using the JavaScript above to set the input, but it appears that the (ng-model) binding is not picking up the change. Any idea how to force the change detection in Angular2?

mikeloll avatar Nov 17 '15 16:11 mikeloll

Might be related to https://bugs.chromium.org/p/chromedriver/issues/detail?id=1037 it happens on remote connections (e.g. using vnc)

holdfenytolvaj avatar Jan 22 '16 05:01 holdfenytolvaj

@DenisGorbachev your workaround with $("#your-id").val("your-value") doesn't seem to work anymore. It says .val is not a function. Is that correct? (using the latest protractor version)

pgrm avatar Feb 16 '16 12:02 pgrm

another workaround: pass individual chars to sendKeys: 'blah'.split('').forEach((c) => element.sendKeys(c))

dodoshanti avatar Feb 23 '16 16:02 dodoshanti

@dodoshanti thanks for the workaround! It slows a bit down the process but at least it works :)

foch avatar Feb 25 '16 13:02 foch

@dodoshanti , @foch The problem is happening because a previous test is using the modifier keys and the modifier keys are not released.

By Design the modifier (SHIFT, CONTROL, ALT, META) keys are stateful, and once they are pressed, they remain in pressed state unless

  1. One of the modifier key is pressed again, or
  2. webDriver.Key.NULL is passed

So the easiest solution is to use these keys like this

browser.actions()
      .sendKeys(protractor.Key.ALT)
      .sendKeys('s')
      .sendKeys(protractor.Key.NULL) // Passing Null is extremely important
      .perform();

Very well explained here: webdriver.WebElement.sendKeys

hiteshaneja avatar Feb 25 '16 22:02 hiteshaneja