Test

Written by Aidy.

Hello World.

Web Automation with Ruby and Cucumber

Written by Aidy.

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.

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