Skip to content

Instantly share code, notes, and snippets.

@hanikhan
Last active August 5, 2018 11:11
Show Gist options
  • Save hanikhan/df426631c5b317630a6d7d150817c3b0 to your computer and use it in GitHub Desktop.
Save hanikhan/df426631c5b317630a6d7d150817c3b0 to your computer and use it in GitHub Desktop.
Custom method to handle issue with sendKeys on IE 11 where some keys are skipped or not entered correctly
//Currently, the code uses element.getAttribute("value") to get text from text box. This can also be replaced with element.getText() based on the type of text box
private void CustomSendKeysIE(WebElement element, String stringToEnter) throws InterruptedException {
//Convert String to be entered to a character array
char[] charsToEnter = stringToEnter.toCharArray();
//Iterate characters in the character array over a loop
for(int i=0;i<charsToEnter.length;i++){
//Adding a sleep of 500 milliseconds
Thread.sleep(500);
//Send character at index i
element.sendKeys(String.valueOf(charsToEnter[i]).toString());
//Check if entered character(s) match with the substring
if(!element.getAttribute("value").equals(stringToEnter.substring(0,i+1))){
//Enter the character again if text length does not match(Indicates that a character was skipped OR not typed)
if(!(element.getAttribute("value").length()==(i+1))){
element.sendKeys(String.valueOf(charsToEnter[i]));
//Check if entered character(s) match with the substring
if(!element.getAttribute("value").equals(stringToEnter.substring(0,i+1))){
//use throws or similar statement to throw an exception instead of syso based on your test case requirement
System.out.println("Throw Exception");
}
}
//If text length matches, it indicates that an invalid character was entered
else if(element.getAttribute("value").length()==(i+1)){
//Send BACK_SPACE to the text box
element.sendKeys(Keys.BACK_SPACE);
//send the character at index i again
element.sendKeys(String.valueOf(charsToEnter[i]));
//Check if entered character(s) match with the substring
if(!element.getAttribute("value").equals(stringToEnter.substring(0,i+1))){
//use throws or similar statement to throw an exception instead of syso based on your test case requirement
System.out.println("Throw Exception");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment