This was initially written in 2016, updates may apply now.
What Is Watir
Watir (pronounced water) stands for Web Application Testing in Ruby.Major properties:
- Open source (both Ruby and Watir).
- Able to drive web browsers including Microsoft Internet Explorer, Mozilla Firefox, Apple Safari, Google Chrome, and Opera.
- Works on all major operating systems: Microsoft Windows, Apple Mac OS X and Linux.
- Full access to Ruby features (Watir itself is just a family of Ruby gems, i.e. Watir code is in fact Ruby code).
- Able to access files, databases, emails, etc.
Watir can do almost anything that you can manually do with a browser: open a page, click a link, button, radio button, check-box, enter text in a text field… It can also inspect a currently opened page in the browser: tell you the URL from the address bar, page title, list all links from the page, search for text...
What can it not do?
Watir can NOT control browser plugins like Java applets, Adobe Flash, or Microsoft Silverlight.
Installation
There are 4 basic steps (this document based on a PC with Windows 7 Pro).1. Install Ruby
- Go to the RubyInstaller download page: https://rubyinstaller.org/downloads/
- Download and install a Ruby version of your choice.
- During installation, make sure to check "Add Ruby executable to your PATH" and "Associate .rb and .rbw files with this Ruby installation" options.
- DevKit can be downloaded from the same place mentioned above, make sure to match the installed Ruby version (details can be found at the web page).
- The downloaded executable is just a self-extracting archive that can be extracted anywhere. It is recommended to use the location where Ruby is installed.
- E.g., if Ruby is installed at C:\Ruby, extract DevKit to C:\Ruby\devkit.
- Open a Command window, navigate to where DevKit is extracted and run the following:
cd c:\Ruby\devkit3. Install the required gems
ruby dk.rb install
ruby dk.rb init
Open a Command window, run the following:
gem update --systemIf you encounter the following error
gem install --no-ri --no-rdoc rspec
gem install --no-ri --no-rdoc win32console
gem install --no-ri --no-rdoc watir
ERROR: While executing gem ... (Gem::RemoteFetcher::FetchError)Do the following then re-run the gem update/install commands:
SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed ...
gem source -r https://rubygems.org/If the following error is seen when trying to add a source to an empty collection:
gem source -a http://rubygems.org/
gem update --system
gem source -r http://rubygems.org/
gem source -a https://rubygems.org/
C:\Users\xyin>gem sources -l
*** CURRENT SOURCES
C:\Users\xyin>gem sources -a https://rubygems.orgTry this if a proxy is used:
Error fetching https://rubygems.org:
timed out (https://rubygems.org/specs.4.8.gz)
C:\Users\xyin>gem sources -a https://rubygems.org --http-proxy=http://10.4.149.51:80804. Install ChromeDriver
https://rubygems.org added to sources
C:\Users\xyin>gem sources
*** CURRENT SOURCES ***
https://rubygems.org
- Download file "chromedriver_win32.zip" from https://sites.google.com/a/chromium.org/chromedriver/downloads
- Make sure to use the correct version of ChromeDriver to match the Chrome browser in use. Supporting info is included for Chrome browser v60 and above.
- Unzip the above file to get file chromedriver.exe, do not run it, but place it somewhere under your PATH, e.g. under the bin directory where you installed Ruby.
- To get the version of a chromedriver.exe file, run the exe from a command window like this:
Chrome Version: 44.0.2403.125
ruby 2.2.6p396 (2016-11-15 revision 56800) [i386-mingw32]
ChromeDriver 2.16.333243 (0bfa1d3575fc1044244f21ddb82bf870944ef961)
*** LOCAL GEMS ***
appium_console (2.0.1)
appium_lib (9.3.3)
awesome_print (1.7.0)
bigdecimal (default: 1.2.6)
bond (0.5.1)
childprocess (0.6.1)
chronic_duration (0.10.6)
coderay (1.1.1)
ffi (1.9.17 x86-mingw32)
io-console (default: 0.4.3)
json (default: 1.8.1)
method_source (0.8.2)
mini_portile2 (2.1.0)
minitest (5.4.3)
nokogiri (1.7.0.1 x86-mingw32)
numerizer (0.1.1)
power_assert (0.2.2)
pry (0.10.4)
psych (default: 2.0.8)
rake (default: 10.4.2)
rautomation (0.17.0)
rdoc (default: 4.2.0)
rubygems-update (2.6.10)
rubyzip (1.2.1)
selenium-webdriver (3.0.4, 3.0.3)
slop (3.6.0)
spec (5.3.4)
test-unit (3.0.8)
thor (0.19.4)
tomlrb (1.2.3)
watir (6.1.0)
websocket (1.2.4)
win32console (1.3.2 x86-mingw32)
yard (0.9.8)
Testing and Debugging -- irb or pry
irb is a command window based Interactive Ruby Shell that can be used to develop and debug Ruby scripts.The following example shows the steps of opening a browser, navigating to XXX Instant Messenger login page, entering user ID and password, and clicking the login button.

Another debugging tool is "pry", a gem that needs to be installed first (gem install pry). It has nice colored coding:

Chrome Browser Web Element Exploration
Chrome's Developer Tool can be used to find web page elements' attributes (class, id, text, etc.), similar to the Object Spy tool of QTP/UFT.Run Ruby Test Case
Open a command window and run:ruby <testcase>.rb
Scripting Notes
Using regexp in element's attribute valueExample:
Instead of using:
table(:class => "idp-grid-table table table-condensed")Use this if the attribute value is too long (or contains some special characters):
table(:class => /idp-grid-table/)Inserting string variable in regexp
If a variable is included in a regexp expression, use it like this:
/#{var_name}/Getting element class nameTo obtain the class name of an element:
<element>.class_nameGetting element colorTo obtain color code of an element:
<element>.style 'background-color'Dealing with multiple windows originated from the same browserWhen a new window is opened from the original one, extra steps are needed to identify elements within different windows.
Examples:
To obtain number of total windows: browser.windows.length
To get title of the second window: browser.window(:index => 1).title
To click a button in the second window:
browser.window(:index => 1).use doDisabling notification popups from browser
browser.button(:class => "submit").click
end
Open browser like this:
browser = Watir::Browser.new :chrome, :switches => %w[--disable-notifications]Disabling pop-up blocking (i.e. always allowing pop-ups) within browserOpen browser like this:
browser = Watir::Browser.new :chrome, :switches => %w[--disable-popup-blocking]


No comments:
Post a Comment