Introduction To Watir
Introduction To Watir
Introduction To Watir
What is WATIR?
y Web Application Testing In Ruby y It is a library for the Ruby language which drives Internet Explorer the
same way people do; clicks links, fills in forms, and presses buttons. y Watir can also check results, such as whether expected text appears on the page. y It can be used to test all types of web applications (ASP.Net, JSP, PHP, Rails, etc) y Open Source written by Bret Pettichord, Paul Rogers and many other contributors.
However, there are several recorders out there WatirMaker Watir WebRecorder Webmetrics RIA Script Recorder (most recent discussionthey are considering open sourcing their application) y Watir is not a link checker However, you can easily write your own link checker and customize it to your specific needs. y Watir is not a test case management tool. However, you can write one in Ruby if desired. y Doesnt test Flash or Applets.
What is Ruby?
y Full featured Object Oriented scripting language o Made famous for its web application framework Rails. (Ruby on Rails) y Interpreted rather than compiled y Written by Matz (Yukihiro Matsumoto) Started in 1994 y Written in C Will work on any platform that has a C compiler
Windows Linux
y Uses the COM interface of Internet Explorer (IE) a.k.a ActiveX or OLE y Allows an external program to control IE Similar interfaces exist for Word, Excel, PowerPoint and Outlook. y Full access to the contents of an HTML page y Provides different ways to access objects
You dont need to be a professional developer to program your watir scripts. And yetYou are learning a robust programming language not just another tool.
(You have the power to connect to databases, read data files, export XML, structure your code into reusable libraries, and pretty much anything else you can think of)
y No Vendor-script
y Its simple elegant INTUITIVE y It has a supportive online community for when you get stuck.
Setting up WATIR
http://rubyforge.org/frs/?group_id=167
2nd:
Install Watir:
Packaged as a gem. Gem = A Ruby library that can be installed over the internet. Current Watir gem version contains Watir 1.5.[2] To install SIMPLY type: gem install watir
http://www.microsoft.com/downloads/details .aspx?FamilyID=e59c3964-672d-4511-bb3e2d5e1db91038&DisplayLang=en
ScITE (Free)
Use it to:
Attach to IE windows and quickly identify browser objects Run quick experiments to see if things will work in your tests
y y y
My
Demo: Show_all_objects
view)
Use Watir
y Using the Watir API is very easy. y Reference the Watir API using the require
y Web pages are developed with objects: Links, buttons, tables, drop-down boxes, forms, frames, etc. y Watir scripts need to access these objects &
View Source
y View Source
Use IRB
IRB can be used to identify objects on the page. In the example below, I launched http://www.godaddy.com and flashed the first two links one at a time.
Small Scripts
Require watir url = http://www.godaddy.com $ie = Watir::IE.start(url) $ie.bring_to_front $ie.tables.each { |t| puts t.to_s} $ie.tables[1].to_s $ie.tables.length
#iterate through all the tables on the page #goto the first table on the page #show how many tables are on the page. Tables that are nested will be included
IE Developer Tool
y A tool for exploring and understanding web pages. y Locates and selects specific elements on a web page
Manipulating Objects
Web browser view: GoDaddy Watir code: browser.link(:text, GoDaddy).click -OR browser.link(:url, http:www.godaddy.com).click HTML source: <a href=http://www.godaddy.com/>GoDaddy</a>
Web browser view: Check Me: Watir code: browser.checkbox(:name, checkme).set -OR browser.checkbox(:name, checkme).clear HTML source: <input type = checkbox name = checkme value = 1>
Web browser view: Click Me: Watir code: browser.radio(:name, clickme).set -OR browser.radio(:name, clickme).clear HTML source: <input type = radio name = clickme id = 1>
HTML source: <select name = "selectme" > <option name=1> <option name=2>Web Testing <option name=3>in Ruby <option name=4>is fun </select>
Web br
ser ie :
Watir c de:
br br ser.text_field(:name, typeinme).set(Life is g ser.text_field(:name, typeinme).clear d) -OR-
HTML s urce:
<input type = "text" name = "typeinme" >
HTML source:
<input type = "button" name = "clickme" value = "Click Me">
browser.button(:id, xxx).click
browser.button(:name, xxx).click
browser.button(:value, xxx).click
browser.button(:text, xxx).click
:id
browser.button(:caption, xxx).click
:name
browser.button(:index, x).click
:value
browser.button(:class, xxx).click
:text
browser.button(:xpath, //img[@src=xxx]/input ).click
:caption
:index
:class
:xpath
has to identify a button y A developer might only use 1 3 elements in his code. y And you, as a watir scripter can only use 1 element maybe 2 to describe your desired object.
y Suppose you had 3 buttons on your web page all with the
text: Click Here. Using the above statement, watir will access the first button with the text: Click Here. What if you wanted the 3rd? y You can use multiple identifiers on some methods:
browser.link(:text => Click Here , :index => 3).click
Method Examples
consuming part of creating your test scripts y However, after your objects have been identified & manipulated: you want to Test them! y Youll want to create PASS or FAIL scenarios. This is the most sophisticated part of your scripts.
Test::Unit
y Test::Unit is a library of Ruby (just like Watir)
It is not technically part of Watirhowever it is used regularly to structure tests. To use Test::Unit in your scripts you require it just as you do watir require test/unit require watir
y Test::Unit is a way to organize your code into tests y Test::Unit has built in methods called assertions that
assert(browser.link(:text, Click Here).exists?) The above statement will return a TRUE or FALSE indicating a pass or fail in your test.
Test::UnitA Failure
Returns results such as these: >ruby opf_smoketest.rb Loaded suite opf_smoketest Started F Finished in 51.516 seconds. 1) Failure: test_smokeTest(TC_manage_accounts) [opf_smoketest.rb:35:in `create_gallery' opf_smoketest.rb:398:in `test_smokeTest']: <"http://app.onlinephotofiler.com/AddGallery.aspx"> expected to be =~ </app.onlinephotofiler.com\/GalleryThumbnails/>. 1 tests, 1 assertions, 1 failures, 0 errors >Exit code: 0
Test::UnitA Pass
Returns results such as these: Loaded suite opf_smoketest Started 01. Create Gallery 02. Add Photos 03. Edit Photos 04. Create Badge 05. Save Badge 06. Version Number 07. Reorder Galleries 08. Reorder Images 09. Edit Tags 10. Edit Title & Description 11. Create Permalinks 12. PhotoStore Make Purchase Finished in 225.701 seconds. 1 tests, 29 assertions, 0 failures, 0 errors >Exit code: 0
PASS PASS PASS PASS PASS PASS PASS PASS PASS PASS PASS PASS
Test::Unit Assertions
Its important to understand that Test::Unit will execute your methods in alphabetical/numerical order! Also, the setup and teardown methods will wrap around every test. (Every methods starting with test.
They will execute in this order (every test method is wrapped with the setup and teardown methods)
def setup def test_01 def teardown def setup def test_02 def teardown def setup def test_03 def teardown
def test_all_methods_within_one_launch_of_the_browser_and_in_this_order 01 02 03
Windows Pop-Ups
y Sometimes when a user is using a web page a pop-up
window will appear. These require special attention in watir. y Pop-Up examples:
Security Alerts Choose File pop-ups Save As Login (username/password) panels Alert boxes Script prompt/textbox Confirmation Boxes (ok/cancel)
.attach method:
AutoIt
y AutoIt is a scripting language designed for automating the
Windows GUI y Bundled with Watir now (you dont have to require it)
y y y y y y y y y
$browser.file_field(:id, "ctl00_NonGalleryContent_Uploader1_FileUpload1").click_no_wait sleep 2 #------------------------------------------------------------------------------------#AutoIt Watir.autoit.WinWaitActive("Choose file", '', 3) Watir.autoit.ControlSetText("Choose file", "", 1148, "1_gardengnome.jpg") Watir.autoit.ControlClick("Choose file", "", "&Open") #-------------------------------------------------------------------------------------
AutoIt3 Bonus!
go here: http://www.autoitscript.com/autoit3/
y AutoIt3 has an information tool (similar to
the IE dev toolbar) that can help you identify windows objects. Download the full AutoIt3 program to access this tool.
y AutoIt3 Download:
http://www.autoitscript.com/autoit3/dow nloads.shtml
#requires require 'watir' require 'test/unit' require 'test/unit/testcase' require 'opf_navigate_to_opf_test.rb' class TC_manage_accounts < Test::Unit::TestCase #includes include Watir include Mod_navigate_to_OPF_test #variables $gallery_name = "Auto Gallery 17" $version_number = "Version 2.1.1" $storefront_url = "http://laurenssuperwondertestingsite.com/ GalleryThumbnails.aspx" $site_login = xxxx" $site_password = xxxxx"
def create_gallery $browser.link(:class,"ctl00_OwnerBar1_menuGalleries_1 dropdownMenuItem ctl00_OwnerBar1_menuGalleries_5").click $browser.text_field(:id, "ctl00_NonGalleryContent_MyPhotoGallery_mtbTitle_tbText").set( $gallery_name) $browser.button(:id, "ctl00_NonGalleryContent_lnkCreate").click sleep 6 assert_match(/app.test.onlinephotofilercom.ide\/GalleryThumbnails/, $browser.url.to_s) assert($browser.div(:text, "#{$gallery_name}").exists?) puts ("01. Create Gallery - PASS") End def test_smokeTest create_gallery End end #End class: TC_manage_accounts
y y y y y y y y y y
1. http://elandingstest.alaska.gov/confluence/display/IERS/Web+Application+Testing+in+Ruby+-+WATIR+Introduction 2. http://wtr.rubyforge.org/documentation.html 3. http://del.icio.us/behzad/testing 4. http://wiki.openqa.org/display/WTR/Project+Home 5. http://jrandomhacker.info/Watir 6. http://www.io.com/~wazmo/blog/archives/2007_07.html 7. http://wtr.rubyforge.org/ 8. http://swik.net/Watir+Tutorial 9. http://pettichord.com/watirtutorial/reference/index.html 10. http://blog.dukk.org/files/folders/1/download.aspx 11. http://217.77.36.138/presentations/javazone/2006/slides/4499.pdf 12. http://wiki.openqa.org/display/WTR/Watir+Training+Presentation+and+Exercises 13. http://wtr.rubyforge.org/s101/doc/ 14. http://members.shaw.ca/paul_rogers/presentations/Ruby_Watir_CRUSERS.pdf and countless others I have referenced over the years.