Mike
Mike

Reputation: 795

First character is missing inconstantly while sending string to the ExtJS input via sendKeys()

I randomly face the issue of missing first character in the ExtJS5 input field, while sending string via sendKeys method.

System info: Ubuntu 14.04 -> docker containers with selenium grid (2.48.2) Browser Firefox

Code is simple. I just get input web element, wait if it's clickable (i.e. isEnabled and isDisplayed), clear and send string:

wait.until(ExpectedConditions.elementToBeClickable(input)).clear();
input.sendKeys(value);

input element is simple too:

<input id="textfield-1455-inputEl" data-ref="inputEl" type="text" role="textbox" size="1" name="name" class="x-form-field x-form-required-field x-form-text x-form-text-default x-form-focus x-field-form-focus x-field-default-form-focus" autocomplete="off" componentid="textfield-1455"/>

I've noticed that issue occurs only for the first sendKeys() executing on the page:

Other occurrences of the sendKeys on the page are stable.

I've looked for similar questions. It does not seem the issue with special characters (Missing characters example: 46-> 6; coverTest -> overTest; 1 -> nothing);

Also, I don't think it's an issue with missing characters due to remote webdriver infrastructure. The tests fail randomly but in exact places.

I know that I can use sendKeys(), then check the value of the input and repeat the sending action. However, it's the last option.

Is there any additional check needed for ExtJS input (any attribute in DOM) in order to be sure that input field is ready?

Appreciate your help.

Upvotes: 6

Views: 4030

Answers (2)

Austin
Austin

Reputation: 596

I was struggling with sendKeys failing my self, but the following works pretty consistently. The method findVisibleElement is a custom wrapper for driver.until....

protected static boolean sendKeysByChar(By by, String input)
{
        WebElement field = driver.findVisibleElement(by).base();

    field.click();
    field.clear();

    for (int i = 0; i < input.length(); i++) {
        String current = driver.findElement(by).getAttribute("value");
        String nextChar = String.valueOf(input.charAt(i));
        while (current.length() <= i || !current.endsWith(nextChar)) {
            field.sendKeys(nextChar);
            current = driver.findElement(by).getAttribute("value");
        }
    }

    field = driver.findElement(by); // Refresh element
    if (field.getAttribute("value").equals(input)) { return true; }

    log.warn("Send keys by char failed.");
    return false;
}

Upvotes: 0

Prateek
Prateek

Reputation: 1145

Some times it happens with me. Try clicking on to the field first, but it's a wild guess assuming there can be some focus related issues. Your sequence could be somewhat like this:

wait.until(ExpectedConditions.elementToBeClickable(input)).click();
input.clear();
input.sendKeys(value);

Weird thing is that I actually faced a situation, where I clicked it twice before sending values and it worked somehow :P

Another thing to try could be using a non-native javascript executor.

JavascriptExecutor myExecutor = ((JavascriptExecutor) driver);
myExecutor.executeScript("arguments[0].value='6';", input);

Sorry man, if the system would have been in front of me I'd have tried much more things.

Upvotes: 3

Related Questions