Watir Automated Testing

Watir (Web Application Testing in Ruby), pronounced water, is an open-source family of Ruby libraries for automating web browsers. It allows you to write tests that are easy to read and maintain. It is simple and flexible.

Watir drives browsers the same way people do. It clicks links, fills in forms, presses buttons. Watir also checks results, such as whether expected text appears on the page.

Watir is a family of Ruby libraries but it supports your app no matter what technology it is developed in. Whilst Watir supports only Internet Explorer on Windows, Watir-WebDriver supports Chrome, Firefox, Internet Explorer, Opera and also running in headless mode (HTMLUnit).

Like other programming languages, Ruby gives you the power to connect to databases, read data files and spreadsheets, export XML, and structure your code as reusable libraries. Unlike other programming languages, Ruby is concise and often a joy to read.

A. Install

  1. Install Watir (Watir Classic)

    # require system installed ruby and rubygems
    # gem update --system --no-rdoc --no-ri
    gem install watir --no-rdoc --no-ri
    
  2. Install Watir WebDriver

    # ruby 1.9.3
    gem install watir-webdriver
    

    Example:

    require 'watir-webdriver'
    b = Watir::Browser.new
    b.goto 'bit.ly/watir-webdriver-demo'
    b.text_field(:id => 'entry_0').set 'your name'
    b.select_list(:id => 'entry_1').select 'Ruby'
    b.select_list(:id => 'entry_1').selected? 'Ruby'
    b.button(:name => 'submit').click
    b.text.include? 'Thank you'
    
  3. Chrome

    a. ChromeDriver

    Chrome support is through the platform specific ChromeDriver binary, which you download and put on your path.

    b = Watir::Browser.new :chrome
    

    b. Chrome Profiles

    profile = Selenium::WebDriver::Chrome::Profile.new
    profile['download.prompt_for_download'] = false
    profile['download.default_directory'] = "/path/to/dir"
    # create ChromeDriver with profile
    b = Watir::Browser.new :chrome, :profile => profile
    

    See the full list of profile options here.

    c. Chrome Switches

    b = Watir::Browser.new :chrome, :switches => %w[--ignore-certificate-errors --disable-popup-blocking --disable-translate]
    

    See the full list of switches here.

    d. Using a proxy with Chrome

    b = Watir::Browser.new :chrome, :switches => %w[--proxy-server=myproxy.com:8080]
    

B. Use

  1. Loading RubyGems

    require 'rubygems'
    
  2. Including Watir gem to drive Internet Explorer on Windows

    require 'watir'
    
  3. Including Watir-WebDriver gem to drive Firefox/Chrome on Windows/Mac/Linux

    require 'watir-webdriver'
    
  4. Starting a new browser & and going to website

    browser = Watir::Browser.new # :chrome
    browser.goto 'http://bit.ly/watir-example'
    
  5. Selector

    # by id
    browser.text_field(:id => 'A')
    # by name
    browser.text_field(:name => 'A')
    # by class
    browser.button(:class_name => 'A')
    # using regular expression
    browser.buttons(:id => /btnAdd/)
    browser.text_fields(:name, /prefix_\d+/)
    # parent item
    browser.label(:text => 'Text1').parent
    browser.p(:class_name => 'A', :text => 'Text1').parent
    browser.ps(:class_name, 'A').collect(&:text).index('Text1')
    # nest item
    browser.p(:id, 'A').text_field
    # duplicate element
    browser.p(:id, 'A').text_field(:index=>2) # third text field
    browser.ps(:id, /prefix_\d+/).first
    browser.ps(:id, /prefix_\d+/).last
    
  6. Text Fields

    browser.text_field(:name => 'entry.0.single').set 'Watir'
    browser.text_field(:name => 'entry.1.single').set "I come here from Australia. \n The weather is great here."
    
  7. Radios

    browser.radio(:value => 'Watir').set
    browser.radio(:value => 'Watir').clear
    browser.radio(:name => 'entry.8.group', :value => '5').set
    
  8. Checkboxes

    browser.checkbox(:value => 'Ruby').set
    browser.checkbox(:value => 'Python').set
    browser.checkbox(:value => 'Python').clear
    
  9. Select Lists - Combos

    browser.select_list(:name => 'entry.6.single').clear
    puts browser.select_list(:name => 'entry.6.single').options
    browser.select_list(:name => 'entry.6.single').select 'Chrome'
    
  10. Buttons

    browser.button(:name => 'submit').click
    
  11. Links

    browser.link(:text => 'Google Docs')
    
  12. ps & Spans

    browser.p(:class_name => 'ss-form-desc ss-no-ignore-whitespace')
    browser.span(:class_name => 'ss-powered-by')
    
  13. Popups

    browser.window(:title => "annoying popup").use do
      browser.button(:id => "close").click
    end
    
  14. JavaScript Dialogs

    # JAVASCRIPT ALERT
    # Check if alert is shown
    browser.alert.exists?
    # Get text of alert
    browser.alert.text
    # Close alert
    browser.alert.ok
    browser.alert.close
    # JAVASCRIPT CONFIRMS
    # Accept confirm
    browser.alert.ok
    # Cancel confirm
    browser.alert.close
    # JAVASCRIPT PROMPT
    # Enter text to prompt
    browser.alert.set "Prompt answer"
    # Accept prompt
    browser.alert.ok
    # Cancel prompt
    browser.alert.close
    

    If you're having trouble using the above method, you can override the JavaScript functions to return the value you want, so when they're meant to show, they don't!

    # don't return anything for alert
    browser.execute_script("window.alert = function() {}")
    # return some string for prompt to simulate user entering it
    browser.execute_script("window.prompt = function() {return 'my name'}")
    # return null for prompt to simulate clicking Cancel
    browser.execute_script("window.prompt = function() {return null}")
    # return true for confirm to simulate clicking OK
    browser.execute_script("window.confirm = function() {return true}")
    # return false for confirm to simulate clicking Cancel
    browser.execute_script("window.confirm = function() {return false}")
    # don't return anything for leave page popup
    browser.execute_script("window.onbeforeunload = null")
    
  15. Checking for text in a page

    puts browser.text.include? 'Your response has been recorded.'
    
  16. Checking the title of a page

    puts browser.title == 'Thanks!'
    
  17. Measure Page Performance

    puts browser.text.include? 'Your response has been recorded.'
    

C. Examples

# encoding: UTF-8
require 'rubygems'
require 'watir'
browser = Watir::Browser.new
browser.goto 'http://bit.ly/watir-example'
browser.text_field(:name => 'entry.0.single').set 'Watir'
browser.text_field(:name => 'entry.1.single').set "I come here from Australia. \n The weather is great here."
browser.radio(:value => 'Watir').set
# browser.radio(:value => 'Watir').clear
browser.checkbox(:value => 'Ruby').set
browser.checkbox(:value => 'Python').set
browser.checkbox(:value => 'Python').clear
# browser.select_list(:name => 'entry.6.single').clear
puts browser.select_list(:name => 'entry.6.single').options
browser.select_list(:name => 'entry.6.single').select 'Chrome'
browser.radio(:name => 'entry.8.group', :value => '5').set
browser.radio(:name => 'entry.11.group', :value => '5').set
browser.radio(:name => 'entry.12.group', :value => '5').set
browser.button(:name => 'submit').click
puts browser.text.include? 'Your response has been recorded.'
puts browser.title == 'Thanks!'

D. References

Comments

Popular posts from this blog

Reduce TIME_WAIT Socket Connections