-
Notifications
You must be signed in to change notification settings - Fork 45
FAQ
A good place to start is typically the Watir Documentation Page which links you to the Quick Start Guide, the rdoc (api), articles, and other resources.
Anther good way to understand how to use Watir and get examples is to take a look at the unit tests in Watirspec. There are tests for all of the features in Watir, which can serve as excellent documentation and a basic tutorial.
Best approach:
http://justinram.wordpress.com/2006/02/27/installing-watir-via-the-gem-utility/
Goto the cmd line
set HTTP_PROXY=[http://mycache:8080](http://mycache:8080)
Explained in more detail here.
gem install watir
If you are getting an error like:
Updating RubyGems...
Attempting remote update of rubygems-update
Install required dependency builder? [Yn] Y
ERROR: While executing gem ... (Gem::GemNotFoundException)
Could not find builder (>= 0) in any repository
What you need to do is manually install the latest version of rubygems. Download the zip file from here, extract it and run setup.rb.
Once you have done this the commands "gem install watir" and "gem update watir" will work correctly.
dir = File.dirname(__FILE__)
Dir[File.expand_path("#{dir}/*.rb")].uniq.each do |file|
require file
end
Take a look at Platforms page of Watir web site.
There are toolbars you can use in Internet Explorer and Firefox. See Browser Development Toolbar for instructions.
See Frames page.
See Regular Expressions page.
I have more than one object of the same name. How do I commit an action on anything but the first one?
See Multiple Attributes page.
Insert the object recognition into a small method
def login_link; $ie.link(:text, 'Log in'); end
def username_field; $ie.text_field(:name, 'userid'); end
then in your test class do:
login_link.click
username_field.set(username)
See IRB page.
Using Watir 1.5.6 or later, it is easy to extend Watir to support additional tags. Thus:
module Watir
class Abbr < NonControlElement
TAG = 'ABBR'
end
end
This article from Bret about how to create your own classes that access HTML elements on the page using the Container Module using previous versions of Watir.
http://www.io.com/~wazmo/blog/archives/2006_10.html#000242
Sometimes you need to wait for something to happen in the Application under test before you interact with it. Sleep statements are hardcoded and lock you down into a certain number of seconds before moving through your test. To avoid that, we've written a polling mechanism in the latest versions of Watir - the wait_until method.
An example might be that you're loading the Google home page and for some reason it's taking time to load. Here's a basic contrived script with a sleep statement.
require 'watir'
browser = Watir::IE.start('http://www.google.com')
sleep 5 # we need to wait for the page to load and on a subjective basis I've chosen 5 seconds which works on my machine
browser.text_field(:name, 'q').set('ruby poignant')
....
Unfortunately the sleep is hardcoded and doesn't work for anyone else on my team who have slower network connections, my connection has gotten faster, but it still waits for 5 seconds before setting the text field.
Watir 1.5.x has added a wait_until method that can poll for a certain condition to return true before continuing on or erroring out. By default it checks the condition every half second up until 60 seconds. So I rewrite my code to look like this:
require 'watir'
browser = Watir::IE.start('http://www.google.com')
Watir::Waiter.wait_until{ browser.text_field(:name, 'q').exists? } # in this case all I care about is the text field existing, you could check title, text, anything you're expecting before continuing
browser.text_field(:name, 'q')set('ruby poignant')
...
It now works for me with a half second delay, but also works for the other members of my team who have network delays up to a minute. If you're considering using sleep, use wait_until instead. It will make your test code more resilient to timing issues in those cases where you really need to use it.
See Pop Ups page.
See Security Alerts page.
See Tips and Tricks page.
As of development build 1.5.1.1100 a new IE.new_process method was added to 'watir/contrib/ie-new-process'. This starts up a new IE process for each IE window, which is really how it should be done. To close these use IE#kill. Any one getting intermittent RPC errors when opening windows may want to use this instead.
The following Example uses this method to create 5 IE process and add them to an array:
require 'watir/contrib/ie-new-process'
ies = []
5.times do
ie = Watir::IE.new_process
ies<<ie
end
This routine can be useful as part of setup or teardown. It attaches to, and closes, all open IE windows.
def close_all_windows
loop do
begin
Watir::IE.attach(:title, //).close
rescue Watir::Exception::NoMatchingWindowFoundException
break
rescue
retry
end
end
end
An enhanced version of this functionality is now included in Watir 1.5. (It also closes modal web dialogs.)
require 'watir/close_all'
Watir::IE.close_all
A final brute-force method (that works on Windows XP) might be to kill all iexplore.exe processes via a system command:
system("taskkill /t /f /im iexplore.exe")
When IE throws up a JavaScript pop-up window, it appears to hang my Ruby script. How can I make those windows go away?
See JavaScript Pop Ups page.
See JavaScript page.
See JavaScript page.
If you include asynchronous JS in your definition of done, you'll need to code around that specifically. There's an open feature request to have Watir's wait method track XHRs and timers that are launched when the page is loaded, and wait for them as well. That may be tricky to do though, and with an all-volunteer project, it really depends on someone making the time to do it.
In lieu of that, one option is having the application keep track if XHRs and timers that it kicks off, and setting the some value to true when they are all complete. For example:
def wait_until_loaded(timeout = 30)
start_time = Time.now
until (however_your_application_reports_its_loaded == 'true') do
sleep 0.1
if Time.now - start_time > timeout
raise RuntimeError, "Timed out after #{timeout} seconds"
end
end
end
A simpler option is:
tries = 0
until browser.link(:text, /link_to_wait_for/).exists? do
sleep 0.5
tries += 1
end
browser.link(:text, /link_to_wait_for/).click
Another option is to retry until timeout or no exception is raised.
def rescue_wait_retry(exception = Watir::Exception::UnknownObjectException, times = 10, seconds = 2, %block)
begin
return yield
rescue exception => e
puts "Caught #{exception}: #{e}. Sleeping #{seconds} seconds." if $DEBUG
sleep(seconds)
@ie.wait
if (times -= 1) > 0
puts "Retrying... #{times} times left" if $DEBUG
retry
end
end
yield
end
@ie.link(:url, %r|confirmdelete\.action\?educationId|).click #This starts some ajax stuff
rescue_wait_retry { @ie.button(:id, 'form_0').click }
See the Right Click an Element page.
See the Save All Images on a Webpage page.
See Test Unit page.
If your tests inherit from TestCase or RSpec, take a look at Nick Sieger's project ci_reporter which can run from a Rakefile and collect your results in xml that can be picked up by projects that understand xunit xml reports, such as Cruise Control. Cruise Control is a great continuous integration server with versions for Ruby, Java and .Net and can provide a nice dashboard and runner for your tests.
Here's an extremely simple example using test::unit and ci_reporter. For more examples refer to the information for ci_reporter on Nick's site.
Example:
require 'test/unit'
require 'ci/reporter/rake/test_unit_loader.rb'
require 'watir'
class My_Test < Test::Unit::TestCase
def test_me
browser = Watir::IE.start('http://www.google.com')
assert(browser.link(:text, 'About Google').exists?)
browser.close
end
end
Note: Report files are written, by default, to the test/reports or spec/reports subdirectory of your project. If you wish to customize the location, simply set the environment variable CI_REPORTS (either in the environment, on the Rake command line, or in your Rakefile) to the location where they should go.
See Data-Driven Tests page.
See Data-Driven Tests page.
Try one of these:
- Add your defaut homepage (or 'About:Blank' if you start with a blank page) to the same security group (e.g. 'intranet' or 'trusted sites') as the site you are testing; or
- Turn off Internet Explorer Protected Mode; or
- Change your ruby permissions to "run as administrator"; or
- Disable User Access Control
See Tips and Tricks page.
see Building Watir
Visit Watir Search, Google custom search engine that searches Watir web sites, wiki, bug tracker, source and all Watir related mailing lists (and nothing else).
- go to http://jira.openqa.org/browse/WTR
- log in
- click link CREATE NEW ISSUE (at orange bar near the top of the page)
- you will need openqa.org account to log in
- if you do not have one, you can create it at http://www.openqa.org/sass/index.action
require 'watir'
require 'win32ole'
@@word=WIN32OLE.new('Word.Application')
@@word.Documents.Add()
def take_a_screenshot(url)
@autoit = WIN32OLE.new("AutoItX3.Control")
browser = Watir::IE.new
browser.bring_to_front
browser.goto(url)
browser.maximize
@autoit.Send("{PRINTSCREEN}")
browser.close
@@word.Selection.Paste
@autoit.Send("{ENTER}")
end
def save_file
@@word.ActiveDocument.SaveAs('C:\screenshots.doc')
@@word.ActiveDocument.close
@@word.Quit
end
take_a_screenshot('http://www.agiletester.co.uk')
take_a_screenshot('http://www.fsf.org/')
save_file
require 'fileutils'
class Cookies
def delete(dir= "C:\Documents and Settings\#{ENV['USERNAME']}\Cookies")
# or wherever your cookies are downloaded to (can be browser specific)
FileUtils.rm_rf dir
end
end
See How do I use variables with regular expressions? page.
In some cases, after you install Watir, the .dll file fails to get registered which gives an error similar to the following:
Exception 'WIN32OLERuntimeError' at <filename.rb>:<line#> - unknown OLE server: 'AutoItX3.Control'
If this is your problem, then all you need to do is type the following where your AutoItX3.dll file is installed (if you are using Watir's version of AutoIt then on windows for 1.5.6 it should be at: c:\ruby\lib\ruby\gems\1.8\gems\watir-1.5.6\watir, for 1.6.2 it should be at: C:\ruby\lib\ruby\gems\1.8\gems\watir-1.6.2\lib\watir )
regsvr32 AutoItX3.dll
Following this you will see a pop-up window saying: "DllRegisterServer in autoitx3.dll succeeded."
Most of the time, if you are require'ing 'watir/contrib/enabled_popup', you will get the following error:
NameError: uninitialized constant Watir::PageContainer::Win32
from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/contrib/enabled_popup.rb:5
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from (irb):2
However, in 1.6.2, you will need to add "require 'watir/ie'" after you require 'watir' to fix this error message. This issue has been fixed in the git version of watir and will be fixed in gems subsequent to 1.6.2.
For more info see this thread:
http://groups.google.com/group/watir-general/browse_thread/thread/f3d3d8f6023890cb/016c62b5a5ff6fb6