Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

What you can do In WatiR

6,776 views

Published on

This is the first presentation I have made in my company, it is useful to the guys who know little about Watir. It provides a direction about the whole Watir flow, including details and frame, including theory and practice. Expect it can kind of help you guys on the way of Watir.

Published in: Technology, Education
  • Be the first to comment

What you can do In WatiR

  1. 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. 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. 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. 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. 5. How to improve your work efficiency in Watir? • Two examples. 07/01/09
  6. 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. 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. 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
  9. 9. A Watir Frame 07/01/09
  10. 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. 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. 12. Frame of Commerce team 07/01/09
  13. 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
  14. 14. Test data file 07/01/09
  15. 15. Report file 07/01/09
  16. 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. 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. 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. 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. 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. 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. 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. 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. 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. 25. Some How and what? 07/01/09
  26. 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. 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. 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. 29. Create a google search test case 07/01/09
  30. 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. 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. 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. 33. Practice file • Read write text file • Read write PDF • Read write Excel • Read SQL file 07/01/09
  34. 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. 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. 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. 37. Read data from SQL server • Talk about sql-server.rb 07/01/09
  38. 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. 39. Example: rewrite Watir inner Verify method • rewrite_verify.rb 07/01/09
  40. 40. Change .rb file into .exe • gem install rubyscript2exe • Rubyscript2exe script.rb 07/01/09
  41. 41. Clear idea about this Frame now? 07/01/09
  42. 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. 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. 44. Questions && discussion Any questions or ideas, please contact MSN: cjq_999@sina.com 07/01/09

×