Sunteți pe pagina 1din 90

WEB AUTOMATION WITH WATIR

Dec 08, 2011

Agenda
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.

Watir Introduction Watir Installation and testing Installation testing and troubleshotting Ruby/Watir IDE A Sample Watir test script Running Watir test script Locating HTML elements Interacting with HTML elements Controlling HTML elements Watir Unit Test Framework Test Implementation with Page Object Pattern Watir Recorder Others

Watir Introduction
Watir is an open-source Ruby libraries for

automating web browsers


Locates html objects Interacts found object Validate results

Watir supports only Internet Explorer on

Windows Watir-WebDriver supports Chrome, Firefox, Internet Explorer, Opera Watir uses the power of Ruby programming languages

Watir Installation
Install Ruby Interpreter Install RubyGems (package manager) Install Watir

Install Watir-Webdriver

Install Ruby Interpreter


On window Go to Ruby Installer and choose the most recent version to install

(Ruby 1.9.2-p290 or newer) Follow screen intructions to complete installation

Select the following two options Add Ruby executables to your PATH Associate .rb and .rbw files with this Ruby installation

Check that Ruby was installed successfully. In command line


>ruby v ruby 1.9.2p290 (2011-07-09) [i386-mingw32]

Updating RubyGems
Sometimes Ruby installs do not have the

latest package of RubyGem To update your RubyGems


>gem update system () RubyGems system software updated

Verify your RubyGems was updated to the

current version
>gem v 1.8.11

Installing Watir
In Command Prompt (new session), type
gem install watir

You should get a response that looks something like this (do not worry if the Watir version number or gems count doesnt match exactly):
>gem install watir () Successfully installed water-2.0.1 13 gems installed

Test the install by driving Internet Explorer


In Command Prompt, type the following:
irb require watir browser = Watir::Browser.new title=>

#true #<Watir::IE:0x..f8169d749 url=about:blank

A new IE window should open:

Installing Watir-WebDriver
In Command Prompt
gem install watir-webdriver

You should get a response that looks something like this:


>gem install watir-webdriver () Successfully installed watir-webrdiver-0.3.2 6 gems installed

Install ChromeDriver for Chrome

browser Configure security setting in IE


Four zones have been set to the

same mode (enabled or disabled).

Installing Watir-WebDriver
Test the install by driving Internet Explorer

Test the install by driving Firefox


Test the install by driving Chrome
In Command Prompt, type the following:
irb require watir-webdriver =>#true browser = Watir::Browser.new :ie => #<Watir::Browser:0x4eb80dc url="http://localhost:5555/" title="WebDriver">

Replace :ie with :ff or :chrome to test firefox and chrome respectively

Installation Troubleshotting
Refer to

http://wiki.mpifix.com/index.php/Installing_Watir_for_ Windows#Troubleshooting_Watir-Webdriver Or copy the whole Ruby folder on a machine has sucessful installation to yours (verified on Windows only)
The folder is commonly at C:\Ruby192

Installing Watir on Mac


The same steps that happens on Window
Install Ruby interpreter
Install/update Ruby Gem

Refer to http://wiki.mpifix.com/index.php/Installing_Watir_for_ Mac_OS for further details

Ruby/Watir IDE
RubyInSteel
Plug in to Visual studio 60-day trial Syntax checking and auto suggestion Also put at \\filevn01\Apps\Utilities\RubyInSteel_VS2010_2.zip

Note: RubyInSteel has problem with Ruby 1.9.3 version

RubyMine
Standalone 30-day trial Strong syntax checking and auto suggestion Also put at \\filevn01\Apps\Utilities\RubyMine-3.2.4.exe

Others
NetBeans TextMate (MAC) Komodo (wins, mac, linux)

Ruby In Steel

RubyMine

Sample Watir test script


Google search require 'watir' # the watir controller include Watir

#Declaring Variables test_site = 'http://www.google.com' #Opening Internet explorer browser ie = IE.new #Navigating a web page ie.goto(test_site) #interact with objects in web page by enter a search keyword and click on Search button ie.text_field(:name, "q").set("pickaxe") # q is the name of the search field ie.button(:name, "btnG").click # "btnG" is the name of the Search button sleep 4 #evaluating results if ie.contains_text("Programming Ruby") puts "Test Passed. Found the test string: 'Programming Ruby'. Actual Results match Expected Results." else puts "Test Failed! Could not find: 'Programming Ruby'" end

Running a Watir test script


Command line: Store watir script into a file and call it from command line

Running a Watir test script-Cont.1


RubyInSteel: Point RubyInSteel to Ruby Interpreter

Running a Watir test script-Cont.2


RubyInSteel Just click on Run button to run

Running a Watir test script-Cont.3


RubyMine
Just right click on watir file and select Run

End of Day #3
Do you understand items mentioned in this session?

Q&A?

Locating HTML Elements


Watir syntax

Get Element by single artribute


Get Element by multiple artributes Get Element by regular expression

Get Element by Index


Get Element by Xpath query Get Element by nesting

Tips to locate objects

Locating HTML Elements - Watir syntax


HTML Element Name TextBox HTMl Button Image Button Label

HTML Example
<input type="text" id="one" name="typeinme"> <input type="button" id="one" name="clickme" value="Click Me"> <input type="image" src="images/doit.gif"> <label class="ss-q-help" for="entry_0">A text field</label>

Watir Syntax IE.text_field(how, what) IE.button(how, what) IE.button(how, what) IE.label(how, what)

<select id="one" name="selectme"> <option></option> <option>Ruby</option> <option>Watir</option> <option>CSharp</option> </select>

DropDownList

IE.select_list(how, what)

CheckBox Radio

<input type="checkbox" id="one" name="checkme"> <input type="radio" name="clickme" id="one">

IE.checkbox(how, what) IE.radio(how, what)

Locating HTML Elements-Watir syntax Cont.1


HyperLink Form
<a href="http://pragmaticprogrammer.com/titles/ruby/" id="one" name="book">Pickaxe</a> <form id="one" name="loginform" action="login" method="get"> </form> <frameset cols="*,*"> <frame src="menu.htm" name="menu"> </frameset>
<span class=gbtcb></span> <div class="ss-form-entry">

IE.link(how, what)

IE.form(how, what)

Frame Span Div P

IE.frame(how, what) IE.span(how, what) IE.div(how, what) IE.p(how, what)

<input type="submit" value="Submit" name="submit"> </div> <p>This is some text in a paragraph.</p> <table id="gbmpal"> <tr> <td class="gbmpala"> <a id="gb_71" onclick="gbar.logger.il(9,{l:'o'})" href="https://accounts.google.com" class="gbml1">Sign out</a> </td> </tr> </table>

Table

IE.table(how, what)

Locating HTML Elements-Watir syntax-Cont.2


Image
<img src="planets.gif" width="145" height="126" alt="Planets" usemap="#planetmap" /> <map name="planetmap"> <area shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun" />

IE.img(how, what)

Map
<area shape="circle" coords="90,58,3" href="mercur.htm" alt="Mercury" /> </map>

IE.map(how, what)

Area

<area shape="circle" coords="90,58,3" href="mercur.htm" alt="Mercury" /> <h1>This is heading 1</h1> <h2>This is heading 2</h2> <h3>This is heading 3</h3> <h4>This is heading 4</h4> <h5>This is heading 5</h5> <h6>This is heading 6</h6>

IE.label(how, what) IE.h1(how, what) IE.h2(how, what)

h1-h6

Locating HTML Elements-Watir syntax - Cont.3


Where
Hows:

:name :id :index :value :text :title


Whats:

For example ie.text_field(:id, txtUsername) ie.text_field(:id, txtPassword) ie.button(:id, BtnGo) ie.select_list(:id, YearDDL)

String value of how /Regular expression/

Locating HTML Elements - Single artribute


browser.text_field(:id, "entry_0") browser.text_field(:class, "ss-q-short") browser.text_field(:name, "entry.0.single")

Play with this sample page https://spreadsheets.google.com/viewform?hl=en&formkey=dDliTk 5XU1R4RUMtb2c1WDZxWHNENmc6MQ#gid=0


require "watir-webdriver" include Watir browser=Browser.new :chrome browser.goto "https://spreadsheets.google.com/viewform?hl=en&formkey=dDliTk5XU1R4RUMtb2c1WD ZxWHNENmc6MQ#gid=0" browser.text_field(:id, "entry_0").exists? chrome.text_field(:class, "ss-q-short").exists? chrome.text_field(:name, "entry.0.single").exists?

Locating HTML Elements Multiple artributes


browser.radio(:name => "entry.8.group", :value => "2") Browser.radio(:name => "entry.8.group", :value => "3")

Play with this sample page https://spreadsheets.google.com/viewform?hl=en&formkey=dDliTk 5XU1R4RUMtb2c1WDZxWHNENmc6MQ#gid=0

Locating HTML Elements regular expression


This statement will locate a form on web page which has action link contains formResponse
chrome.form(:action, /formResponse/)

Locating HTML Elements regular expression


This statement will locate a form on web page which has action link contains formResponse
chrome.form(:action, /formResponse/)

n$ ^n /n/

Matches any string with n at the end of it Matches any string with n at the beginning of it Matches any string that contains string n

Locating HTML Elements regular expressionCont.1


n$ Matches any string with n at the end of it

^n
/n/ [abc]

Matches any string with n at the beginning of it


Matches any string that contains string n Match any character between the brackets

Locating HTML Elements - Index


browser.checkbox(:name => "entry.4.group", :index=>0) browser.checkbox(:name => "entry.4.group", :index=>1) browser.checkbox(:name => "entry.4.group", :index=>2)

Play with this sample page https://spreadsheets.google.com/viewform?hl=en&formkey=dDliTk5XU1R4RU Mtb2c1WDZxWHNENmc6MQ#gid=0

Locating HTML Elements - Xpath


When An element which you can't access using those pre-defined attributes? There is no API for a particular HTML element? When you want to access some element depeding upon some other element? When performance is needed

Locating HTML Elements Xpath-Cont.1


Some Xpath query strings
Xpath samples //div id(bob) or //*[@id='bob'] //div[@id='bob'] //*[@class='bob'] //div[@class='bob'] //div//a //div/a //div[@class='bob']/.. //a[@title] //a[@title='bob'] //a[contains(@title,'bob')] //a[starts-with(@title,'bob')] Comment Find all elements of a particular type Find all elements with a specific ID Find an element of a particular type with a specific ID Find elements with a specific class Find an element of a particular type with a specific class Find descendant elements of a particular type Find direct child elements of a particular type Find a parent element of an element of a particular type that have an artribute of a specific value Find elements of a particular type that have a specific attribute Find elements of a particular type that have an attribute of a specific value Find elements of a particular type that have an attribute that contains a specific value Find elements of a particular type that have an attribute that starts with a specific value

Locating HTML Elements Xpath-Cont.2


Example Suppose you have an HTML "select" element like:
<select foo="bar"> <option value="1"> 1< /option> < /select>

Now "foo" is not standard attribute but browser will simply ignore it, so you can still get the element by
element = browser.select(:xpath, "//select[@foo='bar']")

Suppose you have a "map" like this:


<map name="top_menu_map" id="top_menu_map"> <area shape="rect" coords="18,2,62,17" href="http://abc.com/public/signup.htm" target="_self" alt="engin home"> </area > </map>

Now theres no area class in Watir, and you want to access this element
browser.element_by_xpath("//area[contains(@href , 'signup.htm')]").click()

Locating HTML Elements Xpath-Cont.3


Example Accessing elements with respect to other elements
<table> <tr>
<td> <img src="1.jpg"> <input type="button"> < /td> <td> <img src="2.jpg"> <input type="button"> < /td> <td> <img src="3.jpg"> <input type="button"> < /td> <td> <img src="4.jpg"> <input type="button"> < /td>

< /tr> <tr>


<td> <img src="5.jpg"> <input type="button"> < /td> <td> <img src="6.jpg"> <input type="button"> < /td> <td> <img src="7.jpg"> <input type="button"> < /td> <td> <img src="8.jpg"> <input type="button"> < /td>

< /tr> < /table>

Now suppose you want to click on button that has image with src="7.jpg" in front of it. So you have two ways to do it:
browser.button(:xpath, "//img[@src='7.jpg']/input").click()

Locating HTML Elements Nesting


You can access an element by going through DOM

hierarchy
For example, if you had something like this:

<div id="one"> <div id="two"> <a href="http://watir.com/">click me</a> </div> </div>

You could click the link with:


browser.div(:id => "one").div(:id => "two").link(:text => "click me").click

So, there is a browser, then a div, another div and the link we are looking for.

Locating HTML Elements Nesting Cont.1


Suppose you have a dropdown list as below

And you want to get all elements in the dropdown box


@browser.select_list(:id, /dropDownListOdometerUnit/).options.each do |each| puts each.text end

Locating HTML Elements Tips


Use irb mode to see whether the element can be

gotten, this approach is to avoid any timing issue due to Javascript/Ajax


Use exists method Use flash method to highlight object on web page screen

E.g.
irb(main):064:0> chrome.text_field(:id, "entry_0").exists? => true irb(main):065:0> chrome.text_field(:id, "entry_0").flash => 10

Locating HTML Elements Tips Cont.1


Use HTML collections to print all elements of a particular type,

you then can easily get the desized elemens from printed properties
Explicitly specicy HTML elements

@browser.links.each do |link| puts link.href end

You can also narrow down the result, just simply fill in further filter condition
@browser.links(:href => /create_vehicle.aspx/).each do |link| puts link.href end

Locating HTML Elements Tips Cont.2


Use browser elements method

Suppose you have the html code as in below screenshot

Your code would be


elements = @browser.elements(:class,"ruledRows") elements.each do |element| puts element.text end

The output will be 2006 Saturn Ion-2 2.2L 2004 Saturn Ion 2.2L, Vin F, Eng Des L61, USA 2001 Dodge Durango 5.9L, Vin Z 2007 Ford F-250 Super Duty XL 5.4L, GAS, Vin 2007 Buick Lucerne CX 3.8L 2005 Honda CR-V EX 2.4L

End of Day #4
Do you understand items mentioned in this session?

Q&A?

Interact with HTML Elements


Interact with Browser

Interact with Page elements (HTML elements)

Interact with HTML Elements Browser


Launch browser browser = Watir::Browser.new :chrome Navigate to a page URL browser = Browser.goto "www.identifix.com" Launch browser and navigate to a web page browser = Browser.start "www.identifix.com" Back a page on browser browser.back() Forward a page on browser browser.forward() Refresh browser browser.refresh() Clear browser cookies browser.clear_cookies() Check if a new window opens with Watir-Webdriver browser.windows.size # You can check the number of windows with this method

Interact with HTML Elements Browser Cont.1


Attach to a browser window created Utilize use method on browser object Example:
require "watir-webdriver" include Watir ff.goto "www.identifix.com" ff.a(:id, "hlDetailLink").click ff.window(:title, "Featured Fix OF The Week").use ff.title #"Featured Fix OF The Week" ff.window(:title, "Direct-Hit").use irb(main):038:0> ff.title #=> "Direct-Hit"

Interact with HTML Elements Page elements


Setting the text field (or text area) specified name

specified value.
ie.text_field(:name,'name').set('value')

Setting the select with to the specified value ie.select_list(:name,'name').select('value') Clicking the button with the specified value (label) ie.button(:value,'value').click Clicking the link matching 'text' ie.link(:text,'text').click Enabling a checkbox @browser.checkbox(:name, "name").set

Interact with HTML Elements Page elements Cont.1


Accessing elements in a

"frame" or "iframe
Note that you can not directly access elemnent in iframe/frame from browser level, the access has to through iframe

This wont get the element ff.area(:target, "_blank").send_keys :enter This will get the element ff.frame(:name => "ifrmRotation", :id=>"ifrmRotation").area(:target, "_blank").send_keys :enter

Interact with HTML Elements Page elements


Sending keys to interact with element In some cases, you need to check actions from user inputs from keyboard, you can also simulate that action in Watir E.g.
@ie.button(:id, 'BtnGo').send_keys :enter

The learn more button in previous example is a typical case, the navigation does not work with click action Reference http://watirwebdriver.com/sending-special-keys/

Interact with HTML Elements Page elements Cont.1


Trigger elements event Usually in IE, actions like clicking on hyperlinks, buttons does not trigger the action, in those cases, you will need to trigger elements events directly from Watir-Webdriver

E.g ie.button(:id, "BtnGo").fire_event("onclick")

Controlling your script


Why timing control?

Sleep statement
Implicit waits Explicit waits

Ajax Handling
Javascript handling

Controlling your script - Why timing control?


By design, Watir does not wait for new page to load, except:
when you use" to an existing window use the "click" method "set" or "clear" radio buttons or checkboxes when you select an item in a select list when you clear a text field when you "goto" a new page when you "submit" a form when you "back", "forward" or "refresh" the browser when you "fire_event" on any element

Controlling your script Why timing control? Cont.1


This means that you do not have explicitly code for waiting for a browser page to load. However, there are still some circumstances where you will need to wait New windows (e.g. popup) Objects that depends on Javascript to show up Asynchronous javascript / AJAX Slow things down for troubleshooting reasons

Controlling your script Sleep statement


An example might be that you're loading the Google

home page and for some reason it's taking time to load. So your script could be as below
require 'watir-webdriver' browser = Watir::Browser.start('http://www.google.com') sleep 5 # we need to wait for the page to load, I've chosen 5 seconds which works on my machine browser.text_field(:name, 'q').set('ruby poignant') .... Back to sample google search example of the training to see how things work if sleep 5 statement is removed

Unfortunately the sleep is hardcoded and doesn't

work for anyone else who has slower network connection

Controlling your script Implicit Waits


Specify a maximum time (in seconds) the script will

try to find an element before timing out. This is done by setting the property of the underlying driverSpecify a maximum time (in seconds) the script will try to find an element before timing out
E.g
require 'watir-webdriver' b = Watir::Browser.new b.driver.manage.timeouts.implicit_wait = 3 #3 seconds

Note: Using implicit waits can make your tests slower and more difficult to understand when they fail

Controlling your script Explicit Waits


Watir::Wait.until { ... }: where you can wait for a block to be true object.when_present.set: where you can do something when its

present object.wait_until_present: where you just wait until something is present object.wait_while_present: where you just wait until something disappears
The default timeout for all these methods is 30 seconds, but your can pass an argument to any of these to increase (or decrease) it as needed.
E.g require 'watir-webdriver' b = Watir::Browser.new :chrome b.goto 'bit.ly/watir-webdriver-demo' b.select_list(:id => 'entry_1').wait_until_present b.text_field(:id => 'entry_0').when_present.set 'your name' b.button(:name => 'submit').click b.button(:name => 'submit').wait_while_present Watir::Wait.until { b.text.include? 'Thank you' }

Controlling your script Ajax handling


How?
Ajax handling from Watir is just simply using timing control to wait

for result from Ajax event. The timing control may be a wait loop (while iterate with sleep command) or built-in control functions

=> Its not recommended to use wait loop


E.g Or a simpler option def wait_until_loaded(timeout = 30) start_time = Time.now until (application_reports_its_loaded == 'true') do sleep 0.1 if Time.now - start_time> timeout raise RuntimeError, "Timed out after #{timeout} seconds" end end end tries = 0 until browser.link(:text, /link_to_wait_for/).exists? do sleep 0.5 tries += 1 end browser.link(:text, /link_to_wait_for/).click end

Controlling your script Ajax handling Cont.1


Sample loop control
def wait_until_loaded(timeout = 30) start_time = Time.now until (application_reports_its_loaded == 'true') do sleep 0.1 if Time.now - start_time> timeout raise RuntimeError, "Timed out after #{timeout} seconds" end end End

Or a simpler option
tries = 0 until browser.link(:text, /link_to_wait_for/).exists? do sleep 0.5 tries += 1 end browser.link(:text, /link_to_wait_for/).click end

Controlling your script Ajax handling Cont.2


Built-in Methods

(explicit methods)
Suppose you click on Go button in screenshot aside and expect the secret word is returned

require "watir-webdriver" include Watir @browser = Browser.new :chrome @browser.goto ("http://www.degraeve.com/reference/simple-ajax-example.php") @browser.button(:value => "Go").click Wait.until {@browser.div(:id, 'result').text.include? "The secret word"}

Controlling your script Javascript handling


Same reasons and

approaches that applied to Ajax handling


E.g
Select Year, Make, Model and Engine to create a vehicle in DH Create Vehicle page

Wait.until {browser.select(:name, 'YearDDL').when_present.text.include? "Year: Please Select"} browser.select_list(:id, "YearDDL").select 2004

Watir Unit Test Framework


Installation

Create a test instance


Create a test method Verify results (assertion statements)

Chain test cases in single Unit test class


Setup and Teardown A Sample Test::Unit class template

A Sample Watir Test::Unit class


Run the test cases in Test::Unit class Run test cases in multiple Test::Unit classes

Watir Unit Test Framework - Installation


Ruby has an xUnit framework called Test::Unit

that we can use with Watir. You do not have to install anything, Test::Unit is included in Ruby To use Test::Unit in your test scripts, enter the following in your test script
require 'test/unit'

Watir Unit Test Framework - Create a test instance


Create your class for your test case that inherites

from Test::Unit::TestCase class


class myTest < Test::Unit::TestCase # fill in Test Case methods here end

Watir Unit Test Framework - Create a test method


Test method starts with test.

Test method that does not start with test wont be called

automatically, but they can be referenced by unit test methods (starts with test)
def test_01_search_valid browser.text_field(:name, "q").set("pickaxe") browser.button(:name, "btnG").click assert(browser.contains_text("Programming Ruby"), "Test Passed. Found the test string: 'Programming Ruby'") end

Watir Unit Test Framework - Verify results


Watir can support assertions by wrapping Watir methods within an assert

E.g
assert(ie.contains_text("Reached test verification point.") Watir can test for many different states of objects. We can use assertions to check of objects exists, are enabled or disabled, or any other state that the DOM tells the web browser about an object Some assertions that Ruby Unit Test Framework supports assert (Test::Unit::Assertions)

assert_block (Test::Unit::Assertions) assert_equal (Test::Unit::Assertions) assert_no_match (Test::Unit::Assertions) assert_not_equal (Test::Unit::Assertions) assert_not_nil (Test::Unit::Assertions) assert_not_same (Test::Unit::Assertions) assert_nothing_raised (Test::Unit::Assertions) assert_nothing_thrown (Test::Unit::Assertions) assert_raise (Test::Unit::Assertions) assert_respond_to (Test::Unit::Assertions)

Watir Unit Test Framework - Chain test cases in single Unit test class
Test::Unit class allows you to have more than one test method

inside the class


When we run the test script from the command line, Test::Unit

uses reflection to go through our test class and execute all the test cases declared in it.
The runner by default executes the test cases alphabetically,

so if you need to chain test cases, prefix letters from the alphabet or numbers after the test prefix to force them to run in order. E.g
test_01_your_method_name test_02_your_method_name

Watir Unit Test Framework - Setup and Teardown


The method names setup and teardown are reserved for

Test::Unit.
If you would like to use setup and teardown functionality,

simply use those as method names for the actions you want executed before and after executing each test case.
def setup # fill in code that will run before every test case here End def teardown # fill in code that will run after every test case here end

Watir Unit Test Framework - A Sample Test::Unit class template


A sample Test::Unit class which include things mentioned above

would be as below
require 'watir-weddriver' require 'test/unit' include Watir #includes Ruby's test case functionality #include Watir namespace

class WatirTest < Test::Unit::TestCase def setup # fill in code that will run before every test case here end def test_01_your_method_name # fill in method body with Watir code and assertion here end def test_02_your_method_name # fill in method body with Watir code and assertion here end def teardown # fill in code that will run after every test case here end end

Watir Unit Test Framework - A Sample Watir Test::Unit class: Google search
class WatirTest < Test::Unit::TestCase def setup if $ie == nil $ie = IE.new $ie.goto("http://google.com") end end def test_01_search_valid $ie.text_field(:name, "q").set("pickaxe") $ie.button(:name, "btnG").click assert($ie.contains_text("Programming Ruby"), "Test Passed. Found the test string: 'Programming Ruby'") end def test_02_search_invalid $ie.text_field(:name, "q").set("csharp") $ie.button(:name, "btnG").click assert(not($ie.contains_text("Programming Ruby")), "Test Passed. Did not find string: 'Programming Ruby'") end end

End of Day #5
Do you understand items mentioned in this session?

Q&A?

Watir Unit Test Framework Run test cases


Run single script
Like running for a watir test script as inntroduced earlier
Syntax
>ruby "filepath\filename.rb" argument1 argument2, ... For example

E.g
>ruby "C:\Users\trhuynh\Desktop\DirectHit\Main\Tests\Automation\DirectHitPAT\ TCs\TC.Login.rb" firefox

Watir Unit Test Framework Run test cases Cont.1


Run a specific test method in Unit::TestCase
If Watir is written using Ruby Unit Test Framework, we can only run a particular test method instead of all test methods. In order to do that, specify test method name followed by --name" directive and your file name
For example
>ruby my_test_file.rb --name test_01_your_method_name

Watir Unit Test Framework Run test cases Cont.2


Using Command-line Arguments
You can access any command-line arguments passed by the shell

with the ARGV special variable. ARGV is an Array variable which holds, as strings, each argument passed by the shell.
In your Watir script, you can get each argument by using [] operator

(e.g. ARGV[0], ARGV[1]...)


We can make use of this feature to indicate which browser, which

test environment the written Watir test scripts to run against

Watir Unit Test Framework Run test cases Cont.3


Run multiple scripts
Ruby does not support to specify multiple scripts in command line But you can still run multiple scripts by create an additional ruby file which "require"

individual scripts The command-line arguments passed by the shell will be the arguments passing to each individual scripts specified in additional ruby file For example, suppose you have two Watir scripts "C:/Users/trhuynh/Desktop/Test/test1.rb" "C:/Users/trhuynh/Desktop/Test/test2.rb" You then can run these two script by creating an additional file (e.g. testAll.rb) which has content as require "C:/Users/trhuynh/Desktop/Test/test1.rb" require "C:/Users/trhuynh/Desktop/Test/test2.rb" Now, you can run the test as below
>ruby "C:/Users/trhuynh/Desktop/Test/TestAll.rb" firefox
We would utilize this feature to create test suite for PAT in Continuos Integration

Watir Unit Test Framework Run test cases Cont.4


Run same script with different settings
Its useful when you want to repeat the same test with various

settings like repeating the same test on different browser What we need to do is wrap the full path + fike name into system comand E.g
system("ruby #{File.dirname(__FILE__)}/Suite.Search.rb \"chrome\" \"www.identifix.com\"") system("ruby #{File.dirname(__FILE__)}/Suite.Search.rb \"firefox\" \"www.identifix.com\"")

Storing element objects and data test with page object framework
Advantage

An example
Implementation Test Execution

Page object framework - advantage


Page Objects is a design pattern to model the

application under test as objects in your code. Page Objects eliminate duplication by building an abstraction that allows you to write browser tests for maximum functional test maintainability and robustness
Promotes re-use and reduce duplication Make tests readable Make tests more robust Improve maintainability, particualarly is the application is

rapidly evolving

Page object framework - Example


Consider this script:
require 'watir-webdriver' # the watir controller include Watir browser = Watir::Browser.new :chrome browser.goto "www.identifix.com" browser.text_field(:id => "txtUsername").set "tester0101" browser.text_field(:id => "txtPassword").set "tester0101" browser.button(:id => "BtnGo").click assert(Watir::Wait.until { browser.title == " Direct-Hit -- Create A New Vehicle " } browser.select_list(:id, "YearDDL").select "2004" browser.select_list(:id, "MakeDDL").select "Saturn" browser.select_list(:id, "ModelDDL").select "Ion" browser.select_list(:id, 'OptionsDDL').select "2.2L, Vin F, Eng Des L61, USA/Canada" browser.button(:value,"Select Vehicle").click assert(Watir::Wait.until { browser.title == "Direct-Hit -- Main Asset Search" }

Page object framework Example Cont.1


With page objects, this could

be splitted into diferent classes to take the advantage of page object framework
Pages.Login Pages.CreateVehicle Pages.VehicleHome

Page object framework Example Cont.2


Pages.Login.rb
class LoginPage < BrowserContainer def open @browser.goto URL self end
def login_as(user, pass) user_field.set user password_field.set pass go_button.click next_page = CreateVehiclePage.new(@browser) Watir::Wait.until { next_page.loaded? } return next_page end private def user_field @browser.text_field(:id => "txtUsername") end def password_field @browser.text_field(:id => "txtPassword") end def go_button @browser.button(:id => "BtnGo") end end

Page object framework Example Cont.3


Pages.CreateVehicle
class CreateVehiclePage < BrowserContainer def logged_in? logged_in_element.exists? end def loaded? @browser.title == "Direct-Hit -- Create A New Vehicle"

end
def create_vehicle(year, make, model, engine) @browser.select_list(:id, "YearDDL").select year @browser.select_list(:id, "MakeDDL").select make @browser.select_list(:id, "ModelDDL").select model @browser.select_list(:id, 'OptionsDDL').select engine select_button.click() #return next_page next_page = VehicleHomePage.new(@browser) Watir::Wait.until { next_page.loaded? } return next_page end private def logged_in_element @browser.link(:title => "Logout") end def year_select @browser.select_list(:id, "YearDDL") end def make_select @browser.select_list(:id, "MakeDDL") end def model_select @browser.select_list(:id, "ModelDDL") end def select_button @browser.button(:value,"Select Vehicle") end end

Page object framework Example Cont.4


Pages.VehicleHome.rb
class VehicleHomePage < BrowserContainer def logged_in? logged_in_element.exists? end def loaded? @browser.title == "Direct-Hit -- Main Asset Search" end private def logged_in_element @browser.image(:id => "tabHome", :alt => "Home") end end

Page object framework Example Cont.5


Parms.Login.rb
class VehicleHomePage < BrowserContainer def logged_in? logged_in_element.exists? end def loaded? @browser.title == "Direct-Hit -- Main Asset Search" end private def logged_in_element @browser.image(:id => "tabHome", :alt => "Home") end end

Page object framework Example Cont.6


Parms.Login.rb
class LoginPageParms def self.get_username return tester0101" end def self.get_password return tester0101" end end

Page object framework Example Cont.7


Site.rb
require require require require "#{File.dirname(__FILE__)}/base_page.rb" "#{File.dirname(__FILE__)}/login_page.rb" "#{File.dirname(__FILE__)}/create_vehicle_page.rb" "#{File.dirname(__FILE__)}/vehicle_home_page.rb"

class Site < BrowserContainer def login_page @login_page = LoginPage.new(@browser) end def create_vehicle_page @create_vehicle_page = CreateVehiclePage.new(@browser) end def vehicle_home_page @vehicle_home_page = VehicleHomePage.new(@browser) end def close @browser.close end end # Site

Page object framework - Implementation


Create a class file corresponding to each web

page of web application


Pages.Login Pages.CreateVehicle Pages.VehicleHome

Page object framework Implementation Cont.1


Define class for each class file Reference to base class

require "#{File.dirname(__FILE__)}/base_page.rb"
Test methods that users would work on the web page under test; it depends of your test

purposes. At least therere two methods, one to dertermine whether the page is loaded, the other one to dertermine if the page is loaded correctly def logged_in? logged_in_element.exists? end def loaded? @browser.title == "Direct-Hit -- Main Asset Search" end

All object definition should not be exposed to user

private def logged_in_element @browser.image(:id => "tabHome", :alt => "Home") end

Page object framework Implementation Cont.2


Add a site class Site represents the web application under test; the site class should contain methods to instantiate all page classes
Add all page class references to site class

require "#{File.dirname(__FILE__)}/base_page.rb" require "#{File.dirname(__FILE__)}/login_page.rb" require "#{File.dirname(__FILE__)}/create_vehicle_page.rb" require "#{File.dirname(__FILE__)}/vehicle_home_page.rb"


Add methods to instantiate each page class

def login_page @login_page = LoginPage.new(@browser) end def create_vehicle_page @create_vehicle_page = CreateVehiclePage.new(@browser) end def vehicle_home_page @vehicle_home_page = VehicleHomePage.new(@browser) end

Page object framework Implementation Cont.3


Create further paremeter classes This is optional; it depends on how much common data your test methods will need to access in each web page. If you feel its needed, you may create each paremeter class for each page class
class LoginPageParms def self.get_username return tester0101" end def self.get_password return tester0101" end

end

Page object framework Implementation Cont.4


Call page class methods in your test methods in Ruby Test::Unit

framework
def test_01_login @login_page = $site.login_page.open @create_vehicle_page = @login_page.login_as LoginPageParms.get_username(),\ LoginPageParms.get_password() assert(@create_vehicle_page.logged_in?, "Test Passed. Login successfully") end def test_02_create_vehicle @create_vehicle_page = $site.create_vehicle_page @vehicle_home_page = @create_vehicle_page.create_vehicle "2004", "Saturn", "Ion", "2.2L, Vin F, Eng Des L61, USA/Canada" assert(@vehicle_home_page.logged_in?, "Created and entered VHP successfully") end

Page object framework Test Execution


Same as running a normal Unit:Test test case, refer to Watir Unit Test

Framework Run test cases section

End of Day #6
Do you understand items mentioned in this session?

Q&A?

S-ar putea să vă placă și