0

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.

  1. How do I use sendKeys to input multiple value in different rows?

  2. And is it possible to shorten my code for multiple random value like above?, (in real case we have 1000 voucher code)

asked Nov 6, 2018 at 2:40
1
  • Manual test for automation above will be like this; type voucher code 1 -> press keyboard Enter -> type voucher code 2-> and so on. Commented Nov 6, 2018 at 8:11

2 Answers 2

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.

answered Nov 6, 2018 at 9:04
1
  • /n is my new friend Commented Nov 6, 2018 at 9:32
0

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);
};
João Farias
11.2k2 gold badges21 silver badges41 bronze badges
answered Nov 6, 2018 at 3:18
2
  • Tried and It didn't work, all value still appear in the same row within text field. Commented 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]); } Commented Nov 9, 2018 at 4:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.