Search This Blog

Saturday, October 9, 2010

How to install Watir?

 
Execute the following on command prompt after installing ruby
gem update --system
gem install watir

List of elements supported by Watir


 

Example Test Case

Google Test Search
This document walks through a very simple test case: Google Search. To begin, open it in a text editor. If you do not have that file, download it from here. To open it with SciTE, simply right-click the file in Windows Explorer, and select Edit from the popup menu.
The format of this example will be to show the Ruby test case scripting code in a box as you would see it in a text editor, with an explanation after it.
Getting Started
Open Internet Explorer, and try out the test case manually on your own:
1.       go to the Google home page
2.       enter pickaxe in the search text field
3.       click the Google Search button
Expected Result
A Google page with results should be shown. Programming Ruby should be high on the list.
Once you have tried the test case out manually, it's time to automate the test case using Watir. Return to the Google home page and view the page source: right mouse click > View Source. Now you can follow along and see how to automate this test with Watir based on the HTML tags in the Google search web application.

Section 1: Comments
Test cases should be commented, just as program code should be commented. In Ruby, any text on a single line that follows a # is a comment, and is ignored by the Ruby interpreter at run time.
What you see in the text editor:
#-------------------------------------------------------------#
# Demo test for the Watir controller.
#
# Simple Google test written by Jonathan Kohl 10/10/04.
# Purpose: to demonstrate the following Watir functionality:
#   * entering text into a text field,
#   * clicking a button,
#   * checking to see if a page contains text.
# Test will search Google for the "pickaxe" Ruby book.
#-------------------------------------------------------------#
Styles differ with comments, but they can really help with test case maintenance. In this case, the author has provided their name, a date and a purpose of the test case. This is here to improve readability, and to help others who may be using the test to understand what it does.

Section 2: Includes
To use Watir, or any other library in our test case, requires us to tell the program where to find the library.
What you see in the text editor:
# the Watir controller
require "watir"
When we run our test script, the Watir library is loaded so that our test cases can use it.

Section 3: Declare Variables
If we are going to use something in our script more than once, or something that could change, we can declare it as a variable and reuse it throughout the script. Some objects we can use for testing tend to change, such as URLs for applications we are testing. In this script, we assign the test URL as a variable. If it changes, we only have to change it in one place.
What you see in the text editor:
# set a variable
test_site = "http://www.google.com"
The test case author has chosen to assign the URL to a variable called test_site. It may not be much of an issue in this test case, but using variables for test URLs is often a good practice.

Section 4: Open an Internet Explorer Browser
To begin driving Internet Explorer, we need to tell Watir to open an instance for testing.
What you see in the text editor:
# open the IE browser
ie = Watir::IE.new
To explain what this is doing, we can start from the right of the = operator. We send a message new to the IE (Internet Explorer) class that is inside Watir module, and assign it to a variable called ie. The ie variable is a local variable (like test_site). This means it can be accessed from our script, but not from other Ruby functions or methods.

Section 5: Interacting With Google
Now we are ready to start automating the steps we ran manually using Watir.

Beginning the test case
What you see in the text editor:
# print some comments
puts "Beginning of test: Google search."
puts is a reserved word in the Ruby language that tells the Ruby Interpreter to print whatever comes after it contained in quotes to the screen. We could just as easily write this information to a file. These puts statements are in this test case to make it more self-explanatory. You can print to the screen as a "friendly message" (e.g. telling the user something is loading while they are waiting for results, or printing a result as "The answer is 5" instead of just "5") or as "flagging" and that is to print to the screen for debugging purposes. Printing what we are doing when we automate the test case is useful for debugging when developing test cases, and for quickly repeating failures for bug reports when the test case doesn't pass.

Step 1: Go to the Google site
This test case follows a pattern of printing out what we intend Watir to do on a web application, followed by the Watir scripting code to carry out that action. This is a style of test case development that is useful for tracking down test case failures quickly.
What you see in the text editor:
puts " Step 1: go to the test site: " + test_site
ie.goto test_site
The first line uses a puts statement to print out the test case step we are attempting to the screen. The second line uses the Watir method goto to direct the test case to the test site: http://www.google.com (stored in the variable test_site). When we print out the variable, we concatenate it to the string (the part in quotes) by using the + sign.

Step 2: Enter Search Term pickaxe in the search field
We need to enter the term to search in the text field on the Google home page.
What you see in the web browser:
What you see in the text editor:
puts " Step 2: enter 'pickaxe' in the search text field."
ie.text_field(:name, "q").set "pickaxe" # "q" is the name of the search field
The first line prints the step we are on to the screen. The second line enters the text pickaxe in the text field named q. The comment telling the user that q is the name of the text field is optional.
This is the tag in the HTML source with the name attribute we used:
The text field has a name attribute q, so we use that to tell Watir what object to interact with.



Step 3: Click the Google Search button
We need to click the search button to activate the Google Search functionality.
What you see in the web browser:
What you see in the text editor:
puts " Step 3: click the 'Google Search' button."
ie.button(:name, "btnG").click # "btnG" is the name of the Search button
The first line prints the step we are on to the screen. The second line clicks the Google Search button.
This is the tag in the HTML source with the name attribute we used:


Section 6: Evaluating the Results
The Expected Result
This test case prints out what the Expected Result should be prior to the test case using Watir to evaluate the results.
What you see in the text editor:
puts " Expected Result:"
puts "  A Google page with results should be shown. 'Programming Ruby' should be high on the list."
These two statements simply print the expected result of the test to the screen.



Verify Results
Using Watir and a little Ruby, we can evaluate the results to verify whether the test case passed or failed.
puts " Actual Result:"
if ie.text.include? "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

Why Watir?


Why Watir?

  • It’s a free Open Source tool. There are no costs to use the tool.
  • There’s a very active and growing community behind it.
  • It uses Ruby, a full-featured modern scripting language, rather than a proprietary vendorscript.
  • It supports your web app no matter what it is developed in.
  • It supports multiple browsers on different platforms.
  • It is powerful and easy to use, yet beautifully lightweight.

What is Watir (Web Application Testing in Ruby)


Watir is a toolkit used to automate browser-based tests during web application development.
Is an open-source (BSD) family of Ruby libraries for automating web browsers
“Watir drives browsers the same way people do. Watir also checks results, such as whether expected text appears on the page.”
This automated test tool uses the Ruby (programming language) scripting language
  • Internet Explorer,
  • Mozilla Firefox,
  • Google Chrome and
  • Safari (web browser)
Available as a RubyGems gem
Watir operates differently than HTTP based test tools, which operate by simulating a browser. Instead Watir directly drives the browser through the Object Linking and Embedding protocol, which is implemented over the Component Object Model (COM) architecture

Thursday, September 23, 2010

How to get Child Objects?


We can use description object to get all the objects on the page that matches that specific description. Suppose we have to check all the checkboxes present on a web page. So we will first create an object description for a checkboxe and then get all the checkboxes from the page

Dim obj_ChkDesc

Set obj_ChkDesc=Description.Create
obj_ChkDesc(“html tag”).value = “INPUT”
obj_ChkDesc(“type”).value = “checkbox”

Dim allCheckboxes, singleCheckBox

Set  allCheckboxes = Browse(“Browser”).Page(“Page”).ChildObjects(obj_ChkDesc)

For each singleCheckBox in allCheckboxes

      singleCheckBox.Set “ON”

Next

The above code will check all the check boxes present on the page. To get all the child objects we need to specify an object description i.e. we can’t use the string arguments that will be discussed later in the 2nd way of using the programming description.

Possible Operation on Description Object

Consider the below code for all the solutions
Dim obj_ChkDesc

Set obj_ChkDesc=Description.Create
obj_ChkDesc(“html tag”).value = “INPUT”
obj_ChkDesc(“type”).value = “checkbox”

Q: How to get the no. of description defined in a collection
A: obj_ChkDesc.Count ‘Will return 2 in our case

Q: How to remove a description from the collection
A: obj_ChkDesc.remove “html tag” ‘would delete the html tag property from the collection

Q: How do I check if property exists or not in the collection?
A: The answer is that it’s not possible. Because whenever we try to access a property which is not defined its automatically added to the collection. The only    way to determine is to check its value that is use a if statement “if obj_ChkDesc(“html tag”).value = empty then”.

Q: How to browse through all the properties of a properties collection?
A: Two ways
      1st:
                  For each desc in obj_ChkDesc
                              Name=desc.Name
                              Value=desc.Value
                              RE = desc.regularexpression
                  Next
      2nd:
                  For i=0 to obj_ChkDesc.count - 1
                              Name= obj_ChkDesc(i).Name
                              Value= obj_ChkDesc(i).Value
                              RE = obj_ChkDesc(i).regularexpression
                  Next

2.      By giving the description in form of the string arguments.

You can describe an object directly in a statement by specifying property:=value pairs describing the object instead of specifying an object’s
name. The general syntax is:

TestObject("PropertyName1:=PropertyValue1", "..." , "PropertyNameX:=PropertyValueX")

TestObject—the test object class could be WebEdit, WebRadioGroup etc….

PropertyName:=PropertyValue—the test object property and its value. Each property:=value pair should be separated by commas and quotation
marks. Note that you can enter a variable name as the property value if you want to find an object based on property values you retrieve during a run session.

Consider the HTML Code given below:


Now to refer to the textbox the statement would be as given below

Browser(“Browser”).Page(“Page”).WebEdit(“Name:=txt_Name”,”html tag:=INPUT”).set “Test”

And to refer to the radio button the statement would be as given below

Browser(“Browser”).Page(“Page”).WebRadioGroup(“Name:=txt_Name”,”html tag:=INPUT”).set “Test”

If we refer to them as a web element then we will have to distinguish between the 2 using the index property

Browser(“Browser”).Page(“Page”).WebElement(“Name:=txt_Name”,”html tag:=INPUT”,”Index:=0”).set “Test” ‘ Refers to the textbox
Browser(“Browser”).Page(“Page”).WebElement(“Name:=txt_Name”,”html tag:=INPUT”,”Index:=1”).set “Test” ‘ Refers to the radio button


Hierarchy of test description


When using programmatic descriptions from a specific point within a test object hierarchy, you must continue to use programmatic descriptions
From that point onward within the same statement. If you specify a test object by its object repository name after other objects in the hierarchy have
Been described using programmatic descriptions, Quick Test cannot identify the object.

For example, you can use Browser(Desc1).Page(Desc1).Link(desc3), since it uses programmatic descriptions throughout the entire test object hierarchy.
You can also use Browser("Index").Page(Desc1).Link(desc3), since it uses programmatic descriptions from a certain point in the description (starting
from the Page object description).

However, you cannot use Browser(Desc1).Page(Desc1).Link("Example1"), since it uses programmatic descriptions for the Browser and Page objects but
then attempts to use an object repository name for the Link test object (QuickTest tries to locate the Link object based on its name, but cannot
locate it in the repository because the parent objects were specified using programmatic descriptions).

How to use Descriptive programming?


There are two ways in which descriptive programming can be used
1.      By creating properties collection object for the description.
2.      By giving the description in form of the string arguments.

1.      By creating properties collection object for the description.

To use this method you need first to create an empty description

Dim obj_Desc ‘Not necessary to declare
Set obj_Desc = Description.Create

Now we have a blank description in “obj_Desc”. Each description has 3 properties “Name”, “Value” and “Regular Expression”.

obj_Desc(“html tag”).value= “INPUT”

When you use a property name for the first time the property is added to the collection and when you use it again the property is modified. By default each property that is defined is a regular expression. Suppose if we have the following description

obj_Desc(“html tag”).value= “INPUT”
obj_Desc(“name”).value= “txt.*”

This would mean an object with html tag as INPUT and name starting with txt. Now actually that “.*” was considered as regular expression. So, if you want the property “name” not to be recognized as a regular expression then you need to set the “regularexpression” property as FALSE

obj_Desc(“html tag”).value= “INPUT”
obj_Desc(“name”).value= “txt.*”
obj_Desc(“name”).regularexpression= “txt.*”

This is how of we create a description. Now below is the way we can use it

Browser(“Browser”).Page(“Page”).WebEdit(obj_Desc).set “Test”

When we say .WebEdit(obj_Desc) we define one more property for our description that was not earlier defined that is it’s a text box (because QTPs WebEdit boxes map to text boxes in a web page).

If we know that we have more than 1 element with same description on the page then we must define “index” property for the that description

Consider the HTML code given below


Now the html code has two objects with same description. So distinguish between these 2 objects we will use the “index” property. Here is the description for both the object

For 1st textbox:
      obj_Desc(“html tag”).value= “INPUT”
obj_Desc(“name”).value= “txt_Name”
obj_Desc(“index”).value= “0”

For 2nd textbox:
      obj_Desc(“html tag”).value= “INPUT”
obj_Desc(“name”).value= “txt_Name”
obj_Desc(“index”).value= “1”

Consider the HTML Code given below:


We can use the same description for both the objects and still distinguish between both of them
obj_Desc(“html tag”).value= “INPUT”
obj_Desc(“name”).value= “txt_Name”

When I want to refer to the textbox then I will use the inside a WebEdit object and to refer to the radio button I will use the description object with the WebRadioGroup object.

Browser(“Browser”).Page(“Page”).WebEdit(obj_Desc).set “Test” ‘Refers to the text box
Browser(“Browser”).Page(“Page”).WebRadioGroup(obj_Desc).set “Test” ‘Refers to the radio button

But if we use WebElement object for the description then we must define the “index” property because for a webelement the current description would return two objects.