Dealing with alert boxes

Written by Aidy.

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

 

Testing Translations & Foreign Characters

Written by Aidy.

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

 

Convert Drop-Down to Array

Written by Aidy.

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[0] 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).

Dealing with iframes

Written by Aidy.

Sometimes people use iframes on websites and when they do this and they put elements like buttons that you want to click inside the iframes then this means you actually have to reference the iframe aswell as the element you want to use. To illustrate this I've created another test web page that's basically just an iframe within which I've put the other test page. You can check it out here: http://www.lazyautomation.co.uk/lazy2.html

If you right click on the 'Hello World' button and inspect it in Google Chrome you'll notice that nothing's really changed with this button but if you attempt to click it on this new page using any of the techniques we discussed in previous tutorials it won't actually work on this page. If you scroll up you'll notice the tag for the iframe that the button is now sitting in, it has an id of 'iframe01'.

Here's the new code that let's you click the button within the iframe.

require "watir-webdriver"
b = Watir::Browser.new :chrome

b.goto 'http://www.lazyautomation.co.uk/lazy2.html'
b.iframe(:id, 'iframe01').button(:id, 'hello1').click

 

As you can see, on line 5 we've simply reference the iframe before the button. Easy!