It's an old question but still valid. I use Chrome Driver v2.53.
It looks like the keys are being send one by one to the browser (like a separate keyDown events). When it happens too fast then one of two results can be observable:
- characters are shifted
- characters are missing
My solution is as follows:
protected void sendKeys(final WebElement element, final String keys) {
for (int i = 0; i < keys.length(); i++){
element.sendKeys(Character.toString(keys.charAt(i)));
waitUntil(attributeContains(element, "value", keys.substring(0, i)));
}
}
It is reliable and works fast enough. What is more, when we want to clear the input field before sending the keys then the same event shift can occur, e.g:
element.clear();
element.sendKeys("abc");
It is possible that the clear operation will happen in one of four places:
- before sending the letter "a"
- before sending the letter "b"
- before sending the letter "c"
- after sending the letter "c"
I recommend to always check if the operation we've just executed has been accomplished successfully, e.g.: when we want to clear the input field it's a good practice to:
- check the value of the input field
- if the value is an empty string then return
- if the value is not an empty string then call clear() function and wait until the value will be equal to an empty string
It's a lot of operation to execute for a simple task. However, it will make the test more stable.