Sometime ago, I started working with Watir-WebDriver – an API that runs on top of Selenium 2.0 which means it is capable of automating most major browsers. Watir is short for Web Application Testing in Ruby (my new favorite language). Don’t get confused with Watir and Watir-WebDriver though – Watir only automates IE.
I am on Windows (v7 x64) but the installation for your OS shouldn’t be much different. I am assuming you have Ruby installed already. To begin, download the development kit from RubyInstaller.org that applies to your system. If you are on either Linux or Mac OS X, refer the downloads page at Ruby-Lang.org. After download is complete, follow these steps:
- Unzip the file
- Open shell prompt (cmd.exe) and set the folder where you unzipped all files
- In shell, execute the following:
ruby dk.rb init ruby dk.rb review ruby dk.rb install gem install watir-webdriver --platform=ruby
If you encounter the below error at step
ruby dk.rb review:
Simply add the PATH to your ruby installation directory to your config.yml file. For example, Ruby is installed in
C:\Ruby200-x64 on my system so the new content of the file becomes:
# This configuration file contains the absolute path locations of all # installed Rubies to be enhanced to work with the DevKit. This config # file is generated by the 'ruby dk.rb init' step and may be modified # before running the 'ruby dk.rb install' step. To include any installed # Rubies that were not automagically discovered, simply add a line below # the triple hyphens with the absolute path to the Ruby root directory. # # Example: # # --- # - C:/ruby19trunk # - C:/ruby192dev # --- - c:/Ruby200-x64
ruby dk.rb review again but this time it should pass.
You can resume from step
ruby dk.rb install and finish installing Watir now.
Here are some examples demonstrating basic usage.
# create new firefox instance b = Watir::Browser.new :firefox # navigation b.goto "http://newtours.demoaut.com" # check if textbox exists using b.text_field(:name => "userName").exists? # set value b.text_field(:name => "userName").set "test" b.text_field(:name => "password").set "test" # click login b.button(:name => "login").click # radio b.radio(:value => "oneway").set # is this value selected? puts b.radio(:value => "oneway").set? # listbox b.select_list(:name => "fromPort").select "Portland" # verify if Portland selected puts b.select_list(:name => "fromPort").selected?("Portland") b.radio(:value => "Business").set puts b.radio(:value => "Business").set? # div: when its present, output text puts b.div(:class => "footer").when_present.text # screenshot b.screenshot.png b.screenshot.save "file.png"
Singular vs Plural
This is a very neat feature – use singular to find the first occurence of the object or use plural to find all its occurences.
b.div # returns the first div b.divs # returns a collection of div b.image # returns the first img b.images # returns a collection of img b.table(:id => "table").images # images inside a table b.divs(:class => "test") # all divs with class => test
This was just an intro so I only covered the basic usage of the API. I will write a comprehensive post with all types of elements and their various methods in the upcoming post.