Last active
August 29, 2015 13:57
-
-
Save alb-i986/9838948 to your computer and use it in GitHub Desktop.
Helper method that makes sure sendKeys enters the given text fully.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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