Selenium sendKeys are not sending all characters

Larica B picture Larica B · May 13, 2016 · Viewed 16.5k times · Source

I'm using Java, Selenium, and Chrome for test automation. Our developers recently upgraded our UI from AngularJS to Angular2 (not sure if that matters). But since then, sendKeys is inputting incomplete characters in to the text field. Here's an example:

    public void enterCustomerDetails()
    {   
        txtFirstName.sendKeys("Joh201605130947AM");
        txtSurname.sendKeys("Doe201605130947AM");
        txtEmail.sendKeys("[email protected]");
    }

I also tried using executeScript. It didn't work. It can enter complete characters but the form thinks the field is null.

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, "[email protected]");
        }

I also tried using .click() before .sendKeys and adding in sleep time. They didn't work too.

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

It worked but that means I have to rewrite all my codes from sendKeys to the 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, "[email protected]");
    }

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

Answer

G&#252;nter Z&#246;chbauer picture Günter Zöchbauer · May 13, 2016

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

Angular can't process input events when they arrive too fast.

As a workaround you would need to send single characters with a small delay between each.