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

2

6

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:

  • Enter the page, wait for page load, work with first input
  • Enter the page, wait for page load, choose Enable into corresponding select box in order to enable input field, work with input field (image with this example is attached)
  • Enter the page, wait for page load, click button add in order to add the needed input field, work with input field

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.

Rivas answered 29/1, 2016 at 14:12 Comment(2)
Have you made sure that this is not a bug in Ext framework? I remember that my datefield didn't take the first character typed after tabbing into it because the field focus event was fired only after I started typing. Everything was fine if one used the mouse. I can't find the sencha forum thread, but I think it was fixed in 4.2.1 or 4.2.2 (which were released AFTER the first versions of Ext5).Burgenland
Thanks for a real example. However, If so, I think this issue will not occur randomly. At least, it would appear more frequently and for all possible cases with my STR. My ExtJS version is 5.1.2.748 and the issue occurs for several releases.Rivas
L
3

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.

Lahey answered 30/1, 2016 at 9:14 Comment(1)
Thanks. I have already had provided by you variants in my mind =) I was trying to find a root of the issue and thought input field is not ready. Weird thing is that the issue appears randomly.Rivas
C
0

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;
}
Careworn answered 7/9, 2017 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.