Entering Objects Information directly into the test script is called
descriptive programming. In DP; we will be "manually” specifying the
properties and values by which the relevant object will be identified. This way
UFT won’t search for the properties data in the Object Repository, but will
take it from the DP statement.
Object Spy is used to get the properties and their values to uniquely
identify the objects in the application. If we know such properties and
their unique values that can identify the required object without ambiguity, we
need not use Object Spy.
Two ways of
descriptive programming:
1. Static Programming
2. Dynamic Programming
1. Static
Descriptive Programming: We provide the set of properties and values that describe the
object directly in a VB-Script statement.
Example:
'Username="UFT
world"
'Password="UFT"
'***Login to gmail account using Static
descriptive Programing***
'Launch gmail
systemutil.Run
"iexplore.exe","http:\\www.gmail.com"
'Assign object property value to a variable
pwd
pwd="Passwd"
'Wait till browser loads
Browser("title:=Gmail: Email from
Google").Page("title:=Gmail: Email from Google").Sync
'Enter Email id in Username Field
Browser("title:=Gmail: Email from
Google").Page("title:=Gmail: Email from
Google").WebEdit("name:=Email").Set Username
'Enter password in Passowrd Field
Browser("title:=Gmail: Email from
Google").Page("title:=Gmail: Email from
Google").WebEdit("name:=" & pwd).Set Password
'Cick on the Sign In Button
Browser("title:=Gmail: Email from
Google").Page("title:=Gmail: Email from Google").WebButton("name:=Sign
in").Click
2. Dynamic Descriptive Programming: We
add a collection of properties and values to a “Description” object, and then
enter the Description object name in the statement.
Example:
'Username="uft world"
'Password="uft"
'***Login to gmail account using Dynamic
descriptive Programing ***
'Launch gmail
systemutil.Run
"iexplore.exe","http:\\www.gmail.com"
'Descriptive object to identify Browser
with a particular title
Set Dbrowser=description.Create
Dbrowser("micclass").value="Browser"
Dbrowser("title").value="Gmail:
Email from Google"
'Descriptive object to identify Web page with
a particular title
Set Dpage=description.Create
Dpage("micclass").value="Page"
Dpage("title").value="Gmail: Email
from Google"
'Descriptive object to identify a particular
Web Button
Set Dbutton=description.Create
Dbutton("micclass").value="WebButton"
Dbutton("name").value="Sign in"
'Descriptive object to identify Web Text Box
Set Dedit=description.Create
Dedit("micclass").value="WebEdit"
Dedit("name").value="Email"
'wait till browser loads
Browser(Dbrowser).Page(Dpage).Sync
‘Enter Email id in Username Field
Browser(Dbrowser).Page(Dpage).WebEdit(Dedit).Set
"uft world"
Dedit("name").value="Passwd"
'Enter password in Password Field
Browser(Dbrowser).Page(Dpage).WebEdit(Dedit).Set
"uft"
'Cick on the Sign In Button
Browser(Dbrowser).Page(Dpage).WebButton(Dbutton).Click
Important Points to Remember:
1. When using Descriptive Programming from a specific
point within a test object hierarchy, you must continue to use Descriptive
Programming 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 Descriptive Programming, UFT will not be able
to identify the object.
Example: We
can use Browser(Dbrowser).Page(Dpage).Link(oDesc), since it uses Descriptive
Programming throughout the entire test object hierarchy.
You can also use Browser(“Google”).Page(Dpage).Link(oDesc), since it uses
Descriptive Programming from a certain point in the description (startingfrom the Page object description).
However, you cannot use Browser(Dbrowser).Page(Dpage).Link(“Example1”), since
it uses Descriptive Programming for the Browser and Page objects but then attempts to use an object repository name for the Link test object (UFT
tries to locate the Link object based on its name, but cannot locate it in the
repository because the parent objects were specified using Descriptive
Programming).
2. UFT evaluates all property values in Descriptive
Programming as regular expressions. Therefore, if you want to enter a value
that contains a special regular expression character (such as *, ?, or +), use
the \ (backslash) character to instruct QTP to treat the special characters as
literal characters.
Example:
for example if we had parenthesis around username, it would have been
written as
Browser(“micclass:=Browser”).Page(“micclass:=Page”).WebEdit(“type:=text”,”name:=\(userName\)”,”html
tag:=INPUT”).Set "mercury"
since parenthesis is a special regular expression
character and in this case you would want to treat it as a literal.
3. micclass is shown as Class Name in object spy. If you use Class Name in DP, UFT will throw an error. Make
sure to use micclass. Check Class Name vs Class vs micclass in
UFT.
4. Somehow there is a myth in UFT community that DP
can be used when UFT is unable to identify an object using normal means.
Descriptive Programming provides a way to bypass object repository and gives a
bit of flexibility to identify the object. However, if even after adding
appropriate add-ins you are unable to identify an object using normal means,
don’t assume that DP can come to your rescue.
Examples:
Launch the browser, check if correct browser opened or not, ensure
username password singing button exist, set user name and
password,make sure browser navigate to correct page.
‘We will use SystemUtil.Run to launch our target
browser
SystemUtil.Run "iexplore.exe",
"http://newtours.demoaut.com/"
If Browser( "title:=Welcome: Mercury
Tours" ).Exist( 15 ) Then
Reporter.ReportEvent micPass,
"Step 1- Launch", "Correct browser was
launched."
Else
Reporter.ReportEvent micFail,
"Step 1- Launch", "Browser failed to launch."
ExitTest
End If
Uses of the wildcard character:
With
Browser("title:=Welcome:.*").Page("title:=Welcome.*")
If
.WebEdit("name:=userName").Exist(0) Then
.WebEdit("name:=userName").Set "test"
If .WebEdit("name:=password").Exist(0)
Then
.WebEdit("name:=password").Set "test"
If
.Image("name:=login").Exist(0) Then
.Image("name:=login").Click
Else
Reporter.ReportEvent micFail,"Sign-In Button Error","Button
not found."
End If
Else
Reporter.ReportEvent micFail, "Password Edit Error", "EditBox
not
found."
End If
Else
Reporter.ReportEvent
micFail, "UserName Edit Error", "EditBox not found."
End If
End With
Browser( "title:=.*Mercury.*" ).Sync
If Browser( "title:=Find a Flight.*"
).Exist( 1 ) Then
Reporter.ReportEvent micPass, "Login",
"Login successful"
Else
Reporter.ReportEvent micFail,
"Login", "Login failed"
End If
Why to use Descriptive Programming...?
Below are some of the situations when Descriptive
Programming can be considered to useful:
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>>”.
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/UFT while recognizing a object.
NOTE: When you don’t want to use object repository at all.
Why not Object repository?
Scenario 1: Suppose we have a web
application that has not been developed yet. Now QTP/UFT 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/UFT 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.
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.
When you want to take action on similar type of object i.e. suppose we
have 20 textboxes on the page and their 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.
When to use Descriptive Programming...?
1. If the application is having Dynamic Objects.
OR:- Difficult to handle Dynamic Objects using Object Repository.
2. When we have more objects to perform operations.
OR:- The performance will decrease if object repository is having huge number of objects.
3. If the application is having objects that are adding in the Run Time.
OR:- We can’t add objects to Object Repository in run time.
4. If we need to start Automation before Build Release.
OR:- There is no application to create Object Repository.
5. If Application is having similar type of objects or similar name objects.
OR:- Object Repository will create multiple objects with same description unnecessarily.
6. Big Team Size.
OR:- Shared Object Repository is not changeable by multiple persons at a time. Maintenance becomes harder if all the team members have created their own object repositories.
2. When we have more objects to perform operations.
OR:- The performance will decrease if object repository is having huge number of objects.
3. If the application is having objects that are adding in the Run Time.
OR:- We can’t add objects to Object Repository in run time.
4. If we need to start Automation before Build Release.
OR:- There is no application to create Object Repository.
5. If Application is having similar type of objects or similar name objects.
OR:- Object Repository will create multiple objects with same description unnecessarily.
6. Big Team Size.
OR:- Shared Object Repository is not changeable by multiple persons at a time. Maintenance becomes harder if all the team members have created their own object repositories.
Advantages of DP:
1. Version Free
DP:- Script can be executed in any version of QTP without any changes.
2. Code Portability
DP:- Just code is enough run script. We can copy and paste in other scripts for any other required requirement.
3. Reusing of Properties
DP:- We can assign properties to a global variable and reuse it for same type of objects.
4. Plug and Play
DP:- Any time scripts will be in ready to run state. No need to worry about any other settings or files.
5. Just Maintenance of variables
DP:- We can store the object properties in the form of variables in a txt / vbs file which will be placed in central location. In this case we just need to maintain that file.
Handling Run time added objects / Dynamic Objects /
Same Description Objects
Dynamic objects : Property values are
getting changed regularly.
Same Description Objects : Property
values are same for multiple objects.
Runtime Added Object : Object
is not there in recording time but it might be add in runtime.
Ex: Always
the job links in naukri and monster sites will get update.
Child object method is very useful to get control
on Runtime added objects or dynamic objects or same description objects which
are there application.
Example: If there are multiple signin
links on the page, using child objects method we can click on any signin link.
Dim oLinkDescription, oLinks, lnkCount
Set oLinkDescription=Description.Create
oLinkDescription("name").value="signin"
set oLinks=browser("micclass:=Browser").page("micclass:=Page").
ChildObjects(oLinkDescription)
if oLinks.count <> 0 then
oLinks(0).click
End If
Description: This code will click on the first signin link. To click on second signin link replace ‘oLinks(0).click to oLinks(1).click in ‘if’ condition. In the same manner if any object properties are changed we can get index of the object using childobjects method.
Example: If there is a login page having username,
password, signin button fields. Think that the signin button property is
getting changed frequently from signin to login or enter. Then we can use child
objects method to get the buttons count from the page, and then using index we
click on the button which we want.
Set oButtonDescription=Description.Create
oButtonDescription
("micclass").value="Button"
set oButtons=browser("micclass:=Browser").page("micclass:=Page")
.ChildObjects(oButtonDescription)
for cnt=0 to oButtons.count-1
oButtons(cnt).highlight
Next
oButtons(cnt).highlight
Next
Description: The above code will highlight
all buttons in the page. This will make easy to identify the index of the
button. Using index we can easily click on the button.
What method do we need to follow... Static or
Dynamic......?
There is no rule that we have to use static or dynamic. But based on situation we have to decide whether we need to use static or dynamic. If a single property is enough for an object then go for static. And go for Dynamic if multiple properties need to specify for an object.
Example:
Static DP:
Browser("title:=Google").Page("title:=Google").WebEdit("name:=q", "html tag:=INPUT").Set "DP"
Browser("title:=Google").Page("title:=Google").WebButton("name:=Google Search").Click
Dynamic DP:
If you’re going for dynamic the only problem is
repetitive code. Every time we need use "description.create" which
leads to more maintenance. There are three objects and if we want to use
multiple properties for those objects we need to write description.create for
three times.
‘Creating Browser description
‘“title: =Google"
Set oGoogBrowser
= Description.Create
oGoogBrowser( "title" ).value = "Google"
' Creating Page description
' "title:=Google"
Set oGoogPage =
Description.Create
oGoogPage( "title" ).Value = "Google"
'* Creating WebEdit description
' “html tag:=INPUt",
"name:=q"
Set oGoogWebEdit = Description.Create
Set oGoogWebEdit = Description.Create
oGoogWebEdit( "html
tag" ).Value = "INPUT"
oGoogWebEdit( "name" ).Value = "q"
Browser(oGoogBrowser).Page(oGoogPage).WebEdit(oGoogWebEdit).Set "DP
is great"