• Like

Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Introduction to Watir

  • 1,616 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,616
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
53
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • require ‘watir’ Browser=Watir::IE.new Browser.goto(“http://godaddy.com”) Browser.show_all_objects Link = browser.link(:index, 1) Puts link Puts link.text Puts link.flash exit
  • Execute the script from the previous slide – to show watir in action. Very Basic script.
  • Identify Objects Manipulate Test *** Email Report Data-Driven

Transcript

  • 1. PRESENTED BY: LAUREN SNYDER Introduction to Watir
  • 2. What is WATIR?
    • W eb A pplication T esting I n R uby
    • 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.
      • Watir can also check results, such as whether expected text appears on the page.
      • It can be used to test all types of web applications (ASP.Net, JSP, PHP, Rails, etc…)
    • Open Source – written by Bret Pettichord, Paul Rogers and many other contributors.
  • 3. What WATIR is not…
    • Watir is not a record/playback tool.
      • However, there are several recorders “out there”
        • WatirMaker
        • Watir WebRecorder
        • Webmetrics RIA Script Recorder (most recent discussion…they are considering open sourcing their application)
    • Watir is not a link checker
      • However, you can easily write your own link checker and customize it to your specific needs.
    • Watir is not a test case management tool.
      • However, you can write one in Ruby if desired.
    • Doesn’t test Flash or Applets.
  • 4. What is Ruby?
    • Full featured Object Oriented scripting language
      • Made “famous” for it’s web application framework Rails. (Ruby on Rails)
    • Interpreted rather than compiled
    • Written by Matz (Yukihiro Matsumoto)
      • Started in 1994
    • Written in C
      • Will work on any platform that has a C compiler
        • Windows
        • Linux
  • 5. How does Watir work?
    • Uses the COM interface of Internet Explorer (IE)
      • a.k.a ActiveX or OLE
    • Allows an external program to control IE
      • Similar interfaces exist for Word, Excel, PowerPoint and Outlook.
    • Full access to the contents of an HTML page
    • Provides different ways to access objects
  • 6. The BIG question: Why Watir
  • 7. Why Watir cont…
    • As a testing tool: It’s as robust & sophisticated as ‘professional’ tools such as Rational, Mercury & Segue.
    • As a library of a programming language [Ruby ] : It’s powerful.
    • (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…)
    • No “ Vendor-script ”
    • It’s simple – elegant – INTUITIVE
    • It has a supportive online community for when you get ‘stuck’.
  • 8. Setting up WATIR
  • 9. Learning WATIR: Getting Started
    • As you start to get into Ruby/Watir you’ll want some
    • Good information at your fingertips!
    • Introductory Documentation:
    • Watir homepage: http://wtr.rubyforge.org
    • Watir User Guide: http://wiki.openqa.org/display/WTR/User+Guide
    • Scripting 101 Tutorial: http://wtr.rubyforge.org/s101/doc/
    • Books:
    • Everyday Scripting with Ruby: for Teams, Testers, and You: http://www.pragprog.com/titles/bmsft/
    • Programming Ruby (Online Book): http://www.ruby-doc.org/docs/ProgrammingRuby/
  • 10. Learning WATIR: More In-Depth…
    • Forums:
    • Watir General Forum (now on Google Groups): http://groups.google.com/group/watir-general?hl=en
    • Watir Search (web interface that searches 7 Watir sites): http://www.google.com/coop/cse?cx=007267089725385613265%3Agmydx5gtw6u
    • “ The Guts” Documentation:
    • Ruby Rdoc: http://wtr.rubyforge.org/rdoc/index.html
    • Methods Suported by Element Reference: http://wiki.openqa.org/display/WTR/Methods+supported+by+element
    • Online Ruby Information: http://www.ruby-doc.org/
  • 11. Development Environments (IDE’s) for Ruby
    • Use any text editor as an IDE:
      • ScITE (Free)
        • Included with your ruby download.
      • Notepad (Free)
      • Eclipse (using RDT Plugin)
        • http://rubyeclipse.sourceforge.net/
      • Ruby In Steel (Free - $199) (Add-on to VS.Net )
        • http://www.sapphiresteel.com
      • Komodo IDE ($295) / Komodo Edit (Free)
        • http://www.activestate.com
  • 12. Using Ruby’s Interactive Command Interpreter (IRB)
  • 13. Demo: Using IRB
      • Demo: Show_all_objects
  • 14. Let’s get started…
    • It’s time to turn on the watir !
  • 15. Basic Anatomy of a Watir Script #----------------------------------------- #All scripts should use comments #where needed. #----------------------------------------- #Includes require ‘watir’ include Watir #Declare Variables url = “http://www.godaddy.com” #Open the IE Browser browser = Watir::IE.start(url) #Print results to the screen Puts “Begin Test: GoDaddy” #Logical Code / Body of Script if browser.contains_text(“Domain Name Search”) then puts “Test PASSED” else puts “Test FAILED” #Close the IE Browser (clean up) puts “End Test: GoDaddy” browser.close
  • 16.
    • This is the heart and soul of Watir (from the users point of view)
    • Contains all the methods needed to create, navigate and “probe” the IE browser window
    • browser = Watir::IE.start( http://www.godaddy.com )
    • browser = Watir::IE.new
    • browser.attach(:url, http://www.google.com )
    • browser.close
    • browser.maximize
    The Watir::IE Class
  • 17. Use Watir
    • Using the Watir API is very easy.
    • Reference the Watir API using the “require” keyword and start coding
      • require ‘watir’
      • include Watir
      • browser = Watir::IE.start(“http://www.godaddy.com”)
  • 18. Demo: Basic GoDaddy Script
      • Demo: Basic GoDaddy Script
  • 19. Web Pages are all about OBJECTS
    • Web pages are developed with objects:
      • Links, buttons, tables, drop-down boxes, forms, frames, etc.
    • Watir scripts need to access these objects & manipulate them just as a user would.
      • Clicking, submitting, typing, selecting, etc…
  • 20. How do I identify objects?
  • 21. View Source
    • “ View Source” on any page by right-clicking with your mouse on web page:
  • 22. 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.
  • 23. 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} #iterate through all the tables on the page
      • $ie.tables[1].to_s #goto the first table on the page
      • $ie.tables.length #show how many tables are on the page. Tables that are
      • nested will be included
  • 24. IE Developer Tool
    • A tool for exploring and understanding web pages.
    • Locates and selects specific elements on a web page by clicking on the objects in your page.
    • Gives a tree view of objects
  • 25. Demo: IE Developer Toolbar
      • Demo: IE Developer Toolbar
  • 26. Manipulating Objects
    • Now that you know how to identify objects…
    • The next step is how to “Manipulate” objects…
  • 27. Manipulating Web Page Objects: Link
  • 28. Manipulating Web Page Objects: Checkbox
  • 29. Manipulating Web Page Objects: Radio Buttons
  • 30. Manipulating Web Page Objects: Selection Boxes
  • 31. Manipulating Web Page Objects: Text Fields
  • 32. Manipulating Web Page Objects: Buttons
  • 33. A Closer Look…at the structure browser.button(:value, "Click Me").click [Variable] . [method] (: [element] , “ [unique identifier] ” . [method]
  • 34. An Even Closer Look… browser.button( :value , "Click Me").click [Variable] . [method] (: [element] , “ [unique identifier] ” . [method]
  • 35. Element Options for BUTTON
  • 36. 8 elements! –But I only need one…
    • Even though there are 8 possible elements the user has to identify a button…
    • A developer might only use 1 – 3 elements in his code.
    • And you, as a watir scripter can only use 1 element – maybe 2 to describe your desired object.
  • 37. Using multiple identifiers
  • 38. Supported methods by element Chart obtained from: http://wiki.openqa.org/display/WTR/Methods+supported+by+element
  • 39. Method Examples
  • 40. Test Automation is MORE than Identifying objects
    • Identifying objects is currently the most time consuming part of creating your test scripts…
    • However, after your objects have been identified & manipulated: you want to “Test” them!
    • You’ll want to create “PASS” or “FAIL” scenarios.
    … This is the most sophisticated part of your scripts.
  • 41. Test::Unit
    • Test::Unit is a library of Ruby (just like Watir)
      • It is not technically part of Watir…however 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’
    • Test::Unit is a way to organize your code into “tests”
    • Test::Unit has built in methods called “assertions” that help your tests with validation.
      • assert(browser.link(:text, “Click Here”).exists?)
      • The above statement will return a TRUE or FALSE indicating a pass or fail in your test.
  • 42. Test::Unit – Basic Code Structure
    • require 'test/unit’
    • class TC_MyTest < Test::Unit::TestCase
    • include Watir
    • def setup #optional
    • end #optional
    • def teardown #optional
    • end #optional
    • def test_pass
    • assert(something.exists?)
    • end
    • end
  • 43. Test::Unit–A 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']:
    • <&quot;http://app.onlinephotofiler.com/AddGallery.aspx&quot;> expected to be =~
    • </app.onlinephotofiler.com/GalleryThumbnails/>.
    • 1 tests, 1 assertions, 1 failures, 0 errors
    • >Exit code: 0
  • 44. Test::Unit–A Pass
    • Returns results such as these:
    • Loaded suite opf_smoketest
    • Started
    • 01. Create Gallery - PASS
    • 02. Add Photos - PASS
    • 03. Edit Photos - PASS
    • 04. Create Badge - PASS
    • 05. Save Badge - PASS
    • 06. Version Number - PASS
    • 07. Reorder Galleries - PASS
    • 08. Reorder Images - PASS
    • 09. Edit Tags - PASS
    • 10. Edit Title & Description - PASS
    • 11. Create Permalinks - PASS
    • 12. PhotoStore Make Purchase - PASS
    • Finished in 225.701 seconds.
    • 1 tests, 29 assertions, 0 failures, 0 errors
    • >Exit code: 0
  • 45. Test::Unit Assertions
  • 46. How Test::Unit executes your tests.
    • It’s 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’.
  • 47. How Test::Unit executes your tests.
    • If you have the following methods in a testcase using test::unit ->
    • def setup
    • def teardown
    • def test_ 01 def test_ 02 def test_ 03
    • 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
  • 48. How Test::Unit executes your tests.
    • To write your testcases such that you are not launching your IE browser 66x or 180x try this:
      • 1. Use less ‘test” methods and more assertions within your methods
      • -OR-
      • 2. Setup your tests like this:
        • def setup
        • def teardown
        • def 01 #notice these methods no longer start with ‘test’
        • def 02
        • def 03
        • def test_a ll_methods_within_one_launch_of_the_browser_and_in_this_order
        • 01
        • 02
        • 03
        • (Compare this structure with the one on the previous page)
  • 49. Windows Pop-Ups
    • Sometimes when a user is using a web page a pop-up window will appear. These require special attention in watir .
    • Pop-Up examples:
      • Security Alerts
      • Choose File pop-ups
      • Save As
      • Login (username/password) panels
      • Alert boxes
      • Script prompt/textbox
      • Confirmation Boxes (ok/cancel)
  • 50. Windows Pop-Ups – Part 2
    • browser.button(:text, “Click Me”).click  change to:
    • browser.button(:text, “Click Me”).click_no_wait
    • sleep 3 #Use the sleep method with any value you need.
    • “ .attach” method: #create a new browser instance & attach to it.
      • photostore_browser = Watir::IE.attach(:url, /PhotoStore/)
    • -OR-
    • Use AutoIt to manipulate windows controls
  • 51. AutoIt
    • AutoIt is a scripting language designed for automating the Windows GUI
    • Bundled with Watir now (you don’t have to ‘require’ it)
    • $browser.file_field(:id, &quot;ctl00_NonGalleryContent_Uploader1_FileUpload1&quot;).click_no_wait
    • sleep 2
    • #-------------------------------------------------------------------------------------
    • #AutoIt
    • Watir.autoit.WinWaitActive(&quot;Choose file&quot;, '', 3)
    • Watir.autoit.ControlSetText(&quot;Choose file&quot;, &quot;&quot;, 1148, &quot;1_gardengnome.jpg&quot;)
    • Watir.autoit.ControlClick(&quot;Choose file&quot;, &quot;&quot;, &quot;&Open&quot;)
    • #-------------------------------------------------------------------------------------
  • 52. AutoIt3 Bonus!
    • To Read up on AutoIt3 & learn commands go here: http://www.autoitscript.com/autoit3/
    • 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.
    • AutoIt3 Download: http://www.autoitscript.com/autoit3/downloads.shtml
  • 53. Demo: OPF – Smoke Test Script
      • Demo: GoDaddy’s – Online Photo Filer: SmokeTest
  • 54. Putting it all together…
    • #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 = &quot;Auto Gallery 17&quot;
    • $version_number = &quot;Version 2.1.1&quot;
    • $storefront_url = &quot;http://laurenssuperwondertestingsite.com/GalleryThumbnails.aspx&quot;
    • $site_login = “xxxx&quot;
    • $site_password = “xxxxx&quot;
    • def setup
    • navigate_to_OPF_test
    • end
    • def teardown
    • $browser.close
    • end
    def create_gallery $browser .link (:class,&quot;ctl00_OwnerBar1_menuGalleries_1 dropdownMenuItem ctl00_OwnerBar1_menuGalleries_5&quot;).click $browser .text_field (:id, &quot;ctl00_NonGalleryContent_MyPhotoGallery_mtbTitle_tbText&quot;).set($gallery_name) $browser .button (:id, &quot;ctl00_NonGalleryContent_lnkCreate&quot;).click sleep 6 assert_match (/app.test.onlinephotofiler-com.ide/GalleryThumbnails/, $browser.url.to_s) assert ($browser.div(:text, &quot;#{$gallery_name}&quot;).exists?) puts (&quot;01. Create Gallery - PASS&quot;) End def test_smokeTest create_gallery End end #End class: TC_manage_accounts
  • 55. Congratulations! You’re on your way… … to programming the ruby/watir way!
  • 56. References Used 4 Presentation…
    • 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.