Created
April 28, 2012 04:02
-
-
Save carlosmcevilly/2515656 to your computer and use it in GitHub Desktop.
watir notes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
My notes on using watir. This is from 2007 so it's a bit out of date. | |
The part starting with "Cheat Sheet" is cribbed from some long-forgotten web site. | |
Watir can be used with other browsers but this is Firefox-specific. | |
1. install Firefox 1.5 or higher | |
2. install jssh extension | |
3. close firefox | |
4. make a new profile for firefox named 'watir': | |
/Applications/Firefox.app/Contents/MacOS/firefox-bin -ProfileManager | |
5. close firefox | |
6. start Firefox from the command line: | |
/Applications/Firefox.app/Contents/MacOS/firefox-bin -P watir -jssh | |
(or ./start-firefox-watir.csh) | |
7. Check if JSSh is listening on port 9997 with: | |
telnet localhost 9997 | |
8. install firewatir ruby gem: | |
sudo gem install firewatir-1.1.gem | |
9. in /opt/local/lib/ruby/gems/1.8/gems/firewatir-1.1/unittests do: | |
ruby mozilla_all_tests.rb | |
(but do make sure firefox has already been started manually as above) | |
(this is hanging up atm on my machine) | |
10. if "attach_new_browser" test fails: | |
a. make sure that pop ups are allowed in Firefox. To allow the pop up | |
run this test alone and then allow the pop ups | |
b. make sure that Firefox settings should be open new window instead | |
of new tab when link is clicked | |
11. to get back to normal Firefox: | |
./start-firefox-normal.csh | |
12. play with it by hand: | |
$ irb | |
irb(main):001:0> require 'rubygems' | |
=> true | |
irb(main):002:0> require 'firewatir' | |
=> true | |
irb(main):003:0> include FireWatir | |
=> Object | |
irb(main):004:0> ff=Firefox.new | |
=> # | |
irb(main):005:0> ff.goto("http://mail.yahoo.com") | |
Cheat Sheet | |
=========== | |
Basics | |
------ | |
Load the Watir library | |
require 'watir' | |
some sites also say the following. may depend on platform: | |
include Watir | |
Open Internet Explorer at the specified URL | |
$browser = Watir::IE.start("http://google.com") | |
$browser = Watir::IE.start "http://google.com" | |
Attach to an existing browser, raising an exception if it isn't found | |
$browser = Watir::IE.attach(:url, "http://www.google.com") | |
$browser = Watir::IE.attach(:title, "Google") | |
Attach to an existing browser, returning nil if it isn't found | |
$browser = Watir::IE.find(:title, "Google") | |
$browser = Watir::IE.find(:url, "http://www.google.com") | |
Speed up execution (or use the "-b" command line switch) | |
$browser.speed = :fast | |
Close the browser | |
$browser.close | |
Access an Element | |
----------------- | |
Text box or text area | |
t = $browser.text_field(:name, "username") | |
Button | |
b = $browser.button(:value, "Click Here") | |
Drop down list | |
d = $browser.select_list(:name, "month") | |
Check box | |
c = $browser.checkbox(:name, "enabled") | |
Radio button | |
r = $browser.radio(:name, "payment type") | |
Form | |
f = $browser.form(:name, "address") | |
f = $browser.form(:action, "submit") | |
Link | |
l = $browser.link(:url, "http://google.com") | |
Table cell in a table (2nd row, 1st column) | |
td = $browser.table(:name, 'recent_records')[2][1] | |
Manipulate the Element | |
---------------------- | |
Manipulate the Element | |
Click a button or link | |
b.click | |
l.click | |
Enter text in a text box | |
t.set("mickey mouse") | |
t.set "mickey mouse" | |
Enter multiple lines in a multi-line text box | |
t.set("line 1\nline2") | |
t.set "line 1\nline2" | |
Set radio button or check box | |
c.set | |
r.set | |
Clear an element | |
t.clear | |
c.clear | |
r.clear | |
Select an option in a drop down list | |
d.select("Hey!") | |
d.select "Hey!" | |
Clear a drop down list | |
d.clearSelection | |
Submit a form | |
f.submit | |
Flash any element (useful from the watir-console) | |
e.flash | |
Check the Contents | |
------------------ | |
Return the html of the page or any element | |
$browser.html | |
e.html | |
Return the text of the page or any element | |
$browser.text | |
e.text | |
Return the title of the document | |
$browser.title | |
Return true if the specified text appears on the page | |
$browser.text.include? 'llama' | |
Return the contents of a table as an array | |
$browser.table(:id, 'recent_records').to_a | |
Use XPath to Select Elements | |
---------------------------- | |
Click the button with an image called 7.jpg | |
browser.button(:xpath, "//img[@src='7.jpg']/input").click() | |
Click on an area that links to signup.html | |
browser.element_by_xpath("//area[contains(@href , 'signup.htm')]").click() | |
Select based on the name (foo) and value (bar) of a non-standard | |
attribute, e.g. <select foo="bar"> | |
element = browser.select(:xpath, "//select[@foo='bar']") | |
For xpath help, install XPather from: | |
http://xpath.alephzarro.com/index | |
or the .xpi for version 1.3 is in this directory. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment