Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

Showing posts with label Descriptive. Show all posts
Showing posts with label Descriptive. Show all posts

Descriptive Programming in UFT : 10 Key points

1. Descriptive programming is describing the object properties in the code itself instead of defining an object in the object repository.

2. Descriptive programming has two flavors: 
 a.) Static DP
 b.) Dynamic DP

3. Static DP defines the object property in inline description in the Page tree structure as defined below:
''example 3.1) - In case all the object in the tree are defined as static DP
Browser("title:=Bing").Page("title:=Bing").WebEdit("name:=qb").Set "test automation"

''example 3.2) Multiple properties are used to uniquely identify an element,
 the properties are seperated by ,
Browser("title:=ng").Page("title:=ng").WebEdit("name:=qb", "type:=input").Set "test"


4. The structure of element in the page follows the tree structure as Browser --> Page --> WebElement.
We cannot define browser and page as descriptive programming, in case  last element in the tree(WebEdit, WebElement is defined from Object repository.

5. Dynamic description example is shown below:
Set objdescBR = description.Create
objdesc("title").value ="Google"
Set objdescPg = description.Create
objdescPg("title").value ="Google"
Set objdesc = description.Create
objdesc("micclass").value ="Link"
Set objLinks = Browser(objdescBR).Page(objdescPg).childobjects(objDesc)
Msgbox objLinks.count

6. Dynamic description is useful to perform similar action on group of elements. e.g: get the text of all the links in the page.

7. Set objLinks = Browser(objdescBR).Page(objdescPg).childobjects(objDesc) returns list of elements. This can be a list of links in the page or multiple checkbox
in the page.

8. We can access each element of group of element using objLinks(index) as shown below:
For i=0 to objLinks.count -1 to Step 1
 objLinks(i).GetRoProperty("name")
Next

9. Descriptive programming is very useful in case of:

9.1 - Common element definition used across a set of pages in the application. e.g: Canel, OK, New button
9.2 - In case of DP programming used in function libraries, the objects are defined in Function library and can be used in multiple tests.
9.3 - Performing same action with multiple elements with similar properties.
9.4 - Maintaining OR can be complex and OR size may grow considerably in the application. 
9.5 - Application is not ready. 
9.6 - Keyword driven framework.
9.7 - A good approach is to use a mix of Object Repository and Object repository and xpath definition.

10. Regular expression can be used together with Descriptive programming, which can again be part 
of another article 10 things to know, regular expression with descriptive programming.

How to use GETROProperty to extract value using descriptive programming in QTP

We can use descriptive programming to display the value stored for object e.g the value displayed in a edit box or a webelement or default value in a webList.


We can learn following key points from the code below:


  • How to create a description object using description.create
  • How to implement descriptive programming for object uniquely identified by using multiple properties
  • How to use getROProperty to extract information at runtime
  • How to return value by a function.


Public Function ValueDisplayedForObject(DescObj,ObjType)
On error resume next
Set objDesc= Description.create
'Set of Property value are delimited by ,
If (instr(1,DescObj,",") >0) Then
   ''Split multiple links based on delimiter. ,
arrDesc = split(DescObj,",")
intPropSet = Ubound(arrDesc)
''Loop through each of the link and click the required link
for i = 0 to intPropSet
'' Property and value for property are seperated by |
strDesc = split(arrDesc(i),"|")
objDesc(strDesc(0)).value = strDesc(1)
Next
''In case of only one property value set of image
Else
strDesc = split(DescObj,"|")
objDesc(strDesc(0)).value = strDesc(1)
End If
If (ucase(ObjType) = "WEBELEMENT")  Then
ValueDisplayedForObject = Browser("Browser_Encompass").Page("title:=.*").WebElement(objdesc).GetRoProperty("innertext")
ElseIf (ucase(ObjType) = "WEBLIST") Then
ValueDisplayedForObject = Browser("Browser_Encompass").Page("title:=.*").WebList(objdesc).GetRoProperty("value")
Else
ValueDisplayedForObject = Browser("Browser_Encompass").Page("title:=.*").WebEdit(objdesc).GetRoProperty("value")
End If
If (err.number<>0) Then
Reporter.ReportEvent micpass,"Value displayed for object",err.description
End If 

End Function.

 Please see below links for more on descriptive programming

qtp descriptive programming basics

descriptive programming examples and comparison with OR

Descriptive programming examples and comparison with Object Repository

Comparing Descriptive Programming with Object Repository


Suppose we record on Google page for testing and then click back for Search button, steps will be recorded in QTP as shown below. An object reposiory stores all the objects used in the recording. Issue with Object repository is that we need to associate object repository with test for execution and if the project size increase, it becomes difficult to maintain the repository.

Browser("Google").Page("Google").WebEdit("q").Set "TESTING"
Browser("Google").Page("Google").WebButton("btnG").Click
Browser("Google").Page("testing - Google Search").Link("Search").Click

Suppose we do not want to use objects from object repository, we can use descriptive programming to describe the objects.


Descriptive programming can be implemented in following two ways:

1. Using Static description:

Suppose we have to define description for 
Browser("Google").Page("Google").WebEdit("q").Set "TESTING"

We need to do the following:


1. Use object spy to identify the object in the page, mostly for html pages, html id is unique for the object, so will use html Id as property for the object.




Browser("title:=Google").Page("title:=Google").WebEdit("html id:=gbqfq").Set "TESTING":

Also in case of multiple object with similar properties, for example, multiple instances of browser, we can use ordinal identifier to identify the object

the above code using ordinal identifier and using regular expressions can be written as:

Browser("title:=.*","index:=0").Page("title:=Goog.*").WebEdit("html id:=gbqfq","index:=0").Set "TESTING":


2. Using Description Object


In descriptive programming, we can create description object and define the description of the object. For above code, we can create description object for each of the following:

Set objdescBR = description.Create
objdescBR("title").value ="Google"
Set objdescPg = description.Create
objdescPg("title").value ="Google"
Set objdescEdit = description.Create
objdescEdit("html id").value ="gbqfq"
objdescEdit("index").value =0

Browser(objdescBR).Page(objdescPg).WebEdit(objdescEdit).Set "TESTING1"

Description object can be very useful if we want to perform similar actions of multiples, for e.g checking multiple checkbox, providing same text in multiple edit box and so on. 

We will describe some examples on Descriptive Programming


1. Getting total number of links in the page:


Set objdescBR = description.Create
objdescBR("title").value ="Google"
Set objdescPg = description.Create
objdescPg("title").value ="Google"
Set objdesc = description.Create
objdesc("micclass").value ="Link"
Set objLinks = Browser(objdescBR).Page(objdescPg).childobjects(objDesc)
Msgbox objLinks.count

Similar to this example, we can fine multiple objects type in the page based on class of the object, Please give it a try for Images, and checkbox in the page


2. We can also like clicking on a link with a particular name or entering value in a particular edit box with code similar to as  below:


Call clickLink("Orkut")

Public Function clickLink(LinkName)
Set objdescBR = description.Create
objdescBR("title").value ="Google"
Set objdescPg = description.Create
objdescPg("title").value ="Google"
Set objdesc = description.Create
objdesc("micclass").value ="Link"
Set objLinks = Browser(objdescBR).Page(objdescPg).childobjects(objDesc)
For i = 0 to objLinks.count - 1
If (objLinks(i).getroproperty("name") = LinkName) then
objLinks(i).click
End If
Next
End Function