Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium sendKeys are not sending all characters

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! :)

like image 987
Larica B Avatar asked May 13 '16 01:05

Larica B


People also ask

What if sendKeys is not working in Selenium?

If we encounter issues while working with the sendKeys method, then we can use the JavaScript Executor to input text within an edit box. Selenium can run JavaScript commands with the help of the executeScript method. JavaScript command to be used is passed as a parameter to this method.

What is the alternative of sendKeys method?

We can input text in the text box without the method sendKeys with thehelp of the JavaScript Executor. Selenium executes JavaScript commands with the help of the executeScript method.

Is sendKeys () a WebElement method?

Entering Values in Input Boxes To enter text into the Text Fields and Password Fields, sendKeys() is the method available on the WebElement in Selenium. Using the same example of http://demo.guru99.com/test/login.html site, here is how we find the Text field and Password fields and enter text in Selenium.

How do I pass sendKeys in Selenium WebDriver?

We simply locate the text field where we have to enter the email ID and then pass on the string (which is our email ID) to the sendKeys() method. Next, we locate the 'Start Free Testing' button and click the same.


1 Answers

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.

like image 161
Günter Zöchbauer Avatar answered Sep 25 '22 11:09

Günter Zöchbauer