Getting Started With Watir: Web Application Testing In Ruby

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated October 27, 2024
Edited by Swati

Edited by Swati

I’m Swati. I accidentally started testing in 2004, and since then have worked with at least 20 clients in 10 cities and 5 countries and am still counting. I am CSTE and CSQA certified. I love my job and the value it adds to software…

Learn about our editorial policies.

Watir is an Acronym for ‘Web Application Testing In Ruby’. Learn How to Install And Use Watir to Create And Run Test Cases With Code Examples:

Watir is pronounced as Water. It is an open-source collection of Ruby libraries. As it is the Web Application Testing In Ruby, it uses Ruby as its scripting language.

watir tutorial

Installation Of Watir

One should have basic knowledge of HTML, Programming, and Ruby to use Watir. However, as Ruby itself is easy to use and understand, learning it and using Watir is not that difficult.

Watir Installation For Windows

There is a great chance that you don’t have Ruby installed on your computer. To check that, open Command Prompt and type >ruby –v and press ‘Enter’.

Fig 1: If you don’t have Ruby installed, you will get the following message.

Command Prompt - Ruby

If Ruby is installed, you will get its version with this command. You can click here to download the latest version of Ruby. Select the appropriate version, the website itself will recommend Ruby 2.4.X as x64 or x86 installer.

Fig 2: When you click on this version, you will get the following window.

Download Ruby

Fig 3: Click on the Start Download button and you will see the following window.

Start Downloading Ruby

As the downloading process is paused for the purpose of taking a screenshot, you are able to see the ‘Start’ button, otherwise, you will be able to see the ‘Pause’ button here. Now go to the location/folder where your file is downloaded (here as you can see in Fig 2, location is: C:\Users\Ojas\Downloads\Programs).

Fig 4: Execute the rubyinstaller-2.4.3-2-x64.exe file. 

Ruby License Agreement

Fig 5: Select ‘I accept the License’ and click ‘Next’:

Accept License

Fig 6: Click Install and then Finish. 

Install Ruby

The above screen will ask ‘Which components to be installed?’ If you are not sure, just hit the Enter key and the system will install all the three components i.e. MSYS2 base installation, MSYS2 system update and MSYS2 and MINGW development toolchain.

MSYS2 tools are necessary to compile C based ruby gems. MSYS2 can also be installed manually.

Fig 7: MSYS2 Installed Manually

installingmsy

It will take a few minutes to install all the components. Just close the Command Prompt and reopen it. Then type >ruby –v and hit the Enter key. If you get the following message then it means that Ruby is installed successfully.

ruby 2.4.3p205 (2017-12-14 revision 61247) [x64-mingw32]

Software that is written in Ruby is called Ruby Gems. We have installed Ruby but there is a possibility that we might not have the latest version of these Ruby Gems. Hence, first, we need to check this gem version using the command >gem –v, here we got the version 2.6.14.

We will update it using the command:

>gem update –system

Once done we will get the message– RubyGems system software updated.

Now we need to install Selenium web driver gem using the command [>gem install the selenium-web driver –no-ri –no-rdoc].

It will get installed and you will get the following message:

Fetching: selenium-web driver-3.11.0.gem (100%)

Successfully installed selenium-web driver-3.11.0

1 gem installed

**If you get any Windows Firewall message, then close that window.

Now we need IEDriverServer. Click here to Download IEDriverServer

Click on the first link and then select the appropriate version for your computer i.e. Win32 or X64.Zip file will get downloaded. To know where to extract these files follow the below steps:

Go to Command prompt.

Type >PATH

Fig 8: The following result will be obtained: 

Type - PATH - Command Prompt

We will extract our files to PATH C:\Ruby24-x64\bin. Extract files to your Ruby folder path. Now type irb in the command prompt and press ENTER. IRB stands for Interactive Ruby.

>require “selenium-webdriver”

You will get =>true

>browser = Selenium::WebDriver.for:internet_explorer

Your IE browser will get open and you will see the message: “This is the initial start page for the WebDriver server”.

To open web page www.google.co.in in the IE browser type in the command:

>browser. get “https://www.google.co.in/”

The following table will give you a list of drivers of the respective browsers and their download links. Follow the same procedure for the other drivers as well as explained for IEDriverServer.

Browser NameDriver Name Download Link
Firefoxgeckodriverhttps://github.com/mozilla/geckodriver/releases
Google Chromechromedriverhttp://chromedriver.storage.googleapis.com/index.html
Safari
Safari does’t work for windows

Watir Installation:

  • Go to the command prompt.
  • Type command >gem install watir
  • Hit the enter button.

Watir will get installed. Now you can start using Watir.

Watir Installation For Mac

Steps:

  • Installing Ruby
  • Installing Ruby gem
  • Updating Ruby gem
  • Installing Selenium WebDriver
  • Installing IE Driver/Gecko Driver/Chrome Driver/safari driver
  • Installing Watir

There is no need to install Ruby on Mac. Hence we have already covered the first step. It will save a lot of time. We will check the version of ruby with the command:  $ruby -v

Here, we have it as — ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin16]

Now we will check the version of ruby: gem $ gem -v

Here, it is — 2.0.14.1. We will update our ruby gems using the command- $ sudo gem update —system. We will install Selenium WebDriver using the command: $sudo gem install selenium-webdriver —no-ri —no-rdoc

There is a possibility to get the following error while installing Selenium webdriver or updating the ruby gem.

Error: YAML safe loading is not available. Please upgrade the psych to a version that supports safe loading (>= 2.0).

To solve this error we need to update our Ruby. To update Ruby we will first require Ruby version manager, you can get it using this command [$ curl -L https://get.rvm.io | bash -s stable]. To install the latest version of Ruby type [$ rvm install ruby -[version]]. Restart the terminal once the rvm is completely installed.

Now we need to set this latest version as we want to use it. So type [$ rvm use ruby-2.4.1]. To set this version as the default one, use the following command: [$rvm —default use 2.4.1]. Now again update ruby gems. Try installing selenium-webdriver. If you get any error again, then you might require command line developer tools, hence install it using [$ git —version]. As we already have, we have not installed it here.

To install the chrome driver, we need homebrew [$ ruby -e “$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)”].

Let’s install the Chrome driver. [$ brew install chromedriver].

Try this code in terminal:

$irb
>require “selenium-webdriver”
You will get >true
browser=Selenium::WebDriver.for:chrome
browser.get “https://www.google.co.in”

With this code, your browser should open with the specified webpage. Now we will install Watir: [$ gem install water].

We are done with the installation process. You can now start writing test scripts.

How To Use Watir: Creation And Execution Of Test Cases

You can write the test cases or scripts in Notepad. Save this file with .rb extension.

Example: Suppose you have created one file test.rb. To run this script/program double-click this file or follow the steps listed below. Suppose you have saved this file in the C drive. The address of this file is “C\Ruby_Programs”.

>cd\
>cd Ruby_Programs
>ruby test.rb

Hit the ENTER button. To edit this file, right-click on this file and select the option Open with.

#1) Running Test Cases In Parallel

If you have written two test cases and saved them as test1.rb and test2.rb. To run these two tests in parallel, you need to follow these steps:

Fig.9: Go to the command prompt and type [>gem install parallel_tests]. You will see the following message.

parallel_test gem install

Then run your tests.

>parallel_test test1.rb test2.rb

#2) Creating Test Suite

If you want a few tests to run frequently and add them in the test suite then here is the code for it.

Code:

require 'minitest/autorun'
require 'C:/Ruby_Programs/test3' # This is the path of your test file which you want to include.
require 'C:/Ruby_Programs/test4' # This is the path of your test file which you want to include.

Save this file as testSuite.rb and run

C:\Ruby_Programs>ruby testSuite.rb

Watir Examples

Example 1:

As this is our first program we will just open the “Google.co.in” web page and click on the Gmail link.

Code:

require 'watir'
test_site = "https://www.google.co.in/"
browser = Watir::Browser.new:firefox
browser.goto test_site
browser.link(:href, "https://mail.google.com/mail/?tab=wm").click
puts “Test Executes”
browser.close

Understanding The Code

#1) require ‘watir’ # Here we are telling that we will require Watir library to run our program and it will get loaded with this statement.

#2) test_site = “https://www.google.co.in/” # Here we are declaring the variable.

#3) browser = Watir::Browser.new:firefox # Here we are opening the Firefox browser.

Watir::Browser.new:internet_explorer # For Internet Explorer

Watir::Browser.new:safari # For Safari

#4) browser.goto test_site # Or browser.goto “https://www.google.co.in/” # goto command is used to navigate to the specified website.

#5) browser.link(:href, “https://mail.google.com/mail/?tab=wm”).click # Here we click the Gmail link. The hyperlink is referred as a link in the code and we are using its “href” property to locate this hyperlink.

#6) puts “Test Executed” # This line will write the string “Test Executed” on the command prompt.

#7) browser.close # This line will close the browser.

If we don’t mention the browser names, then we will write only this line in our code [Watir::Browser.new], Watir will open the Chrome browser as Chrome is the default browser for Watir.

Example 2:

In this example, we will open the web page www.google.co.in. Then we will search the text “Manual Testing”. Next, we will come back to the home page and click the “Gmail” link.

Code:

require 'watir'
browser=Watir::Browser.new:firefox
browser.goto "https://www.google.co.in"
if browser.title.include? "Google"
browser.text_field(:class, "gsfi").set "Manual Testing"
browser.button(:name, "btnK").click
if browser.text.include? "Manual Testing is a process of finding out the defects"
puts "Title of webpage "+ browser.title
browser.img(:src, “/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png").click
browser.link(:class, "gb_P").click
else
puts "Wrong result found"
end
else
puts "Either you are on wrong page or page not found"
end
puts "Now title of webpage "+ browser.title
browser.close
puts "Test Executed Successfully"

Understanding the Code:

#1) browser.goto https://www.google.co.in – Here we are navigating to the specified website.

#2) if browser.title.include? “Google” – Here we are checking the title of the webpage as whether it is as expected or not. In the else part of this ‘If’ we are putting this statement “Either you are on the wrong page or page not found”. This statement will get displayed on the command prompt.

#3) browser.text_field(:class, “gsfi”).set “Manual Testing” – Here we are entering the text “Manual Testing” in the search textbox.

#4) browser.button(:name, “btnK”).click – Here we are clicking the “Google Search” button.

#5) if browser.text.include? “Manual Testing is a process of finding out the defects” – Now we are checking the text on the webpage to verify if we got the correct search result. In the ‘Else’ part of this ‘If’ statement we are putting the statement “Wrong result found”.

#6) puts “Title of webpage “+ browser.title – Here we are displaying the title of the webpage on the command prompt.

#7) browser.img(:src, “/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png”).click – Here we are clicking on the image to go back to the homepage.

#8) browser.link(:class, “gb_P”).click – Here we are clicking the Gmail link.

#9) puts “Now the title of webpage “+ browser.title – Now again we are putting the title of the webpage on the command prompt. It should be the title of the Gmail page.

#10) browser.close – Here we are closing the browser.

#11) puts “Test Executed Successfully”. Just to make sure that all the steps are executed correctly we are putting this statement on the command prompt.

Example 3:

In this example, we will try to use maximum Watir commands, so that you will understand how to use them. We will see its description as well along with the code.

Code with Comments:

require 'watir'
browser=Watir::Browser.new:chrome
browser.goto “http://watir.com/examples/forms_with_input_elements.html”

#Here we will see how to insert data in text field.
t1=browser.text_field id: 'new_user_first_name'
t1.set "Testing"
t2=browser.text_field id: 'new_user_last_name'
t2.set “testdata"

#Here we will see how to select data from select list.
sl=browser.select_list id: 'new_user_languages'
sl.select 'English'
sl.selected_options

#Here we will see how to select radio button.
rd=browser.radio value: 'no'
rd.set
chk=browser.checkbox value: 'cars'
chk.set

#Here we will see how to click button.
btn=browser.button name: 'new_user_button_2'
btn.click

#This is an Explicit wait statement. This statement will make the program wait #until the particular event happens.
Watir::Wait.until {browser.text.include? 'submit'}

# Here I used this if statement  because this is a test webpage and nothing is #happening after clicking the button. So just to show that button is clicked I used If #statement.
if browser.text.include? "submit"
puts "Data Submitted"
else
puts "Data not yet submitted"
end

# This is an implicit wait statement. It will make your program to wait for 30 seconds compulsory.
browser.driver.manage.timeouts.implicit_wait = 30

# You can send special keys to browser. There is a big list of these keys.
browser.send_keys :page_up

#Here we are taking the screenshot and saving it in a png file.
browser.screenshot.save ‘screenshot1.png'

browser.send_keys :page_down
browser.screenshot.save ‘screenshot2.png'

puts "TestCase Executed"
browser.close

Waits And Special Keys

There are two types of wait statements supported by Watir. First is an explicit wait and the second one is an implicit wait. We have seen the syntax for both in the above code i.e. Example 3.

Special Keys supported by Watir: First, we will see the syntax for using these special keys.

  1. browser.send_keys :page_up #We have used this in our code.
  2. browser.element.send_keys [:control, ‘c’], :cancel
  3. browser.element.click(:enter, :space)

Following is a list of the special keys that are supported by Watir:

:enter
:f1
:control
:numpad0
:shift:f2
:alt:numpad1
:null:f3:pause:numpad2
:cancel:f4:escape:numpad3
:help:f5:space:numpad4
:backspace:f6:end:numpad5
:tab:f7:home:numpad6
:clear:f8:left:numpad7
:return:f9
:up:numpad8
:left_shift:f10:right:numpad9
:left_control:f11:down:multiply
:page_up:f12:insert:add
:page_down:meta:delete:subtract
:arrow_left:command:equals:divide

Regression Testing With Watir

In regression testing, we check if the changes have affected the unchanged part or not.

For that, we need to rerun our previous test cases. Regression testing will be time-consuming if done manually. Watir is one of the best regression testing tools as writing, maintaining/updating and executing test cases are easy.

Conclusion

To conclude, we can say that Watir is easy to learn the tool. It is an open-source tool, hence it is cost-effective and easy to use. Although it doesn’t have so many advantages over Selenium WebDriver, it is a better tool when compared with many other tools.

It supports programming, hence it much better than other recording and playback tools. It is indeed one of the best regression testing tools.

Have you given a try to the Watir tool?

Was this helpful?

Thanks for your feedback!

Leave a Comment