Dear reader,

This site finally got its much-needed overhaul. I tried to keep existing pages at their original URLs.
Should you find an error, please do tell me at contact@aspyct.org.
Feel free to visit the new website, which may contain more documentation pertaining to your search.

Thank you for visiting aspyct.org, I hope you'll find what you're looking for.

Old Aspyct.org blog

 or  go to the new site.

Functional web testing with selenium and ruby

I can see two reasons to use automation: 1) because you’re lazy a good developer and don’t want to do a task by hand twice, and 2) because you prefer turning a boring task into a neat scripting challenge :) Well, next time you do anything related to a web interface, think about selenium and ruby!

Let’s not wait longer, you will need the selenium-webdriver gem. I personally use RVM, and therefore some commands will be slightly different. If you don’t use it yet, well you should probably, it’s great!

1
rvm all do gem install selenium-webdriver

And let’s start by opening an interactive ruby shell and play somewhat with the selenium API (some output lines are stripped for clarity).

1
2
3
$ irb
> require 'selenium-webdriver'
> browser = Selenium::WebDriver.for(:firefox)

By this time, firefox should be opening and soon ready. If you don’t have firefox installed on your machine, now is the time ;) So let’s open the google homepage.

1
> browser.get('http://www.google.be')

Make a search, for “aspyct” for example. Of course you will first need to locate the input. You’re probably familiar with firebug or equivalent, so find the id of that input. If not, simply right click the input and “inspect element”.

So, for me, the id is gbqfq. Now grab this element from our script, and send some text to it. And by the way, click on the search button.

1
2
3
4
> input = browser.find_element(:id, 'gbqfq')
> input.send_keys('aspyct')
> button = browser.find_element(:id, 'gbqfb')
> button.click

Now that you’ve found such a wonderful website (that is, aspyct.org), for sure you want to visit it! Click the link named “Aspyct.org”:

1
2
> link = browser.find_element(:link, 'Aspyct.org')
> link.click

And you probably want to make sure that you landed on the correct website. You could check the page title, or browse the DOM with XPath.

1
2
3
4
> browser.title
 => "Aspyct.org"
> browser.find_element(:xpath, '//h1').text
 => "Aspyct.org"

Now you know what to do next time you have to fill the fields with the data from an CSV file ;) But most importantly, you can automate your functional tests and develop safer and faster. Have fun!

See also: Selenium WebDriver documentation.