All Watir-WebDriver

Watir-WebDriver Cheat Sheet

Browser

# start new driver session
b = Watir::Browser.new :firefox
b = Watir::Browser.new :chrome
b = Watir::Browser.new :ie
 
# goto url
b.goto "http://relevantcodes.com"
 
# refresh
b.refresh
 
# close
b.quit

Textbox

# enter value
b.text_field(:id => "text").set "watir-webdriver"
 
# get value
b.text_field(:id => "text").value
 
# clear
b.text_field(:id => "text").clear

Button

# is enabled?
b.button(:id => "btn").enabled?
 
# button's text
b.button(:id => "btn").text
 
# click
b.button(:id => "btn").click

Checkbox

# check
b.checkbox(:id => "btn").set
b.checkbox(:id => "btn").set(true)
 
# uncheck
b.checkbox(:id => "btn").clear
b.checkbox(:id => "btn").set(false)
 
# is checked?
b.checkbox(:id => "btn").set?

Listbox

# select from list text
b.select_list(:id => "list").select "var"
 
# select using value
b.select_list(:id => "list").select_value "var2"
 
# value is selected?
b.select_list(:id => "list").selected?("var2")
 
# get value
puts b.select_list(:id => "list").value
 
# get all items
b.select_list(:id => "list").options.each do |i|
  puts "#{i.text}"
end

Radio

# select value
b.radio(:id => "radio").set
 
# is var selected?
b.radio(:id => "radio").set?

Image

# is image loaded?
b.image(:src => "img.gif").loaded?
 
# height
b.image(:src => "img.gif").height
 
# width
b.image(:src => "img.gif").width
 
# click
b.image(:src => "img.gif").click
# click 1st image
b.images[0].click

Div

# get text
b.div(:class => "body").text
 
# get text of 2nd div when it appears
b.divs[1].when_present.text

Table

# row 1, col 1
b.table(:id => "table")[0][0].text
# row 1, col 2 (alternate)
b.table(:id => "table").tr{0}.cell{1}.text
 
# row 2 - entire text
puts b.table(:id => "table")[1].text
 
# click row #4
puts b.table(:id => "table")[3].click
 
# get column count
b.table(:id => "table").row.cells.length
 
# row count
b.table(:id => "table").row_count
b.table(:id => "table").rows.length

General

# [exists?]
b.text_field(:id => "text").exists?
 
# [enabled?]
b.select_list(:id => "list").enabled?
 
# [present?]
b.element(:id => "e").present?
 
# [tag_name]
b.element(:id => "e").tag_name
 
# [screenshot]
b.screenshot.save "c:\\page.png"
 
# [to_subtype] # returns button
b.element(:id => "btn").to_subtype 
 
# [index] click 2nd image on page
b.image(:index => 1).click
 
# [loops]
# get names of all text-fields
b.text_fields.each do |i|
  puts i.name
end
 
# get name of first text-field
puts b.text_fields[0].name
# get name of second text-field
puts b.text_fields[1].name

Waiting

# [wait_until_present]
b.button(:id => "btn").wait_until_present
 
# [when_present]
b.button(:id => "btn").when_present.click
b.button(:id => "btn").when_present(10).click
 
# [wait_while_present]
b.button(:value => "submit").click
b.button(:value => "submit").wait_while_present
 
# [implicit wait] 5 seconds
# good to have, but i don't recommend its global use
b.driver.manage.timeouts.implicit_wait = 5

13 comments… add one

Leave a Comment

  • Venkat September 21, 2015, 10:18 pm

    May I know how(syntax) to click on a submenu item from menu….

    Like go to watir.com and then click on Frameworks under Documentation… (here we actually don’t click on Documentation.. we just mouse over it and then submenu is displayed then we click on Frameworks…)

    • Nur November 12, 2015, 3:04 pm

      maybe you try like this : b.image(:Frameworks, :src => “img.gif”).click

  • suman August 3, 2015, 12:06 am

    Thanks Anshoo, earlier I used to follow for QTP now I extended it for ruby too :)

  • chan June 18, 2014, 12:24 am

    Really good and simple summary of watir.

  • Robert November 20, 2013, 1:50 pm

    Looks promising. Need to see how many different browsers it can support. We have a need for some cross browser testing and it would be nice to not have to write several versions of the same script. Also, how well does it work against XAML?

  • Me October 16, 2013, 12:03 am

    b.button(:id => “btn”).when_until_present

    should be

    b.button(:id => “btn”).wait_until_present

  • Sameswar August 12, 2013, 8:38 am

    Hi Anshoo,
    Great job And its awesome stuff.
    Can i get this API?

    Thanks,
    Sameswar

  • Shreejit August 4, 2013, 11:40 am

    Wowwww awesome stuff
    Anshoo, how easy is it to pick up and float with ruby??? (for heavy qtp background :))

    • Anshoo Arora August 10, 2013, 4:04 am

      Shreejit, Ruby is quite easy to learn and isn’t any much more complex than VBScript. Its a much better and a lot more powerful language though.

  • Vinoth July 26, 2013, 10:33 pm

    Looks awesome Anshoo…Thanks for sharing
    I want to learn Ruby ASAP for this…

    • Anshoo Arora August 10, 2013, 4:04 am

      Vinoth/Shreejit, when you install Ruby, you will see in the installation folder “The Book of Ruby” – a great read.