Examples

All examples are designed to work on the live Watir demo form: bit.ly/watir-example.

Loading RubyGems

If you’re a first time Ruby user, you need to understand how to load Ruby gems such as Watir. You can require it via the -rubygems command line option or by using the RUBYOPT environment variable. You can also require it manually in your script:

require 'rubygems'

For more information see here.

Including Watir gem to drive Internet Explorer on Windows

require 'watir'

Including Watir-WebDriver gem to drive Firefox/Chrome on Windows/Mac/Linux

require 'watir-webdriver'

Starting a new browser & and going to our site

browser = Watir::Browser.new
browser.goto 'bit.ly/watir-example'

Setting a text field

spacer
browser.text_field(:name => 'entry.0.single').set 'Watir'

Setting a multi-line text box

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

Setting and clearing a radio button

spacer
browser.radio(:value => 'Watir').set
browser.radio(:value => 'Watir').clear

Setting and clearing check boxes

spacer
browser.checkbox(:value => 'Ruby').set
browser.checkbox(:value => 'Python').set
browser.checkbox(:value => 'Python').clear

Clicking a button

browser.button(:name => 'logon').click

Clearing, getting and selecting selection list values

spacer
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'

Clicking a button

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

Checking for text in a page

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

Checking the title of a page

puts browser.title == 'Thanks!'

There are several examples from the Watir community.

Rate this:

Share this:

  • Email
  • Print
  • Twitter
  • StumbleUpon
  • Reddit
  • Digg
  • Facebook