I've been putting together this series of videos on how to get started with Selenium, Ruby and Cucumber for web automation. Hope some of you find it useful.
Here's a cheeky bit of code that'll help you to dismiss an alert box. If you don't know what an alert box is then head to http://lazyautomation.co.uk/lazy4.html and click the 'Click Me' button to see one in action.
Now let's automate it! The following code will open a browser and navigate to the same test page, click the button and then dismiss the alert box. Then as an added bonus it'll also grab the text from the alert and output it to the screen just in case we need to know what it actually said.
require "watir-webdriver" b = Watir::Browser.new :chrome b.goto 'http://www.lazyautomation.co.uk/lazy4.html' b.button(:id, 'button1').click sleep(3) #I've put this sleep here just so you can actually see the alert before it gets dismissed puts "Alert displayed saying: " + b.alert.text b.alert.ok
This had me stumped for a little while, every time I went to test a website that used a language like German or Greek and a foreign character cropped up then my automated tests generally broke with the following error:
invalid multibyte char (US-ASCII) (SyntaxError)
Turns out that all you need to do is add one simple line to the beginning of any file that contains code that tests foreign characters and you're good to go! The line in question is:
# encoding: utf-8
This forces the use of the UTF-8 format. Although it seems slightly counter-intuitive as a hash symbol normally comments out a line in Ruby but anyway, here's an example of it in action. This piece of code will open a browser and navigate to a test page with a piece of German text on it. Try it out. You can also try deleting the first line to see it fail.
# encoding: utf-8 require "watir-webdriver" b = Watir::Browser.new :chrome b.goto 'http://www.lazyautomation.co.uk/lazy3.html' if b.text.include? "Ich mit Ruby und Watir sie sind sehr gut gefällt! Web Automation Felsen!" puts 'PASS - Found German Text' else puts 'FAIL - Did not find German text' end
It's possible to convert a drop-down to an array using just one line of code like so.
optionsArray = b.select_list(:id, 'options1').options.map(&:value)
As you can see we've created a variable called optionsArray and then assigned all the values from the specified dropdown to that variable as an array.
Now let's have a look at a couple of examples where this could be used.
Select Last Value From Drop-Down
Suppose you had a test that didn't care what the values were in a drop-down and you just wanted to make sure you always selected the last option from the list. This could be used to test something like the most expensive postage option for an online store or the largest t-shirt size. This is an example of how you can do that. This piece of code will open the test site and select the last option from the drop-down.
require "watir-webdriver" b = Watir::Browser.new :chrome b.goto 'http://www.lazyautomation.co.uk/lazy1.html' optionsArray = b.select_list(:id, 'options1').options.map(&:value) b.select_list(:id, 'options1').select_value(optionsArray[-1])
As you can we've navigated to the test site and then assigned all the values from the drop-down to an array.
The values in the array are indexed starting from 0. We can select any of those values by referencing the index in squared brackets. E.g. optionsArray would be the first value from the dropdown. [-1] which is what we've used in this instance goes back one and selects the last value.
Select Random Value From Drop-Down
It may occasionally be useful to add randomness to some tests, the code below will open up the test page and select a random option from the drop-down. Try running this multiple times to see different values get selected.
require "watir-webdriver" b = Watir::Browser.new :chrome b.goto 'http://www.lazyautomation.co.uk/lazy1.html' randomNumber = rand(0..2) optionsArray = b.select_list(:id, 'options1').options.map(&:value) b.select_list(:id, 'options1').select_value(optionsArray[randomNumber])
Instead of using an actual number for the index of optionsArray, this time around we've put a variable in there called randomNumber. Line 5 assigns a random number between 0 and 2 to randomNumber. There are three options in the drop down on the test page if there were 4 then obviously you'd need to increase the range to between 0 and 3 e.g. rand(0..3).