Automated tests have been really popular in the past years. It’s now rare to ask a QA person about testing and not hear about automated tests. Long gone are the times when the QA would still test every single function when a deployment is made. Not only is this time consuming, it’s a waste of money.
What is Watir?
Watir (pronounced as water) is an open source family of Ruby libraries for automating web browsers. It controls a fully functional browser and navigates through websites just like we do. Since it tests your application through a browser, it can support integration tests regardless of what technology your application was developed in.
Why use Watir?
It’s free, lightweight and powerful. It uses Ruby, which is a great language for beginners. Testers with no programming background can easily start using it. It supports multiple browsers on different platforms and also has a great community.
Watir by itself only supports Internet Explorer on Windows. If you want to use Chrome, Firefox or Opera, you can check out watir-webdriver, which also supports running in headless mode. You can also try firewatir for Firefox on Windows.
Before you install Watir, make sure you already have Ruby installed. You can install Watir just like any other gem. You can choose between watir, watir-webdriver or firewatir.
gem install watir
If you’re having an error about an ‘ffi’ gem, you need to install DevKit first. Download DevKit and extract it to ‘C:devkit’. On the command prompt, navigate to ‘C:devkit’ and run these two commands, one after the other. After installing DevKit, you can now install the Watir gem.
ruby dk.rb init
ruby dk.rb install
And you’re ready to write your test script!
Sample Watir Test Script
Here’s a sample script to get your feet wet. Just enter this into a file with a .rb extension.
require 'watir-webdriver' # or whichever gem you choose
browser = Watir::Browser.new
browser.text_field(:name => 'q').set 'Watir'
browser.button(:name => 'btnG').click
puts browser.text.include? 'Watir'
Save and navigate to the file, then run it using:
Did you notice something? There’s no need to explain what’s going on in those lines of code, which is awesome! And that’s why it’s fun to use the Watir API, it’s very easy to use and also very readable.
If you’re still hesitating to use Watir, you should definitely give it a try, especially if this is your first time using web automation tools. Using Watir is really addictive, but remember that it’s not supposed to cover all edge cases in your application, those still need to be covered in unit tests. If you do feel that the tests are taking too much time, you might be interested in using the headless browser feature.
Watir also works well with Cucumber, which is like writing tests in pure English. I’m not a big fan of it, but if you like documenting stuff that is readable to you and other business people; it might be worth your while.