Skip to content

Instantly share code, notes, and snippets.

@alb-i986
Last active August 29, 2015 13:57
Show Gist options
  • Save alb-i986/9838948 to your computer and use it in GitHub Desktop.
Save alb-i986/9838948 to your computer and use it in GitHub Desktop.
Helper method that makes sure sendKeys enters the given text fully.
/**
* Enter the text with {@link WebElement#sendKeys(CharSequence...)} and loop
* (max 50 attempts) until the text is found to have been actually fully entered.
* Needed when for some reason (maybe some JS making WebDriver lose focus from the field)
* {@link WebElement#sendKeys(CharSequence...)} does not do its job and does not send all the
* characters it was asked to.
*
* @param text the text to enter in the text field
* @param textField a text field
* @param driver
*
* @throws WebDriverException after 50 unsuccessful loops
*/
public static void enterTextLoop(String text, WebElement textField, WebDriver driver) {
int i = 0;
int maxAttempts = 50;
while(i<maxAttempts && ! textField.getAttribute("value").equals(text)) {
textField.clear();
textField.sendKeys(text);
logger.debug("after sendKeys, textField contains: " + textField.getAttribute("value"));
i++;
}
if(i == maxAttempts)
throw new WebDriverException("can't enter text fully; giving up after " + maxAttempts + " attempts");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment