SlideShare a Scribd company logo
Watir Study ---In the view as a tester


  1. Watir Introduction.
  2. Why Watir?
  3. Install Ruby and Watir.
  4. Study Ruby and locate elements
  5. Watir & Ruby Resources


07/01/09
Watir introduction.

1.  W A                T
         eb pplication esting n I R     uby
2. It is a Ruby library which drives Internet Explorer the
     same way people do, clicks links, fills in forms, and
     presses buttons.
3. Watir also checks results, such as whether
     expected text appears on the page.
4. Because it’s build on Ruby, you have the power to
     connect to databases, read data files, export
     XML, excel and structure your code into reusable
     libraries.


07/01/09
Why Watir?
• Open-source
• Powerful, due to Ruby is powerful.
• Simple (easy to use and learn)
• Excellent Support , there’s a very
  active and growing community behind
  it.
• Support many browsers such as IE,
  Firefox, Safari, Google chrome, Opera
07/01/09
About Watir automation
•      For automation, the most important is: to do the
       repeated work, so Watir can.
•      Even if you won’t go on a complete automation
       test, you can use Watir to improve your work
       efficiency in your manual black-box test.
•      It is easy to build up the whole frame for your
       project, because define the object, module, class
       in ruby is agile, but if you want to study it quite
       well, you have to study a lot.



07/01/09
How to improve your work
             efficiency in Watir?
• Two examples.




07/01/09
Example 1: How to test issue like
    train-3401? Turn to train-3401
• Run train-3401.rb
• For some work, when use Watir, we can
  improve the work efficiency easily




07/01/09
Example 2: How to test the issue like
            train-3374?
• Turn to train-3374 and tpSearch.do page
• Run train-3374_84.rb;
• For some work, it is really difficult to do
  manual, but easy when use Watir




07/01/09
Watir automation frame
1.     It is with clear structure;
2.     It is easy to manage, maintain the code
3.     The code is easy to read.
4.     The code can be reused;




07/01/09
A Watir Frame




07/01/09
The frame
•   Element: Store all the frequently-used elements, once the elements on
    the page have been changed, we just need fix it at one place;
•   DataFile: For the automation, we’d better prepare test data, we may
    store all the data in an excel file, this class may get the corresponding
    test data from the excel;
•   StdLib: it deal with the logic in our project, make an example: on edit
    something page, we make some calculation, after save it, it display
    another result, in our automation, we have to check the calculation is
    right or not, so we have to include the arithmetic in a function in file
    StdLib;
•   Reporting: it stores test_passed, test_failed methods, create the test
    result.
•   CreateAccountTC, CustomizeWebTC are all test cases, we will run
    them one by one in the Regression




07/01/09
How to make the frame fit for your
             project?
1. What are the relationships between the
   different models? Any common attributes?
   Completely different?
2. How to make the frame easy to understood to
   you?
3. If the requirement changes, do you have to
   change a lot for replay the script?
4. When you know one frame clearly, you may
   know other frame easy



07/01/09
Frame of Commerce team




07/01/09
What’s our goal?
1. Prepare a test data file;
2. Run our automation script following our
  frame;
3. Get a detailed bug report file, easy to
  know where the bug is.




07/01/09
Test data file




07/01/09
Report file




07/01/09
How to achieve the goal?
•   1. Study function  For Element
•   2. Study class and modules For Stdlib
•   3. Read from the file  For DataFile
•   4. Output to a file  For Reporting
•   5. Understand Watir test frame  For
    CreateAccountTC



07/01/09
Install
1. Install Ruby-1.8.6
2. Install Watir
     1) go to command window
    2) gem update --system
    3) gem install Watir
3. Install Ruby IDE, such as Netbeans.
4. Install IE developer toolbar, FireBug




07/01/09
What you need to do if you want
              automation?
•   Navigate the browser
•   Find elements on the page
•   Interact with elements on the page
•   Check output on the page
      – Create and use Methods
      – Create formal test cases
      – Get the report file



07/01/09
How to turn to an IE browser?
1. Create a new IE window
2. Attach to an existing IE window;




07/01/09
Create a new IE browser
• $ie=Watir::IE.new
  $ie.goto(www.active.com)
• $ie=Watir::IE.start(www.active.com)




07/01/09
Attach to an existing browser
• $ie=Watir::IE.attach(:title, ‘’)
• $ie=Watir::IE.attach(:url, ‘’)
• $ie=Watir::IE.find(:title, ‘’)




07/01/09
How to interact with the web
                    element?
•   require 'watir'
•   $ie=Watir::IE.attach(:title, /Google/)
•   $ie.bring_to_front
•   $ie.maximize
•   $ie.text_field(:name, "q").set("google")
•   $ie.button(:value, "I'm Feeling Lucky").click
•   #$ie.button(:name,"btnI").click



07/01/09
Finding <HTML> Elements
 TextBox             IE.text_field(how, what)
 Button              IE.button(how, what)
 DropDownList        IE.select_list(how, what)
 CheckBox            IE.checkbox(how, what)
 RadioButton         IE.radio(how, what)
 HyperLink           IE.link(how, what)
 Form                IE.form(how, what)
 Frame               IE.frame(how, what)
 And many, many more (div, label, image, etc…)…
 Provides different ways to access objects.

07/01/09
What’s ‘how’ and ‘what’?
For IE.text_field(:how, what)
1. IE: means the ie6, ie7 or ie8 window
2. how: means the attributes used to define the
  text_field, such as id, name and so on
3. what: the value of the attributes in step 2.
4. Example: ie.text_field(:name, "q") defines the
  text box with name of ‘q’ in an opened ie window




07/01/09
Some How and what?




07/01/09
Check output on the page
• # Check whether we can get the search result
• if $ie.text.include?("google earth")
•     puts "We can get the search result as expected"
•      #$report.test_passed("Get the search result as
  expected")
• else
•     puts "Failed to search the result we want"
•     #$report.test_failed("Failed to get the search result as
  expected")
• end



07/01/09
Define the ruby method in class
                and module
•   require 'watir‘
•   #If you put class TestA into another .rb file, you just need:
•   #require “filepath/filename.rb”, then you can use the class the same as below, so do the module.
•   class TestA
•      def test_a(str)
•         puts str
•      end
•   end

•   module TestB
•     def test_a(str)
•       puts str
•     end
•   end

•   testa=TestA.new
•   testa.test_a("Hello World 1")
•   include TestB
•   test_a("Hello World 2")




07/01/09
How to use module and class?

In Ruby, only single inheritance allowed.
Football.rb
3. Football is with Round attributes
4. Football is with Elastomer attributes;
An example of football class, with both Round attributes
    and Elastomer




07/01/09
Create a google search test case




07/01/09
Create Methods
•   require 'watir'

•   # It is kind of Element file
•   def search_field
•      return $ie.text_field(:name, "q")
•   end
•   def lucky_search_button
•      return $ie.button(:value, "I'm Feeling Lucky")
•   end

•   # It is kind of StdLib file
•   def get_all_links_with_google_word
•      google_word_links=[]
•      $ie.links.select { |link|
•          if link.text.downcase.include?("google")
•               google_word_links << link.text
•          end
•      }
•      return google_word_links
•   end




07/01/09
Use Methods
•   # Open an new ie window
•   $ie=Watir::IE.start("www.google.com")
•   $ie.bring_to_front
•   $ie.maximize

•   # Set the content we want to search in google.
•   search_field.set("google")
•   lucky_search_button.click

•   # Check whether we can get the search result
•   puts get_all_links_with_google_word
•   if get_all_links_with_google_word.include?("google map")
•       puts “Passed: There is google map after the search"
•   else
•       puts “Failed: No google map link after the search"
•   end


07/01/09
Create formal test cases
 •   require 'watir'
 •   require 'watir/testcase'
 •   class TestGoogle < Watir::TestCase
 •      def test_setup
 •        $ie=Watir::IE.new
 •        $ie.goto("www.google.com")
 •        $ie.bring_to_front
 •        $ie.maximize
 •      end
 •      def test_turn_to_google_search_page
 •        assert $ie.title.eql?("Google"), "Open IE as expected"
 •        assert $ie.button(:name,/btnG/).exists?, "Search button is existing"
 •        assert $ie.link(:text,"privacy").exists?, "No link privacy"
 •      end
 •      def test_search
 •        $ie.text_field(:name,"q").set("Google")
 •        $ie.button(:name,"btnI").click
 •        assert $ie.text.include?("google earth"), "Search as expected"
 •      end
 •   end



07/01/09
Practice file
•   Read write text file
•   Read write PDF
•   Read write Excel
•   Read SQL file




07/01/09
Text example
•      require 'watir'
•      # Create a text file, put something into it;
•      file=File.new("c:test.txt","w")
•      for i in 0..50
•          file.puts "Now, write #{i}"
•      end
•      file.close
•      system("start c:test.txt")
•      sleep 5
•      system("TASKKILL /F /IM notepad.exe")

•      #Read the content out from the file
•      file2=File.open("c:test.txt","r")
•      while f=file2.gets()
•          puts "Now, read:" + f
•      end
•      file2.close

•      system("del c:test.txt")




07/01/09
PDF file
1.     Gem install pdf-writer
2.     Put pdftotext.exe into c:windowssystem32
•      require "pdf/writer"
•      pdf = PDF::Writer.new
•      pdf.select_font "Times-Roman"
•      pdf.text "Hello, Ruby.", :font_size => 72, :justification => :center
•      for i in 0..10
•         pdf.text "Nice #{i}n", :font_size =>16, :justification => :left
•      end
•      pdf.save_as("c:hello.pdf")
•      pdf.close

•      system("start c:hello.pdf")
•      sleep 5
•      system("TASKKILL /F /IM Acrord32.exe")

•      system("pdftotext -layout -q c:hello.pdf c:test.txt")
•      sleep 1

•      system("start c:test.txt")
•      sleep 3

•      system("TASKKILL /F /IM notepad.exe")
•      system("del c:test.txt")
•      system("del c:hello.pdf")




07/01/09
Excel
•      require 'win32ole'
•      # Write some data to an excel file
•      excel = WIN32OLE.new("excel.application")
•      excel.visible = true
•      workbook = excel.workbooks.add
•      worksheet = workbook.Worksheets(1)
•      worksheet.Range("a1").value = 3
•      worksheet.Range("a2").value = 2
•      worksheet.Range("a3:c10").value = 1
•      workbook.saveas("c:test.xls")

•      # Read data from the excel file
•      sleep 3
•      workbook = excel.workbooks.open("c:test.xls")
•      worksheet=workbook.worksheets(1)

•      first_column=[]
•      row=1
•      while worksheet.range("a#{row}").value
•          first_column << worksheet.range("a#{row}").value
•          row += 1
•      end
•      workbook.close
•      excel.quit
•      p first_column

•      system("del c:test.xls")




07/01/09
Read data from SQL server
• Talk about sql-server.rb




07/01/09
How to make the watir system
                methods to yours
1. Turn to assert, verify system class or module;
2. Copy the modules or class out;
3. Make any changes on the methods or class as
  you want
4. First: require the file name include system
  module or class
5. Second: require your file name
6. Your method will cover the system method, so it
  responses as you want.
7. Once you want to use system method again,
  you just need quite the require in step 5

07/01/09
Example: rewrite Watir inner Verify
             method
• rewrite_verify.rb




07/01/09
Change .rb file into .exe
• gem install rubyscript2exe
• Rubyscript2exe script.rb




07/01/09
Clear idea about this Frame now?




07/01/09
Watir Limitation
•      Doesn’t test Flash or Applets.(underwork)
•      Can’t practice on Google map, drag mouse.
•      Deal with pop ups, frame not sensitive
       enough, We can deal with it by sleep
•      The display of web pages will be affected by
       the computer performance, network speed,
       and Watir drives the browsers, so it will also
       be impacted by the two factors.


07/01/09
Watir & Ruby Resources
Watir
• Watir main site: http://wiki.openqa.org/display/WTR/
• Watir user guide: wtr.rubyforge.org/watir_user_guide.html
• Watir API: wtr.rubyforge.org/rdoc/index.html
• Mailing List: rubyforge.org/mailman/listinfo/wtr-general
• Project site: http://wiki.openqa.org/display/WTR/
• User Contributions/examples: http://wiki.openqa.org/display/WTR/Contributions
• Watir FAQ: http://wiki.openqa.org/display/WTR/FAQ

•   Ruby
•   Ruby site: http://ruby-lang.org
•   Ruby docs: http://ruby-doc.org/
•   Ruby Quickstart: ruby-lang.org/en/documentation/quickstart/

•   A great group, really helpful: http://groups.google.com/group/watir-general




07/01/09
Questions && discussion




      Any questions or ideas, please contact MSN: cjq_999@sina.com



07/01/09

More Related Content

PDF
watir-webdriver
PPT
Introduction To Ruby Watir (Web Application Testing In Ruby)
PPT
Automated Testing With Watir
PPT
Watir Presentation Sumanth Krishna. A
PDF
Watir web automated tests
PPT
Keyword Driven Framework using WATIR
PDF
Automated Testing with Ruby
PDF
Introduction to Selenium and Ruby
watir-webdriver
Introduction To Ruby Watir (Web Application Testing In Ruby)
Automated Testing With Watir
Watir Presentation Sumanth Krishna. A
Watir web automated tests
Keyword Driven Framework using WATIR
Automated Testing with Ruby
Introduction to Selenium and Ruby

What's hot (20)

DOC
Selenium Automation Using Ruby
PDF
jQuery Proven Performance Tips & Tricks
PDF
Selenium bootcamp slides
PPTX
Web driver training
PDF
Django Heresies
PDF
Selenium webdriver
PDF
Page object pattern
DOCX
Step 8_7_ 6_5_4_3_2_ 1 in one_Tutorial for Begineer on Selenium Web Driver-Te...
PDF
A Universal Automation Framework based on BDD Cucumber and Ruby on Rails - Ph...
KEY
How To Write a WordPress Plugin
PPT
jQuery introduction
PDF
Behavior Driven Development - How To Start with Behat
PDF
Top100summit 谷歌-scott-improve your automated web application testing
PDF
BDD with cucumber
PPT
Apache Ant
PDF
Rails 3: Dashing to the Finish
PPTX
Apache Ant
PDF
Behavior Driven Development with Cucumber
PPTX
Django Girls Tutorial
PDF
Story Driven Development With Cucumber
Selenium Automation Using Ruby
jQuery Proven Performance Tips & Tricks
Selenium bootcamp slides
Web driver training
Django Heresies
Selenium webdriver
Page object pattern
Step 8_7_ 6_5_4_3_2_ 1 in one_Tutorial for Begineer on Selenium Web Driver-Te...
A Universal Automation Framework based on BDD Cucumber and Ruby on Rails - Ph...
How To Write a WordPress Plugin
jQuery introduction
Behavior Driven Development - How To Start with Behat
Top100summit 谷歌-scott-improve your automated web application testing
BDD with cucumber
Apache Ant
Rails 3: Dashing to the Finish
Apache Ant
Behavior Driven Development with Cucumber
Django Girls Tutorial
Story Driven Development With Cucumber
Ad

Similar to What you can do In WatiR (20)

PPTX
Improving Your Selenium WebDriver Tests - Belgium testing days_2016
PDF
Advanced Site Studio Class, June 18, 2012
PPTX
SPSDenver - SharePoint & jQuery - What I wish I would have known
PDF
Mastering Test Automation: How to Use Selenium Successfully
PDF
How To Use Selenium Successfully
PDF
Automated testing in javascript
PDF
Getting Started with Selenium
PPTX
SharePoint and jQuery Essentials
PPTX
Android webinar class_5
PDF
How to use selenium successfully
PDF
Beyond Fluffy Bunny. How I leveraged WebObjects in my lean startup.
PPT
Java EE Revisits Design Patterns
PDF
Codeigniter
PPTX
Database training for developers
PDF
How To Use Selenium Successfully (Java Edition)
PDF
Beyond Domino Designer
PPTX
Browser Automated Testing Frameworks - Nightwatch.js
PDF
John Resig Beijing 2010 (English Version)
PPT
Java EE revisits design patterns
PPTX
Untangling spring week5
Improving Your Selenium WebDriver Tests - Belgium testing days_2016
Advanced Site Studio Class, June 18, 2012
SPSDenver - SharePoint & jQuery - What I wish I would have known
Mastering Test Automation: How to Use Selenium Successfully
How To Use Selenium Successfully
Automated testing in javascript
Getting Started with Selenium
SharePoint and jQuery Essentials
Android webinar class_5
How to use selenium successfully
Beyond Fluffy Bunny. How I leveraged WebObjects in my lean startup.
Java EE Revisits Design Patterns
Codeigniter
Database training for developers
How To Use Selenium Successfully (Java Edition)
Beyond Domino Designer
Browser Automated Testing Frameworks - Nightwatch.js
John Resig Beijing 2010 (English Version)
Java EE revisits design patterns
Untangling spring week5
Ad

Recently uploaded (20)

PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
A Presentation on Artificial Intelligence
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPT
Teaching material agriculture food technology
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
cuic standard and advanced reporting.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation theory and applications.pdf
Understanding_Digital_Forensics_Presentation.pptx
Machine learning based COVID-19 study performance prediction
Network Security Unit 5.pdf for BCA BBA.
NewMind AI Monthly Chronicles - July 2025
A Presentation on Artificial Intelligence
Diabetes mellitus diagnosis method based random forest with bat algorithm
Encapsulation_ Review paper, used for researhc scholars
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Teaching material agriculture food technology
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
cuic standard and advanced reporting.pdf
The AUB Centre for AI in Media Proposal.docx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Advanced methodologies resolving dimensionality complications for autism neur...

What you can do In WatiR

  • 1. Watir Study ---In the view as a tester 1. Watir Introduction. 2. Why Watir? 3. Install Ruby and Watir. 4. Study Ruby and locate elements 5. Watir & Ruby Resources 07/01/09
  • 2. Watir introduction. 1. W A T eb pplication esting n I R uby 2. It is a Ruby library which drives Internet Explorer the same way people do, clicks links, fills in forms, and presses buttons. 3. Watir also checks results, such as whether expected text appears on the page. 4. Because it’s build on Ruby, you have the power to connect to databases, read data files, export XML, excel and structure your code into reusable libraries. 07/01/09
  • 3. Why Watir? • Open-source • Powerful, due to Ruby is powerful. • Simple (easy to use and learn) • Excellent Support , there’s a very active and growing community behind it. • Support many browsers such as IE, Firefox, Safari, Google chrome, Opera 07/01/09
  • 4. About Watir automation • For automation, the most important is: to do the repeated work, so Watir can. • Even if you won’t go on a complete automation test, you can use Watir to improve your work efficiency in your manual black-box test. • It is easy to build up the whole frame for your project, because define the object, module, class in ruby is agile, but if you want to study it quite well, you have to study a lot. 07/01/09
  • 5. How to improve your work efficiency in Watir? • Two examples. 07/01/09
  • 6. Example 1: How to test issue like train-3401? Turn to train-3401 • Run train-3401.rb • For some work, when use Watir, we can improve the work efficiency easily 07/01/09
  • 7. Example 2: How to test the issue like train-3374? • Turn to train-3374 and tpSearch.do page • Run train-3374_84.rb; • For some work, it is really difficult to do manual, but easy when use Watir 07/01/09
  • 8. Watir automation frame 1. It is with clear structure; 2. It is easy to manage, maintain the code 3. The code is easy to read. 4. The code can be reused; 07/01/09
  • 10. The frame • Element: Store all the frequently-used elements, once the elements on the page have been changed, we just need fix it at one place; • DataFile: For the automation, we’d better prepare test data, we may store all the data in an excel file, this class may get the corresponding test data from the excel; • StdLib: it deal with the logic in our project, make an example: on edit something page, we make some calculation, after save it, it display another result, in our automation, we have to check the calculation is right or not, so we have to include the arithmetic in a function in file StdLib; • Reporting: it stores test_passed, test_failed methods, create the test result. • CreateAccountTC, CustomizeWebTC are all test cases, we will run them one by one in the Regression 07/01/09
  • 11. How to make the frame fit for your project? 1. What are the relationships between the different models? Any common attributes? Completely different? 2. How to make the frame easy to understood to you? 3. If the requirement changes, do you have to change a lot for replay the script? 4. When you know one frame clearly, you may know other frame easy 07/01/09
  • 12. Frame of Commerce team 07/01/09
  • 13. What’s our goal? 1. Prepare a test data file; 2. Run our automation script following our frame; 3. Get a detailed bug report file, easy to know where the bug is. 07/01/09
  • 16. How to achieve the goal? • 1. Study function  For Element • 2. Study class and modules For Stdlib • 3. Read from the file  For DataFile • 4. Output to a file  For Reporting • 5. Understand Watir test frame  For CreateAccountTC 07/01/09
  • 17. Install 1. Install Ruby-1.8.6 2. Install Watir 1) go to command window 2) gem update --system 3) gem install Watir 3. Install Ruby IDE, such as Netbeans. 4. Install IE developer toolbar, FireBug 07/01/09
  • 18. What you need to do if you want automation? • Navigate the browser • Find elements on the page • Interact with elements on the page • Check output on the page – Create and use Methods – Create formal test cases – Get the report file 07/01/09
  • 19. How to turn to an IE browser? 1. Create a new IE window 2. Attach to an existing IE window; 07/01/09
  • 20. Create a new IE browser • $ie=Watir::IE.new $ie.goto(www.active.com) • $ie=Watir::IE.start(www.active.com) 07/01/09
  • 21. Attach to an existing browser • $ie=Watir::IE.attach(:title, ‘’) • $ie=Watir::IE.attach(:url, ‘’) • $ie=Watir::IE.find(:title, ‘’) 07/01/09
  • 22. How to interact with the web element? • require 'watir' • $ie=Watir::IE.attach(:title, /Google/) • $ie.bring_to_front • $ie.maximize • $ie.text_field(:name, "q").set("google") • $ie.button(:value, "I'm Feeling Lucky").click • #$ie.button(:name,"btnI").click 07/01/09
  • 23. Finding <HTML> Elements TextBox IE.text_field(how, what) Button IE.button(how, what) DropDownList IE.select_list(how, what) CheckBox IE.checkbox(how, what) RadioButton IE.radio(how, what) HyperLink IE.link(how, what) Form IE.form(how, what) Frame IE.frame(how, what) And many, many more (div, label, image, etc…)… Provides different ways to access objects. 07/01/09
  • 24. What’s ‘how’ and ‘what’? For IE.text_field(:how, what) 1. IE: means the ie6, ie7 or ie8 window 2. how: means the attributes used to define the text_field, such as id, name and so on 3. what: the value of the attributes in step 2. 4. Example: ie.text_field(:name, "q") defines the text box with name of ‘q’ in an opened ie window 07/01/09
  • 25. Some How and what? 07/01/09
  • 26. Check output on the page • # Check whether we can get the search result • if $ie.text.include?("google earth") • puts "We can get the search result as expected" • #$report.test_passed("Get the search result as expected") • else • puts "Failed to search the result we want" • #$report.test_failed("Failed to get the search result as expected") • end 07/01/09
  • 27. Define the ruby method in class and module • require 'watir‘ • #If you put class TestA into another .rb file, you just need: • #require “filepath/filename.rb”, then you can use the class the same as below, so do the module. • class TestA • def test_a(str) • puts str • end • end • module TestB • def test_a(str) • puts str • end • end • testa=TestA.new • testa.test_a("Hello World 1") • include TestB • test_a("Hello World 2") 07/01/09
  • 28. How to use module and class? In Ruby, only single inheritance allowed. Football.rb 3. Football is with Round attributes 4. Football is with Elastomer attributes; An example of football class, with both Round attributes and Elastomer 07/01/09
  • 29. Create a google search test case 07/01/09
  • 30. Create Methods • require 'watir' • # It is kind of Element file • def search_field • return $ie.text_field(:name, "q") • end • def lucky_search_button • return $ie.button(:value, "I'm Feeling Lucky") • end • # It is kind of StdLib file • def get_all_links_with_google_word • google_word_links=[] • $ie.links.select { |link| • if link.text.downcase.include?("google") • google_word_links << link.text • end • } • return google_word_links • end 07/01/09
  • 31. Use Methods • # Open an new ie window • $ie=Watir::IE.start("www.google.com") • $ie.bring_to_front • $ie.maximize • # Set the content we want to search in google. • search_field.set("google") • lucky_search_button.click • # Check whether we can get the search result • puts get_all_links_with_google_word • if get_all_links_with_google_word.include?("google map") • puts “Passed: There is google map after the search" • else • puts “Failed: No google map link after the search" • end 07/01/09
  • 32. Create formal test cases • require 'watir' • require 'watir/testcase' • class TestGoogle < Watir::TestCase • def test_setup • $ie=Watir::IE.new • $ie.goto("www.google.com") • $ie.bring_to_front • $ie.maximize • end • def test_turn_to_google_search_page • assert $ie.title.eql?("Google"), "Open IE as expected" • assert $ie.button(:name,/btnG/).exists?, "Search button is existing" • assert $ie.link(:text,"privacy").exists?, "No link privacy" • end • def test_search • $ie.text_field(:name,"q").set("Google") • $ie.button(:name,"btnI").click • assert $ie.text.include?("google earth"), "Search as expected" • end • end 07/01/09
  • 33. Practice file • Read write text file • Read write PDF • Read write Excel • Read SQL file 07/01/09
  • 34. Text example • require 'watir' • # Create a text file, put something into it; • file=File.new("c:test.txt","w") • for i in 0..50 • file.puts "Now, write #{i}" • end • file.close • system("start c:test.txt") • sleep 5 • system("TASKKILL /F /IM notepad.exe") • #Read the content out from the file • file2=File.open("c:test.txt","r") • while f=file2.gets() • puts "Now, read:" + f • end • file2.close • system("del c:test.txt") 07/01/09
  • 35. PDF file 1. Gem install pdf-writer 2. Put pdftotext.exe into c:windowssystem32 • require "pdf/writer" • pdf = PDF::Writer.new • pdf.select_font "Times-Roman" • pdf.text "Hello, Ruby.", :font_size => 72, :justification => :center • for i in 0..10 • pdf.text "Nice #{i}n", :font_size =>16, :justification => :left • end • pdf.save_as("c:hello.pdf") • pdf.close • system("start c:hello.pdf") • sleep 5 • system("TASKKILL /F /IM Acrord32.exe") • system("pdftotext -layout -q c:hello.pdf c:test.txt") • sleep 1 • system("start c:test.txt") • sleep 3 • system("TASKKILL /F /IM notepad.exe") • system("del c:test.txt") • system("del c:hello.pdf") 07/01/09
  • 36. Excel • require 'win32ole' • # Write some data to an excel file • excel = WIN32OLE.new("excel.application") • excel.visible = true • workbook = excel.workbooks.add • worksheet = workbook.Worksheets(1) • worksheet.Range("a1").value = 3 • worksheet.Range("a2").value = 2 • worksheet.Range("a3:c10").value = 1 • workbook.saveas("c:test.xls") • # Read data from the excel file • sleep 3 • workbook = excel.workbooks.open("c:test.xls") • worksheet=workbook.worksheets(1) • first_column=[] • row=1 • while worksheet.range("a#{row}").value • first_column << worksheet.range("a#{row}").value • row += 1 • end • workbook.close • excel.quit • p first_column • system("del c:test.xls") 07/01/09
  • 37. Read data from SQL server • Talk about sql-server.rb 07/01/09
  • 38. How to make the watir system methods to yours 1. Turn to assert, verify system class or module; 2. Copy the modules or class out; 3. Make any changes on the methods or class as you want 4. First: require the file name include system module or class 5. Second: require your file name 6. Your method will cover the system method, so it responses as you want. 7. Once you want to use system method again, you just need quite the require in step 5 07/01/09
  • 39. Example: rewrite Watir inner Verify method • rewrite_verify.rb 07/01/09
  • 40. Change .rb file into .exe • gem install rubyscript2exe • Rubyscript2exe script.rb 07/01/09
  • 41. Clear idea about this Frame now? 07/01/09
  • 42. Watir Limitation • Doesn’t test Flash or Applets.(underwork) • Can’t practice on Google map, drag mouse. • Deal with pop ups, frame not sensitive enough, We can deal with it by sleep • The display of web pages will be affected by the computer performance, network speed, and Watir drives the browsers, so it will also be impacted by the two factors. 07/01/09
  • 43. Watir & Ruby Resources Watir • Watir main site: http://wiki.openqa.org/display/WTR/ • Watir user guide: wtr.rubyforge.org/watir_user_guide.html • Watir API: wtr.rubyforge.org/rdoc/index.html • Mailing List: rubyforge.org/mailman/listinfo/wtr-general • Project site: http://wiki.openqa.org/display/WTR/ • User Contributions/examples: http://wiki.openqa.org/display/WTR/Contributions • Watir FAQ: http://wiki.openqa.org/display/WTR/FAQ • Ruby • Ruby site: http://ruby-lang.org • Ruby docs: http://ruby-doc.org/ • Ruby Quickstart: ruby-lang.org/en/documentation/quickstart/ • A great group, really helpful: http://groups.google.com/group/watir-general 07/01/09
  • 44. Questions && discussion Any questions or ideas, please contact MSN: cjq_999@sina.com 07/01/09