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

∥☆過路亽.° 提交于 2021-02-07 13:21:42

问题


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.


回答1:


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.




回答2:


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;
}


来源:https://stackoverflow.com/questions/35086613/first-character-is-missing-inconstantly-while-sending-string-to-the-extjs-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!