I have a text field to input voucher code, and I already tried this;
driver.findElement(By.id("voucher_code")).sendKeys("AL982", "OK992", "PO982", "SX982", "LQ925");
In the text field it become like this:
AL982OK992PO982SX982LQ925
What I want is to be like this:
AL982
OK992
PO982
SX982
LQ925
Because the system read, 1 row = 1 voucher code.
How do I use sendKeys to input multiple value in different rows?
And is it possible to shorten my code for multiple random value like above?, (in real case we have 1000 voucher code)
-
Manual test for automation above will be like this; type voucher code 1 -> press keyboard Enter -> type voucher code 2-> and so on.BetaTester– BetaTester2018年11月06日 08:11:14 +00:00Commented Nov 6, 2018 at 8:11
2 Answers 2
\n is your friend here. sendKeys actually recognizes them as linebreaks, at least in chrome, ff an edge. Can't say anything about other webdriver implementations as I don't use them.
void addVouchers(String[] codes, WebElement input) {
for(String code: codes) {
input.sendKeys(code+"\n");
}
}
should do what you want.
-
/n is my new friendBetaTester– BetaTester2018年11月06日 09:32:32 +00:00Commented Nov 6, 2018 at 9:32
When you are passing comma seperated value, it will append all of it and pass it to field.
you can try this to pass multiple values in different fields.
String[] voucherCodes = {"AL982", "OK992", "PO982", "SX982", "LQ925"};
WebElement field = driver.findElement(By.id("yourIdforEveryField"));
for (String voucher : voucherCodes) {
field.sendKeys(voucher);
};
-
Tried and It didn't work, all value still appear in the same row within text field.BetaTester– BetaTester2018年11月06日 07:48:48 +00:00Commented Nov 6, 2018 at 7:48
-
Try this - String[] voucherCodes = {"AL982", "OK992", "PO982", "SX982", "LQ925"}; String[] fieldIds = {"abc1", "abc2", "abc3", "abc4", "abc5"}; for (int i = 0; i < fieldIds.length; i++) { WebElement field = driver.findElement(By.id(fieldIds[i])); field.sendKeys(voucherCodes[i]); }Joshi– Joshi2018年11月09日 04:56:01 +00:00Commented Nov 9, 2018 at 4:56
Explore related questions
See similar questions with these tags.