protractor: sendKeys does not always enter all text

This bug has inconsistent reproduction. While in elementExplorer, I attempted to clear an input element and then send data to the element by performing the following command:

var el = element(by.css('[ngcontrol="username"]'));
el.clear().then(function() { el.sendKeys('Demo'); });

In about 30% (or less) of the time I would perform this action, the text “Do” would appear. (See attached image.) e2e_ node node__usr_local_bin_protractor_elementexplorer-conf_js--elementexplorer_ _100x34_and_vinelink

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 8
  • Comments: 25 (6 by maintainers)

Most upvoted comments

That is a good example @ludmilanesvitiy , I was able to reproduce it with your site. Works with IE 11 but not Chrome. For others, the elements used were

element(by.css('.things-filter-button-content')).click();
element(by.css('.things-search input')).sendKeys('TextThatShouldBeEntered');

I believe the website I am testing is using Angular 2.0.0-rc.1, but it is also made with Kendo UI 2016.1.412. There is no Angular that appears to be directly accessible. For example, typing ‘angular’ in the console throws an error. Unfortunately, I don’t think I am going to be able to make a reproducible example. Hopefully other people’s examples will be sufficient.

On the login page, which uses Angular 1.2.13, using the configuration file setting rootElement: '[ng-app]':

Both of these work

Username field <input required="" name="username" autofocus="" id="username" type="text" class="form-control ng-pristine ng-invalid ng-invalid-required" placeholder="Username" ng-model="model.username" maxlength="100">

Password field <input required="" id="password" name="password" type="password" class="form-control ng-pristine ng-invalid ng-invalid-required" placeholder="Password" ng-model="model.password" maxlength="100" autocomplete="off">

After it redirects, I have to call browser.useAllAngular2AppRoots(); for the protractor testing to continue working. Otherwise, I receive a “could not find testability for element” error. The main website after logging in does not have a ng-app present.

Input with the problem

<input class="form-control searchBox ng-untouched ng-pristine ng-invalid" name="searchValueInput" ngcontrol="searchParam" required="" tabindex="2" type="text">

Workaround

For anyone interested, a modified version of @pgrm 's workaround works well. Attempting to send keys normally once allows IE to continue faster. Only when the text of the input is incorrect do you erase it and enter one character at a time. Chrome enters single characters fairly quickly, so there is not too much lag.

searchBar.clear();
searchBar.sendKeys(searchTerm);
searchBar.getAttribute('value').then(insertedValue => {
  if (insertedValue !== searchTerm) {
    // Failed, must send characters one at a time
    searchBar.clear();
    var i;
    for(i = 0; i < searchTerm.length; i++){
      searchBar.sendKeys(searchTerm.charAt(i));
    }
} 

@nickwerline, thank you for solution, after i upgraded to Chrome 77 some fields just don’t want to be filled at all when i simply do element.click() --> element.clear() --> element.sendKeys(“text”) but i put your code with getAttribute(“value”) and then send String char by char and it’s working.

The best workaround I’ve found is setting up a for loop to send the characters one at a time.

var i;
var searchTerm = 'Text to Input';
for(i = 0; i < searchTerm.length; i++){
    inputBox.sendKeys(searchTerm.charAt(i));
}

Slows down Chrome a bit and IE by a lot, but it works.

My problem with sendKeys() actually only occurs on Chrome and not in IE11. And it works correctly on my app’s login page, but once it redirects and I call browser.useAllAngular2AppRoots(), Chrome starts having problems.

Windows 7 64-bit Selenium 2.52.0 Jasmine 2.4.1 Protractor 3.3.0 Chrome 51.0.2704.103 Chromedriver 2.21

This may be the same issue as https://github.com/angular/protractor/issues/2019 - could you share which browser and platform you are using? Can you reproduce this against the example application at http://angular.github.io/protractor/testapp/ng1/ ?

Note that this is almost certainly an issue with selenium and the browser, and probably the best we can do is report a bug upstream.

On Version Angular 2.0.0-rc.3 function sendKeys works good! Thank you, @juliemr !