Facebook

For any corporate training requirements Please mail To sudhakar@qtpsudhakar.com
New Schedule in MindQ Systems, Madhapur By SUDHAKAR
Course Name Date Time Duration Type Branch
Selenium+Appium 16th October 2017 7.30 AM - 9.00 AM IST 45 Days ClassRoom + Online Mind Q Madhapur
Selenium+Appium 30th October 2017 9.30 AM - 11.00 AM IST 45 Days ClassRoom + Online Mind Q Madhapur

A book "Cracking the QTP interview" authored by Sudhakar.Read More. Buy From FlipKart

Wednesday, February 11, 2009

Descriptive Programming in QTP


What is Descriptive Programming?



Descriptive Programming is a method of performing operation on the object which is not there in Object Repository. You can also use programmatic descriptions to perform the same operation on several objects with certain identical properties, or to perform an operation on an object whose properties match a description that you determine dynamically during the run session.

When to use Descriptive Programming?
Here there are some situations 
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.

Advantages of Descriptive Programming
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.

Types of Programmatic Descriptions

1. Static: - You list the set of properties and values that describe the object directly in a VBScript statement.
a. Direct specification of description in script
Browser(“micclass:=Browser”).Page(micclass:=page”).Link(“name:=Login”).Click
b. Assigning description to the variables and use that variables in script
g_MainBrowser = “micclass:=Browser”
g_MainPage = “micclass:=Page”
g_Lnk_Login = “name:=Login”
Browser(g_MainBrowser).Page(g_MainPage).Link(g_Lnk_Login).Click

2. Dynamic: - You add a collection of properties and values to a Description object, and then enter the Description object name in the statement.

Set oBrowser = Description.create
oBrowser (“micclass”).value=”Browser”
oBrowser (“name”).value= “Google”
Set oPage = Description.create
oPage (“micclass”).value=”Page”
oPage (“name”).value= “Google”
Set oLink = Description.create
oLink (“name”).value= “Login”
oLink (“index”).value= 1
Browser(oBrowser).Page(oPage).Link(oLink).click

Using the Static type to enter programmatic descriptions directly into your statements may be easier for basic object description needs. However, in most cases, using the Dynamic type provides more power, efficiency, and flexibility. 

Handling Runtime 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.

Ex1: If there are multiple signin links on the page, using child objects method we can click on any signin link.

'******************************************************************************
Dim oLinkDescription
Dim oLinks
Dim 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
'******************************************************************************

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.

Ex2: 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

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. 
EX:

‘Static
Public const LoginPage_Edit_UserName = “name:=UserName”
Public const LoginPage_Edit_PassWord = “name:=Password”
Public const LoginPage_Button_Login = “name:=Login”
To use these constants in your script always it’s a good practice to follow is Keep these constants in a vbs file and associate to your QTP Test.

‘Dynamic
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.

Solution:
Public const LoginPage_Edit_UserName = “name:=UserName”
Public const LoginPage_Edit_PassWord = “name:=Password”
Set LoginPage_Button_Login = CreateObjectDescription(“name:=Login,type:=submit”)

‘******************************************************************
Function CreateObjectDescription(StrProperties)
Dim objDescription
Dim ObjArr
Dim PropCount
Dim ObjProperty
Set objDescription=Description.Create
ObjArr=split(StrProperties,",")
For PropCount=0 to ubound(ObjArr)
     ObjProperty=split(ObjArr(PropCount),":=")      objDescription(ObjProperty(0)).value=ObjProperty(1)
Next
Set CreateObjectDescription=objDescription
End Function
‘******************************************************************
This function gives a way to use multiple properties for any number of objects with less code maintenance. But the only one rule is we need follow is ‘set’ statement. Because the return value of the function CreateObjectDescription is a description object. To store any object in a variable we need to use ‘Set” statement.
The parameter which you pass to the function should be in string format.

EX: Set LoginPage_Button_Login = CreateObjectDescription(“name:=Login,type:=submit”)

Can I Use the combination of Object Repository and Descriptive programming in a statement?
Yes. But the hierarchy must be in Test Object to Descriptive.

Ex:‘Acceptable
Browser(OR LogicalName).Page(OR LogicalName).Link(“name:Login”).click
Or
Browser(OR LogicalName).Page(“micclass:Page”).Link(“name:Login”).click
Here the hierarchy is Test Object to Descriptive. Browser and page are representing Object Repository names and the link is taking description.

‘Not acceptable
Browser(“micclass:=Browser”).Page(OR LogicalName).Link(“name:Login”).click
 Or
Browser(“micclass:=Browser”).Page(“micclass:=Page”).).Link(OR LogicalName).click
This means that from the point where you have started giving description to the objects in a statement, you must continue giving description to the total objects in that hierarchy.

Using Regular Expressions
You can directly use regular expressions in the properties.
Ex1:
Public const LoginPage_Edit_UserName = “name:=UserName.*”
Ex2:
Browser(“micclass:=Browser”).Page(micclass:=page”).Link(“name:=Login.*”).Click
Ex3:
Set oLink = Description.create
oLink (“name”).value= “Login.*”
oLink (“index”).value= 1
Browser(“micclass:=Browser”).Page(micclass:=page”).Link(oLink).click
By default regular expressions for description object is enable. To disable Regular expressions for description object
Set oLink = Description.create
oLink (“name”).value= “Login.*”
oLink (“name”).RegularExpression=False
oLink (“index”).value= 1


No comments :

Post a Comment