0

I would like to create a test for my web app using Ruby and watir webdriver. In my test I need to open 5 browser windows and enter credentials to login to my web app. So far everything works good except one thing - I'm doing a simple loop of 5 iterations and enter same username and pwd in each iteration but my goal is to enter different usernames and pwd's each time. So, what is the best way to do that? Loop through value-key pair?

Here is my example code:

  $i = 0
  $num = 5
  while $i < $num  do
    b = Watir::Browser.new
    b.goto 'https://localhost/mywebapp'
    b.text_field(id:'username').set 'test1'
    b.text_field(id:'password').set 'test1pwd'
    b.button(id: 'submitBtn').click
SGM
  • 97
  • 1
  • 9

1 Answers1

0

Yes, you could loop through key-value pairs by doing this:

logins = { 'username1' => 'password1', 'username2' => 'password2' } #And so on for your 5 accounts.
logins.each_pair do |user, pass|
  b = Watir::Browser.new
  b.goto 'https://localhost/mywebapp'
  b.text_field(id:'username').set user
  b.text_field(id:'password').set pass
  b.button(id: 'submitBtn').click
end

An alternative would be to load the login details from a CSV file.

Wilco van Esch
  • 457
  • 1
  • 7
  • 17
  • Fantastic, Thank you! RE CSV - I assume it's something like this - http://stackoverflow.com/questions/4410794/ruby-on-rails-import-data-from-a-csv-file – SGM Jan 25 '16 at 13:24
  • Exactly, and within the row loop you could then get the values with row[0] and row[1] or with row['Username'] and row['Password'] (if the details are in the first two columns and have headers called Username and Password. Thanks for accepting the answer! – Wilco van Esch Jan 25 '16 at 14:40