Вы находитесь на странице: 1из 23

Descriptive programming in QTP

Author(s): (1) tarun_lalwani

Written On: 15-Sep-2005

Keywords:

QTP, Quick Test, Quick Test Pro, Descriptive Programming, Programming Description

Introduction:

This document demonstrates the usage of Descriptive programming in QTP. It also


discusses situations where Descriptive programming can be used. Using Descriptive
Programming automation scripts can be created even if the application has not been
developed.

Descriptive Programming:

Whenever QTP records any action on any object of an application, it adds some
description on how to recognize that object to a repository of objects called object
repository. QTP cannot take action on an object until unless its object description is
in the Object Repository. But descriptive programming provides a way to perform
action on objects which are not in Object repository

Object Identification:

To identify an object during the play back of the scripts QTP stores some properties
which helps QTP to uniquely identify the object on a page. Below screen shots shows
an example Object repository:
Now to recognize a radio button on a page QTP had added 2 properties the name of
the radio button and the html tag for it. The name the left tree view is the logical
name given by QTP for the object. This can be changed as per the convenience of
the person writing the test case. QTP only allows UNIQUE logical name under same
level of hierarchy. As we see in the snapshot the two objects in Browser->Page node
are “WebTable” and “testPath”, they cannot have the same logical name. But an
object under some other node can have the same name. Now with the current
repository that we have, we can only write operation on objects which are in the
repository. Some of the example operations are given below

Browser("Browser").Page("Page").WebRadioGroup ("testPath").Select "2"

cellData = Browser("Browser").Page("Page").WebTable
("WebTable").GetCellData (1,1)

Browser("Example2").Page("Page").WebEdit("testPath").Set "Test text"

When and Why to use Descriptive programming?

Below are some of the situations when Descriptive Programming can be considered
useful:
1. The objects in the application are dynamic in nature and need special
handling to identify the object. The best example would be of clicking a link
which changes according to the user of the application, Ex. “Logout
<<UserName>>”.
2. When object repository is getting huge due to the no. of objects being added.
If the size of Object repository increases too much then it decreases the
performance of QTP while recognizing a object.
3. When you don’t want to use object repository at all. Well the first question
would be why not Object repository? Consider the following scenario which
would help understand why not Object repository

Scenario 1: Suppose we have a web application that has not been developed
yet. Now QTP for recording the script and adding the objects to repository
needs the application to be up, that would mean waiting for the application
to be deployed before we can start of with making QTP scripts. But if we
know the descriptions of the objects that will be created then we can still
start off with the script writing for testing

Scenario 2: Suppose an application has 3 navigation buttons on each and


every page. Let the buttons be “Cancel”, “Back” and “Next”. Now recording
action on these buttons would add 3 objects per page in the repository. For a
10 page flow this would mean 30 objects which could have been represented
just by using 3 objects. So instead of adding these 30 objects to the repository
we can just write 3 descriptions for the object and use it on any page.

4. Modification to a test case is needed but the Object repository for the same is
Read only or in shared mode i.e. changes may affect other scripts as well.
5. When you want to take action on similar type of object i.e. suppose we have
20 textboxes on the page and there names are in the form txt_1, txt_2, txt_3
and so on. Now adding all 20 the Object repository would not be a good
programming approach.

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

<INPUT type=”textbox” name=”txt_Name”>


<INPUT type=”textbox” name=”txt_Name”>

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:

<INPUT type=”textbox” name=”txt_Name”>


<INPUT type=”radio” name=”txt_Name”>

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.

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, QuickTest 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).
Getting Child Object:

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:

<INPUT type=”textbox” name=”txt_Name”>


<INPUT type=”radio” name=”txt_Name”>

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

Reference:
1. “Mercury QuickTest Professional, User’s Guide”

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXX

This article would go over some general issues people face while using
Descriptive Programming (DP) in QTP.

Using strings with Pattern

Let’s assume we want to click a link "Logout (Tarun)" on my web page.


Two possible methods that can be used are

Method 1

Browser("miccclass:=Browser").Page("micclass:=Page"). _
Link("text:=Logout (Tarun)").Click
Method 2

Set oDesc = Description.Create


oDesc("text").Value = "Logout (Tarun)"
Browser("miccclass:=Browser").Page("micclass:=Page").Link(oDesc).Click

Now both the above methods will fail giving below mentioned error

Cannot identify the object "[ Link ]" (of class Link). Verify that this object's
properties match an object currently displayed in your application.

Looking through the naked eyes on the web page the link does exist indeed
So what went wrong? The problem was with the characters "(" and ")"
present in the text of the link we used. By default QTP treats all DP
properties as regular expression (r.e.) patterns and "(xxx)" is considered as a
group of patter xxx. The text "Logout (Tarun)" when treated as a r.e. gets a
literal meaning of "Logout Tarun", and since there is no such link on the
web page QTP throws an error. To avoid such situations we need to escape
the regular expression characters using the escape character "\". Now we
have three different solutions to correct the problem

Method 1

Browser("miccclass:=Browser").Page("micclass:=Page"). _
Link("text:=Logout \(Tarun\)").Click

Method 2

Set oDesc = Description.Create


oDesc("text").Value = "Logout \(Tarun\)"
Browser("miccclass:=Browser").Page("micclass:=Page").Link(oDesc).Click
Method 3

Set oDesc = Description.Create


oDesc("text").Value = "Logout (Tarun)"
'Do not treat the value as regular expression.
oDesc("text").RegularExpression = False

Browser("miccclass:=Browser").Page("micclass:=Page").Link(oDesc).Click

IMO Method 3 should be preferred for a neater coding as we are using the
actual text of the link.

Overpopulated description while identifying objects

An overpopulated description does not help in recognizing the object. We


should use minimum no. of properties which are stable enough to recognize
the object on every single run. Consider the below overpopulated description

Set oDesc = Description.Create


oDesc("html tag").Value = "TABLE"
oDesc("micclass").Value = "WebTable"
oDesc("innertext").Value = "abcde"
oDesc("outertext").Value = "abcde"
oDesc("innerhtml").Value = "<TR><TD>abcde</TD></TR>"
oDesc("outerhtml").Value =
"<TABLE><TR><TD>abcde</TD></TR><TABLE>"
oDesc("rows").Value = 1
oDesc("cols").Value = 1

Consider the following advices while create such a description

• rows and cols are dynamic properties which might change if the table
gets updated. These properties should be avoided
• Only one of the properties from innertext, outertext, outerhtml and
innerhtml should be used
• outerhtml and innerhtml properties should be avoided as they contains
various tags and difficult to express
• When using Browser().Page().WebTable(oDesc) we can skip
specifying the micclass and html tag properties also because as soon
as we enclose oDesc with the WebTable() test object these two
properties are mostly implied.

Considering the above points we can reduce our description to just

Set oDesc = Description.Create


oDesc("outertext").Value = "abcde"

Underpopulated description while using ChildObjects

Though we reduced the no. of properties in the description object when


identified a table in the last section but while using ChildObjects method we
should make sure the following

• Maximum description properties should be used to reduce the final


result set. Though we should still follow the advices specified in
earlier section of overpopulated descriptions except the last one
(Where we ignore micclass and HTML tag).
• When using ChildObjects to find WebElements, "html tag" should
always be provided to avoid errors.
• Property names used in description should be as the same case
provided in the QTP help file. IMO changing the case sometimes
causes general run error during script run. Though there is no
documentation proving that description names are case sensitive

Using "Class Name" instead of "micclass"

Don’t know why by Mercury/HP preferred to show micclass as "Class


Name" in the object spy. This misleads many DP user to create a description
with non-existent property class name
'Below is the wrong way
Browser("Class Name:=Browser")

'Below is the right way


Browser("micclass:=Browser")

'Below is the wrong way


Set oDesc = Description.Create
oDesc("Class Name").Value = "Browser"
oDesc("title").Value = "My title"

'Below is the right way


Set oDesc = Description.Create
oDesc("micclass").Value = "Browser"
oDesc("title").Value = "My title"

These are few general issues that people face.

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXX

DP Part 2 - Converting OR Based scripts to DP

This article discusses how we can convert an Object repository based script
to DP based script. For this we will do a simple recording of entering some
text on Google.com search text box and clicking the Google search button

The QTP generated script would look something like below

SystemUtil.Run "C:\Program Files\Internet Explorer\IEXPLORE.EXE"


Browser("Browser").Page("Page").Sync
Browser("Browser").Navigate "http://www.google.com"
Browser("Browser").Page("Google").WebEdit("q").Set "KnowledgeInbox"
Browser("Browser").Page("Google").WebButton("Google Search").Click

All the names used between "" are logical name of the objects in the Object
Repository ("Browser", "Page", "Google", "q", "Google Search") as shown
in below image
Now let’s look the below statement and try and convert it to DP

Browser("Browser").Page("Google").WebButton("Google Search").Click

Converting WebButton("Google Search")

The Google Search object present in the OR has following properties

type = submit

name = Google Search

html tag = INPUT


Now to conver the WebButton("Google Search") to its DP counterpart we
can use two different methods

Method 1 – Using String Description

In this we use string parameters to specify the object properties

Browser("Browser").Page("Google").WebButton("type:=Submit", _
"name:=Google Search", "html tag:=INPUT").Click

Method 2 – Using Object Description

In this we first create a description of the object and then use it in the
statement

Set oGoogleSearch = Descrition.Create


oGoogleSearch("type").Value = "Submit"
oGoogleSearch("name").Value = "Google Search"
oGoogleSearch("html tag").Value = "INPUT"

Browser("Browser").Page("Google").WebButton(oGoogleSearch).Click

Which method of DP to choose?

The later articles of this DP Part X series would be discussing in details


when and where to use for which method. For now just understand that both
methods have their own advantage and disadvantages

Few difference IMO are listed below


String Description Object Description
Uses less memory as strings are used Requires more memory as objects are created.
Object creation is as such a overhead
Increases statement length in case Increase lines of code due to object creation
more than one property is to be used overhead and property assignment
Preferred when property value have regular
expression characters which needs to be treated
literally
(explained in one of the later articles)

DP Converted script

SystemUtil.Run "C:\Program Files\Internet Explorer\IEXPLORE.EXE"

Browser("micclass:=Browser").Page("Page").Sync
Browser("micclass:=Browser").Navigate "http://www.google.com"

Browser("micclass:=Browser").Page("micclass:=Page").WebEdit("name:=q
").Set _
"KnowledgeInbox"

Browser("micclass:=Browser").Page("micclass:=Page") _
.WebButton("type:=Submit", "name:=Google Search", "html
tag:=A").Click

Note: We will discuss more in details about micclass used above in later
articles

Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxx

Description
Enables you to control the recovery scenario mechanism programmatically during the run
session. The recovery scenario mechanism enables you to instruct QuickTest to recover
from unexpected events and errors that occur in your testing environment during a run
session.

Activate

Description

Explicitly activates the recovery scenario mechanism at a specific point in the run.

Note: The Activate method only works if the recovery mechanism is enabled, and only
activates those recovery scenarios that are currently enabled.

If the recovery mechanism is currently disabled, the Activate method does not activate
any recovery scenarios. You can use the Recovery object's Enabled property to change
the status of the recovery mechanism.

Syntax

Recovery.Activate

Example

The following example uses the Activate method to explicitly activate recovery scenarios
three times—the first time it explicitly activates the three scenarios associated with the
current test; the second time it activates only the first and third scenario, since the second
scenario is disabled; the third time it does not activate any scenarios, since the entire
recovery mechanism is disabled.

msgbox Recovery.Count,, "Number of Recovery Scenarios"

msgbox Recovery,, "Is Recovery enabled?"

for Iter = 1 to Recovery.Count

Recovery.GetScenarioName Iter, ScenarioFile, ScenarioName

Position = Recovery.GetScenarioPosition( ScenarioFile, ScenarioName )

msgbox Recovery.GetScenarioStatus( Position ),, "Is scenario " & _


ScenarioName & " from " & ScenarioFile & " enabled ?"

ScenarioFile = Empty

ScenarioName = Empty
Next

Recovery.Activate

Recovery.SetScenarioStatus 2, False

Recovery.Activate

Recovery = False

Recovery.Activate

GetScenarioName

Description

Retrieves the name and source file of a recovery scenario, according to the specified
position in the list of recovery scenarios associated with the test.

Syntax

Recovery.GetScenarioName Position, out_ScenarioFile, out_ScenarioName

Argument Type Description


Position Number The index position of the recovery scenario in the list.
Index position values begin with 1.
out_ScenarioFile String Variable that stores the retrieved name of the recovery
scenario.
out_ScenarioName String Variable that stores the retrieved source file path of the
recovery scenario.

GetScenarioPositio

Description

Returns the index position of a recovery scenario in the list of recovery scenarios
associated with the test, according to the specified name and source file.

Syntax

Recovery.GetScenarioPosition (ScenarioFile, ScenarioName)


Argument Type Description
ScenarioFile String The source file path of the recovery scenario.
ScenarioName String The name of the recovery scenario.

Return Value

Number

GetScenarioStatus

Description

Returns the status of a recovery scenario (True = enabled or False = disabled), according
to the specified index position in the list of recovery scenarios associated with the test.

Syntax

Recovery.GetScenarioStatus Position

Argument Type Description


Position Number The index position of the recovery scenario in the list.
Index position values begin with 1.

Return Value

Boolean

SetScenarioStatus

Description

Enables or disables the specified recovery scenario, according to its index position in the
list of recovery scenarios associated with the test.

Syntax

Recovery.SetScenarioStatus Position, Status

Argument Type Description


Position Number The index position of the recovery scenario in the list.
Index position values begin with 1.
Status Boolean The status to set for the specified scenario (True = enabled or False
= disabled).

Count

Description

Returns the number of recovery scenarios associated with the current test.

Syntax

Recovery.Count

Return Value

Number

Enabled

Description

Recovery default property. Retrieves or sets the status of the recovery scenario
mechanism for the current test.

Syntax

To retrieve the recovery scenario mechanism status:

Recovery.Enabled

or

Recovery

To set the recovery scenario mechanism status:

Recovery.Enabled =Status

or

Recovery =Status
Argument Type Description
Status Boolean The status of the specified scenario (True = enabled or False =
disabled).

ReportEvent Method

Description

Reports an event to the test results.

Syntax

Reporter.ReportEvent EventStatus, ReportStepName, Details [, ImageFilePath]

Argument Type Description


EventStatus Number or Status of the Test Results step:
pre-defined 0 or micPass: Causes the status of this step to be passed
constant and sends the specified message to the Test Results
window.
1 or micFail: Causes the status of this step to be failed
and sends the specified message to the Test Results
window. When this step runs, the test fails.
2 or micDone: Sends a message to the Test Results
window without affecting the pass/fail status of the test.
3 or micWarning: Sends a warning message to the Test
Results window, but does not cause the test to stop
running, and does not affect the pass/fail status of the
test.
ReportStepName String Name of the step displayed in the Test Results window.
Details String Description of the Test Results event. The string will be
displayed in the step details frame in the Test Results
window.
ImageFilePath String Optional. Path and filename of the image to be
displayed in the Results Details tab of the Test Results
window. Images in the following formats can be
displayed: BMP, PNG, JPEG, and GIF.
Notes:

• Images cannot be loaded from Quality Center.


• Including large images in the test results may
impact performance.

• If an image is specified as a relative path,


QuickTest will first search the Results folder for
the image and then the search paths specified in
the Folders pane of the Options dialog box.

Examples

The following examples use the ReportEvent method to report a failed step.

Reporter.ReportEvent 1, "Custom Step", "The user-defined step failed."

or

Reporter.ReportEvent micFail, "Custom Step", "The user-defined step failed."

The following example uses the ReportEvent method to include a captured bitmap in the
test results.

Browser("Browser").Page("WebPage").Image("MyLogo").CaptureBitmap("MyLogo.bm
p")

Reporter.ReportEvent micDone, "Display Logo", "This is my logo", "MyLogo.bmp"

Вам также может понравиться