I use Java, Selenium and Chrome to automate testing. Our developers recently upgraded our interface from AngularJS to Angular2 (not sure if this is important). But since then sendKeys has entered incomplete characters in the text field. Here is an example:
public void enterCustomerDetails() { txtFirstName.sendKeys("Joh201605130947AM"); txtSurname.sendKeys("Doe201605130947AM"); txtEmail.sendKeys(" johndoe@gmail.com "); }
I also tried using executeScript. This did not work. It can enter full characters, but the form considers the field to be zero.
public void forceSendKeys(WebElement element, String text) { if (element != null) ((JavascriptExecutor) this.webDriver).executeScript("arguments[0].value=arguments[1]", element, text); } public void enterCustomerDetails() { forceSendKeys(txtFirstName, "Joh201605130947AM"); forceSendKeys(txtSurname, "Doe201605130947AM"); forceSendKeys(txtEmail, " johndoe@gmail.com "); }
I also tried using .click () before .sendKeys and add while sleeping. They did not work either.
I got an idea to enter 1 to 1 characters from this message: How to enter characters one by one in a text box in selenium webdriver?
This worked, but that means I have to rewrite all my codes from sendKeys to a new function:
public void sendChar(WebElement element, String value) { element.clear(); for (int i = 0; i < value.length(); i++){ char c = value.charAt(i); String s = new StringBuilder().append(c).toString(); element.sendKeys(s); } } public void enterCustomerDetails() { sendChar(txtFirstName, "Joh201605130947AM"); sendChar(txtSurname, "Doe201605130947AM"); sendChar(txtEmail, " johndoe@gmail.com "); }
If you guys know a better way, please help! :)
source share