Selenium sendKeys does not send all characters

I use Java, Selenium and Chrome to automate testing. Our developers recently upgraded our interface from AngularJS to Angular2 (not sure if this is important). But since then sendKeys has entered incomplete characters in the text field. Here is an example:

public void enterCustomerDetails() { txtFirstName.sendKeys("Joh201605130947AM"); txtSurname.sendKeys("Doe201605130947AM"); txtEmail.sendKeys(" johndoe@gmail.com "); } 

I also tried using executeScript. This did not work. It can enter full characters, but the form considers the field to be zero.

 public void forceSendKeys(WebElement element, String text) { if (element != null) ((JavascriptExecutor) this.webDriver).executeScript("arguments[0].value=arguments[1]", element, text); } public void enterCustomerDetails() { forceSendKeys(txtFirstName, "Joh201605130947AM"); forceSendKeys(txtSurname, "Doe201605130947AM"); forceSendKeys(txtEmail, " johndoe@gmail.com "); } 

I also tried using .click () before .sendKeys and add while sleeping. They did not work either.

I got an idea to enter 1 to 1 characters from this message: How to enter characters one by one in a text box in selenium webdriver?

This worked, but that means I have to rewrite all my codes from sendKeys to a new function:

  public void sendChar(WebElement element, String value) { element.clear(); for (int i = 0; i < value.length(); i++){ char c = value.charAt(i); String s = new StringBuilder().append(c).toString(); element.sendKeys(s); } } public void enterCustomerDetails() { sendChar(txtFirstName, "Joh201605130947AM"); sendChar(txtSurname, "Doe201605130947AM"); sendChar(txtEmail, " johndoe@gmail.com "); } 

If you guys know a better way, please help! :)

+9
source share
5 answers

I assume this is caused by this Angular2 issue https://github.com/angular/angular/issues/5808

Angular cannot handle input events when they arrive too fast.

As a workaround, you will need to send single characters with a slight delay between them.

+4
source

I stumbled upon this error while doing integration tests using NightwatchJS (which uses selenium).

So, I am writing this for people coming here in the future.

I wrote this command for the extension for nightwatch:

 exports.command = function (selector, value, using) { var self = this; self.elements(using || 'css selector', selector, function (elems) { elems.value.forEach(function (element) { for (var c of value.split('')) { self.elementIdValue(element.ELEMENT, c); } }); }); return this; }; 

What can be used in this way:

 var username = ' integration@test.com '; browser.setValueSlow('input[ngcontrol=username]', username); //Works with ng2! 

This issue has also been discussed on NightwatchJS github here.

+1
source

This is due to a bug in Angular applications. The workaround is to set the sleep function.

 public void setText(By element, String text) { sleep(100); // Angular version < 2 apps require this sleep due to a bug driver.findElement(element).clear(); driver.findElement(element).sendKeys(text); } 
0
source

I also got this error in Java, Selenium. This error can also occur when writing your codes: "sendKeys (CharSequence) from the Webelement type refers to the missing charSequence type"

I tried to vary the wait time and even enter additional characters in front of the main ones, they did not work.

I used a simple trick to change the Java compiler version from JRE 9 to JRE 10.

0
source

try this code .. another way to set values ​​using javascript WebDriver driver = new FirefoxDriver (); JavascriptExecutor driver jse = (JavascriptExecutor); jse.executeScript ("document.getElementsByName ('body') [0] .setAttribute ('type', 'text');"); . Driver.findElement (By.xpath ("// input [@ name = 'body']")) clear (); driver.findElement (By.xpath ("// input [@ name = 'body']")). sendKeys ("Ripon: body text");

-2
source

All Articles