Feature image for Watir blog

Watir (Web Application Testing In Ruby) – Features, Installation, Pros & Cons | DS

Table of Contents

What Is Watir(Web Application Testing In Ruby)?

Watir is an open source tool developed using Ruby which helps in automating web apps. Browsers supported by Watir are IE, Firefox, Chrome, and Edge.

Features of Watir

  • Automatic capturing of screenshots when testing is done.
  • Page performance can be done using objects like performance. navigation, and performance.timing, performance. memory and performance. time origin. These can be obtained as connected to the browser.
  • It’s easy to test file downloads from the UI.
  • Easy to use APIs to test alerts and popups.

Watir Advantages

  • Open source and easy to use.
  • Inbuilt libraries to test page performance, alerts, iframe tests, browser windows, take screenshots, etc.
  • Supported by all the latest browsers.

Watir Disadvantages

  • Only supports the ruby test framework so canโ€™t be used with other testing frameworks.
  • Mobile automation is not enhanced.

Installation of Watir

  1. Download and install the latest version of the ruby installer from Downloads (rubyinstaller.org)
  2. Hit the command โ€˜ruby -vโ€™ to check the ruby version.
  3. Hit the command โ€˜gem -vโ€™ to check the gem version.
  4. Hit command โ€˜gem install watirโ€™ to install watir. If you face any issues with installation try running cmd in โ€˜run as administratorโ€™ mode and hit commands.
  5. Download and install ruby mine, it’s an IDE that helps write, debug, and execute code.

How to write a test in Watir?

  1. Open ruby mine and create a new file naming test demo.RB
  2. Write the below code to launch Chrome browser
require โ€˜watirโ€™

ย  browser = Watir::Browser.new :chrome

ย  browser.goto(โ€œhttps://www.google.comโ€)

Launch the Firefox browser

ย ย ย  require โ€˜watirโ€™

ย ย ย  browser = Watir::Browser.new :firefox

ย ย ย  browser.goto(โ€œhttps://www.google.comโ€)

To Launch the Edge browser

require โ€˜watirโ€™
browser = Watir::Browser.new :edge
browser.goto(โ€œhttps://www.google.comโ€)

To Launch the browser in headless chrome

ย  require โ€˜watirโ€™

ย  Browser = Watir::Browser.new :chrome, headless: true

ย  browser.goto(โ€œhttps://www.google.comโ€)

Capturing a screenshot at time of execution is an interesting feature of watir. This helps to track any issue that occurs at the time of test execution. Below is an example to capture screenshots

require โ€˜watirโ€™
    b = browser = Watir::Browser.new :chrome
    b.goto(โ€œhttps://www.google.comโ€)
    b = browser.screenshot.save 'google.png'

Screenshots will appear like this

Capture screenshot in Watir

For mobile testing, use a desktop browser that will act as a device browser for testing. To do mobile testing, we need to โ€˜install web driver-user-agentโ€™.

gem install web driver-user-agent

image of Install webdriver user agent

           require 'watir'
require 'webdriver-user-agent'
driver = Webdriver::UserAgent.driver(browser: :chrome, 
                                                            agent: :iphone, 
                                                            orientation:   :landscape)
browser = Watir::Browser.new driver
browser.goto 'https://facebook.com'
puts "#{browser.url}"
puts browser.url == 'https://m.facebook.com/'

Above code will launch the browser in landscape mode.

Landscape mode image

This will launch the browser in portrait mode

require 'watir'
require 'webdriver-user-agent'
driver = Webdriver::UserAgent.driver(browser: :chrome, 
                                                            agent: :iphone, 
                                                            orientation:   :portrait)
browser = Watir::Browser.new driver
browser.goto 'https://facebook.com'
puts "#{browser.url}"
puts browser.url == 'https://m.facebook.com/'

portrait mode image


Related Post:-

Related Blogs