Top Banner
06/06/22 06/06/22 Watir Study Watir Study ---In the view a ---In the view a s a tester s a tester 1. 1. Watir Introduction. Watir Introduction. 2. 2. Why Watir? Why Watir? 3. Install Ruby and Watir. 3. Install Ruby and Watir. 4. Study Ruby and locate elemen 4. Study Ruby and locate elemen ts ts 5. Watir & Ruby Resources 5. Watir & Ruby Resources
44

What you can do In WatiR

Dec 09, 2014

Download

Technology

Wesley Chen

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.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: What you can do In WatiR

04/10/2304/10/23

Watir StudyWatir Study ---In the view as a tester ---In the view as a tester

1.1. Watir Introduction.Watir Introduction.2.2. Why Watir?Why Watir?3. Install Ruby and Watir.3. Install Ruby and Watir.4. Study Ruby and locate elements4. Study Ruby and locate elements5. Watir & Ruby Resources5. Watir & Ruby Resources

Page 2: What you can do In WatiR

04/10/2304/10/23

Watir introduction.Watir introduction.

1. Web Application Testing In Ruby

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.

Page 3: What you can do In WatiR

04/10/2304/10/23

Why Watir?Why Watir?

• Open-source• Powerful, due to Ruby is powerful.• Simple (easy to use and learn)• Excellent Support , there’s a very activ

e and growing community behind it.• Support many browsers such as IE, Fir

efox, Safari, Google chrome, Opera

Page 4: What you can do In WatiR

04/10/2304/10/23

About Watir automationAbout 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.

Page 5: What you can do In WatiR

04/10/2304/10/23

How to improve your work efficiencHow to improve your work efficiency in Watir?y in Watir?

• Two examples.

Page 6: What you can do In WatiR

04/10/2304/10/23

Example 1: How to test issue like Example 1: How to test issue like train-3401? Turn to train-3401train-3401? Turn to train-3401

• Run train-3401.rb

• For some work, when use Watir, we can improve the work efficiency easily

Page 7: What you can do In WatiR

04/10/2304/10/23

Example 2: How to test the issue like Example 2: How to test the issue like train-3374? 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

Page 8: What you can do In WatiR

04/10/2304/10/23

Watir automation frameWatir 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;

Page 9: What you can do In WatiR

04/10/2304/10/23

A Watir FrameA Watir Frame

Page 10: What you can do In WatiR

04/10/2304/10/23

The frameThe 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

Page 11: What you can do In WatiR

04/10/2304/10/23

How to make the frame fit for your How to make the frame fit for your project?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

Page 12: What you can do In WatiR

04/10/2304/10/23

Frame of Commerce teamFrame of Commerce team

Page 13: What you can do In WatiR

04/10/2304/10/23

What’s our goal?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.

Page 14: What you can do In WatiR

04/10/2304/10/23

Test data fileTest data file

Page 15: What you can do In WatiR

04/10/2304/10/23

Report fileReport file

Page 16: What you can do In WatiR

04/10/2304/10/23

How to achieve the goal?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

Page 17: What you can do In WatiR

04/10/2304/10/23

InstallInstall

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

Page 18: What you can do In WatiR

04/10/2304/10/23

What you need to do if you want What you need to do if you want automation?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

Page 19: What you can do In WatiR

04/10/2304/10/23

How to turn to an IE browser?How to turn to an IE browser?

1. Create a new IE window

2. Attach to an existing IE window;

Page 20: What you can do In WatiR

04/10/2304/10/23

Create a new IE browserCreate a new IE browser

• $ie=Watir::IE.new

$ie.goto(www.active.com)

• $ie=Watir::IE.start(www.active.com)

Page 21: What you can do In WatiR

04/10/2304/10/23

Attach to an existing browserAttach to an existing browser

• $ie=Watir::IE.attach(:title, ‘’)

• $ie=Watir::IE.attach(:url, ‘’)

• $ie=Watir::IE.find(:title, ‘’)

Page 22: What you can do In WatiR

04/10/2304/10/23

How to interact with the web How to interact with the web element?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

Page 23: What you can do In WatiR

04/10/2304/10/23

Finding <HTML> ElementsFinding <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.

Page 24: What you can do In WatiR

04/10/2304/10/23

What’s ‘how’ and ‘what’?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

Page 25: What you can do In WatiR

04/10/2304/10/23

Some How and what?Some How and what?

Page 26: What you can do In WatiR

04/10/2304/10/23

Check output on the pageCheck 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 expec

ted")• else • puts "Failed to search the result we want"• #$report.test_failed("Failed to get the search result as

expected")• end

Page 27: What you can do In WatiR

04/10/2304/10/23

Define the ruby method in class Define the ruby method in class and moduleand 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")

Page 28: What you can do In WatiR

04/10/2304/10/23

How to use module and class?How to use module and class?

In Ruby, only single inheritance allowed.Football.rb1. Football is with Round attributes2. Football is with Elastomer attributes;An example of football class, with both Round attributes an

d Elastomer

Page 29: What you can do In WatiR

04/10/2304/10/23

Create a google search test caseCreate a google search test case

Page 30: What you can do In WatiR

04/10/2304/10/23

Create MethodsCreate 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

Page 31: What you can do In WatiR

04/10/2304/10/23

Use MethodsUse 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

Page 32: What you can do In WatiR

04/10/2304/10/23

Create formal test casesCreate 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

Page 33: What you can do In WatiR

04/10/2304/10/23

Practice filePractice file

• Read write text file

• Read write PDF

• Read write Excel

• Read SQL file

Page 34: What you can do In WatiR

04/10/2304/10/23

Text exampleText 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")

Page 35: What you can do In WatiR

04/10/2304/10/23

PDF filePDF file1. Gem install pdf-writer2. Put pdftotext.exe into c:\windows\system32• 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")

Page 36: What you can do In WatiR

04/10/2304/10/23

ExcelExcel• 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")

Page 37: What you can do In WatiR

04/10/2304/10/23

Read data from SQL serverRead data from SQL server

• Talk about sql-server.rb

Page 38: What you can do In WatiR

04/10/2304/10/23

How to make the watir system methHow to make the watir system methods to yoursods 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 want4. First: require the file name include system

module or class 5. Second: require your file name6. 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

Page 39: What you can do In WatiR

04/10/2304/10/23

Example: rewrite Watir inner Verify Example: rewrite Watir inner Verify methodmethod

• rewrite_verify.rb

Page 40: What you can do In WatiR

04/10/2304/10/23

Change .rb file into .exeChange .rb file into .exe

• gem install rubyscript2exe

• Rubyscript2exe script.rb

Page 41: What you can do In WatiR

04/10/2304/10/23

Clear idea about this Frame now?Clear idea about this Frame now?

Page 42: What you can do In WatiR

04/10/2304/10/23

Watir LimitationWatir 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.

Page 43: What you can do In WatiR

04/10/2304/10/23

Watir & Ruby ResourcesWatir & Ruby ResourcesWatir• 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

Page 44: What you can do In WatiR

04/10/2304/10/23

Questions && discussionQuestions && discussion

Any questions or ideas, please contact MSN: [email protected]