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)

1
  • Manual test for automation above will be like this; type voucher code 1 -> press keyboard Enter -> type voucher code 2-> and so on.
    – BetaTester
    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.

2
  • /n is my new friend
    – BetaTester
    Nov 6, 2018 at 9:32
  • Happy to help and thanks for the check mark.
    – Daniel
    Nov 6, 2018 at 9:41
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);
};
2
  • Tried and It didn't work, all value still appear in the same row within text field.
    – BetaTester
    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
    Nov 9, 2018 at 4:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.