Descriptive programming in UFT
Descriptive programming provides information to QTP/UFT bypassing object repository. The same object information is passed to QTP/UFT so that it can identify the object.
Let us think about a person whom we don’t know. When we meet for the first time, we create an image of that person. Like the name, tentative age, gender, height, if the person wears specs or not, etc. For us to remember that persons these are enough. we will be able to identify the person in our next meet by using our stored properties of that person.
These are mandatory properties. There are some other things we can notice. We may also notice the shirt color, trouser colour, some more details. They are assistive properties. Help to identify the person uniquely. But they may change.
What is Descriptive Programming in UFT??
Likewise, if we need to identify a textbox, the first thing we will be looking at is the name or id. This will help me to identify the textbox. In this method of script creation, we no need to have Object Repositories.
But assume that we have a special application that is dynamic. So with the stored object description QTP/UFT will not be able to identify the objects.
Say after logging we have a welcome message says Welcome <<UserName>>. Upon clicking on the username it will navigate to the actual page.
This will work fine if the same user logs in but fails when another user logs in as the stored object description does not match to the application object. So we need to find another way of coding.This is called Descriptive Programming.
In the real world, if the person says I want to meet you and I will be seating some coffee hose, table number-2. I will be in a redshirt. Then we will not try to find the person, directly go to that coffee house and go to table no-2. Now if I see a person with a red shirt, we will get the person. Is not it??
So, in this method, we provide information about the object to QTP and request it to perform search operations and carry out tasks.
In a real project, if the GUI is not available or the project size is getting bigger(OR will also bigger), we use descriptive programming.
When to use descriptive programming?
Apart from the above reason, there are few other situations when we need to opt for descriptive programming. They are as follows:
- Our object repository gets huge due to a large project. And larger the OR in size, it will impact the execution performance. Now to reduce dependency on OR, we can go for descriptive programming.
- The objects in the specific application are dynamic 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 you don ‘ t want to use object repository at all. 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 a webpage or an application is still not developed, that time, it is not possible to add the object directly from the application or UAT. In that case, we can still descriptive programming to generate our code by seeing the technical specification document.
- Say we need to verify a table data. There are n numbers of data to verify. These n data are divided into say 20 pages. Each page contains the Next, the Previous button. So for 20 pages, there will be 20*2=40 button objects created. To avoid such huge object creation, we can use descriptive programming.
- If there are n numbers of textboxes present inside a UAT. in that case only value changing is the index of the textboxes. Say for first text box the name is text_1 and for second textbox the name of the textbox is text_2 and so on. In such a condition, adding them to an object repository is not a feasible option. Descriptive programming can be really helpful.
- If the same page of UAT is getting changed due to customer specification and being an automation engineer we can insert all possible variations into different object repositories. Now by coding, we can load and unload the shared object repository based on the customer scenario. But this changing may impact long execution. To minimize this risk, we can go for descriptive programming.
Advantages of Descriptive programming:
- Definitely descriptive Programming based Test scripts are quicker in execution than Repository based Test scripts.
- Scripts are actually portable. It is like to write once run anywhere (we can run these scripts from any other windows machine easily).
- Maintenance is easy (less amount of resources).
- We can start the Test Execution process even though the Application is under development.
Descriptive programming is two types.
1. Static Programming-By describing the form of the string arguments.
2. Dynamic Programming-By creating properties collection object for the description.
In this style of script generation, we provide objects information directly into the script.
Invokeapplication "C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe" dialog("text:=Login").Activate dialog("text:=Login").Winedit("attached text:=Agent Name:").Set "techtravelhub" dialog("text:=Login").Winedit("attached text:=Password:").Set "mercury" dialog("text:=Login").Winbutton("text:=OK").Click
1. Dialog, WinEdit, and WinButton – Test Objects
2. text, attached text – Property names
3. Login, Agent Name: Password: OK – Property values or Logical Names of the Object
4. Activate, Set, Set secureSetsecure, Click – Methods
If we feel one property information is not sufficient for recognizing the object uniquely, then we can provide more properties information by separating with commas.
If we want to get objects information (Test objects, properties, and values), we can use object spy features. This feature is available in Tools Menu, in the local repository, and repository manager.
If we want to maintain ‘Objects information’ in a centralized location then we can use Constants.
Creating similar Constants like:
Const Login="text:=Login", Agent="attached text:=Agent Name:" Const Pwd ="attached text:=Password:", Ok="text:=OK"
Note: we can declare no of Constants in a line by separating with Camas (,), if we take another line then we have to use Const Statement again.
Creating a Library file
Place Constants in Notepad and save as .vbs file
Associate the Library file to QTP (File->Settings->Resources-> Click add (+) icon-> Browse path of the Library file->Click Apply and click Ok buttons
Otherwise, we can load the library file during run-time
execute file “Path of the Library file(.vbs)”
After that create the Test Script using Constants
Creating the Test Script using Constants:
Invokeapplication "C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe" Dialog("Login").Activate Dialog("Login").Winedit("Agent").Set "asdf" Dialog("Login").Winedit("Pwd").Set "mercury" Dialog("Login").Winbutton("Ok").Click
we can describe an object directly in a statement by specifying a property:=value pairs describing the object instead of specifying an object’s name. The general syntax is:
TestObject("PropertyName_1:=PropertyValue_1", "... ", " PropertyName_X:=PropertyValue_X")
- TestObject – the test object class could be WebButton, WebRadioGroup, WebButton etc..
- PropertyName:=PropertyValue- the actual UFT identified test object property and its corresponding value.
Each property:=value pair must be by commas separated and inside quotation marks. Note that we can enter a variable name as the property value if we want to find an object based on property values we retrieve during a run session.
Consider the following example
<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", "htmltag: =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", "htmltag: =INPUT"). set "Test"
If we refer to them as a web element then we will have to distinguish between the two objects using the index property
Browser("Browser").Page("Page").WebElement("Name:=txt _ Name", "htmltag: = INPUT ", " Index: = 0 ").set " Test " //Refers to the textbox Browser("Browser").Page("Page").WebElement("Name:=txt _ Name", "htmltag: = INPUT ", " Index: = 1 ").set " Test " //Refers to the radio button
If we maintain Object Information in the centralized location, then we can handle modifications easily.
In this style of script generation, first, we create description objects, provide properties information and use description objects in the test script. To use this method we need first to create an empty description. Then add filters accordingly.
When we use a property name for the first time the property is added to the collection and when we use it again the property is modified. By default, each property is defined as a regular expression.
Creating Properties Collection Objects
Set oLogin=description.Create Set oAgent=description.Create Set oPassword=description.Create Set oOk=description.Create //Entering Properties Information into Objects oLogin("text").value="Login" oLogin("width").value=320 oLogin("height").value=204 oAgent("attached text").value="Agent Name:" oPassword("attached text").value="Password:" oOk("text").value="OK" //Generating Tests using Properties collection Objects Invokeapplication "C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe" Dialog(oLogin).Activate Dialog(oLogin).Winedit(oAgent).Set "asdf" Dialog(oLogin).Winedit(oPassword).Set "mercury" Dialog(oLogin).Winbutton(oOK).Click
Note1: Create Description of the required objects and put into the specific library file, by associating that library file, we can generate tests.
Note2: Dynamic programming is some difficult in preparation than static programming but maintenance is very easy.
In this style of script creation also, we can maintain Objects information in the Centralized location by putting collection objects in a Library file.
How to get child objects by using description?
Set objDesc=Description.Create objDesc("htmltag").value="input" objDesc("name").value="abc" Set allChilds=Browser("Browser").Page("Page").ChildObjects(objDesc) for each child in allChilds child.set "Test" Next //or for i=0 to allChilds.count-1 allChilds(i).set "Test" Next
Descriptive Programming is an advanced concept of QTP coding. Testers can handle the application without using the OR by implementing Descriptive programming.
Can we add a description dynamically?
Yes, we can add multiple descriptions dynamically using code. In the above code, the snippet
objDesc.count will return how many filters we are using in the collection. we can also dynamically add and delete descriptions (filters) to the collection.
Set objDesc=Description.Create //for addition objDesc.add("htmltag","input") objDesc.add("name","abc") //for deletion objDesc.delete("htmltag")
How do I check if the specif property exists or not in the collection?
The answer is that it’s not possible. Because whenever we try to access a property that is not defined it’s automatically added to the collection. The only way to determine is to check its value that is use an if statement.
if obj.ChkDesc ("htmltag").value = empty then
- They are reusable
- Modification of the script becomes easy in compare to QTP Code
- This is very useful when applications are not available
- It is flexible for the high performance of the application.
- This is a very speedy technique for execution
- It can effectively handle the object implementation problem of an application.
Disadvantages: If the property values change, it is necessary to upgrade the description. So maintenance is an issue for descriptive programming.
Few Common Example of Descriptive Programming:
How To Count The Numbers of Links On A Page and click on a link in QTP/UFT?
This is a very common scenario when automating a webpage. Say on a webpage I need to check if a particular link is available or not. Or there might be a scenario when you need to click on a particular link which is generating dynamically.
If it is static then you can record and do. But which is dynamic, for them we need to implement artificial intelligence. Before we go ahead and code…Let us think about the logic. Logic Well, when we write a code for a link we use to write like this……
< a href="xyz.com">Link Name1</a> <a href="deb.com">Link Name2</a>
so through QTP if we can read the elements of a page and check if an “A” tag is there in the page element it will be surely a link. Isn’t it??
So, all the links look the same. And they follow the same pattern. This is a small piece of code which will look like-
SystemUtil.Run http://www.yahoo.com/ Set aa=description.Create aa("htmltag").value="A" // This type of coding is called descriptive programming. set bb=Browser("name:=Fun.*").Page("title:=Fun.*").childobjects(aa) cc=bb.count msgbox cc
There is another way to find out the count of the link
Set oDesc = Description.Create() oDesc("micclass").Value = "Link" //here we are filtering the object oDesc by its default class micclass. micclass of a link is "Link" Set Links = Browser("Browser").Page("Google").ChildObjects(oDesc) NumberOfLinks = Links.Count() Msgbox NumberOfLinks
even, if, you need to click on a specific link you can modify the script little bit..say I have a link called yahoo.com and I need to click on the mail link.
Logic is again same …find out all the links
Now the extra is that we need to check if the link name is “Mail”. If so generate a click event.
SystemUtil.Run http://www.yahoo.com/ Set aa=description.Create aa("htmltag").value="A" 'either specifying here -------------------------------- aa("name").value="Mail" ------------------------------- set bb=Browser("name:=Fun.*").Page("title:=Fun.*").childobjects(aa) cc=bb.count for i=0 to cc-1 if bb(i).getROProperty("name")="Mail" then//link name bb(i).click exit for end if next
How to check all checkboxes on a webpage/window in UFT or QTP?
Set a=Description.Create a("html tag").value="input" a("type").value="checkbox" Set b=Browser("name:=").Page("title:=").childobjects(a) c=b.count msgbox c For i=0 to c-1 b(i).set "on" Next
How to Close All Browser from QTP/UFT Code?
Most of the time I have seen that we need to close Internet Explorer while testing through QTP. Internet Explorer means all the pop-ups, the main page. Say I am testing an application which needs to navigate different URL. So what we generally do is to open a browser do what we want to do and close it for the next set of a task may be in different URL.
A common test case maybe
1. Open IE and navigate to your HR link to create an employee.
2. After the creation of the employee, you need to close that and open another browser to register him to yahoo groups.
3. Now you need to open some websites called life insurance..fill up the data and employee number which is generated in Step-1.
4. Get the corresponding Policy number and again open the HR link to update employee information.
For such cases to avoid the problem of multiple IEs we use to open a URL perform some task and close it after saving it.
Here is a small code that will assist you to close an IE from QTP.
Sub CloseAllBrowsers() Systemutil.CloseProcessByName("IEXPLORE.exe") wait(5) End Sub
Now you can do a further enhancement on this.
Say I have opened a meeting window and want to show my running window to our client or my seniors or there might be cases when I might open a test management tool like QC which runs on IE and want to run the test. So every time I will not be interested to open QC page.
This is a small modification on the code which will be helpful for such scenarios.
On Error Resume Next Dim intWndCnt Dim oDesc, oWnd 'Create Object description Set oDesc = Description.Create ' Set oWnd = Desktop.ChildObjects(oDesc) intWndCnt = oWnd.Count For i = 0 to intWndCnt - 1 Set TmpObj = oWnd.item(i) strTmpTitle = oWnd.item(i).GetROProperty("Text") If instr(1,strTmpTitle, "Microsoft Internet Explorer",1) > 0 Then If instr(1,strTmpTitle, "Mercury Quality Center",1) > 0 Then 'msgbox "Title :" & oWnd.item(i).GetROProperty("Text") Else 'msgbox "Close : & oWnd.item(i).GetROProperty("Text") oWnd.item(i).close End if End If Next 'Clean up Set oDesc = nothing Set oWnd = nothing
How to Calculate Number of Opened Browsers in QTP/UFT?
Function Browser_Count() Set objBrowser =description.create() objBrowser("micclass").value="Browser" Set sBrowsers=desktop.childobjects(objBrowser) ibCount=sBrowsers.count If ibCount<>0 Then Browser_Count=ibCount Else Browser_Count=1 End if Set objBrowser=Nothing Set sBrowsers=Nothing End Function
How to Close Oracle Form in QTP/UFT??
This is one more situation that arises very common to an ERP test engineer. Most of the time we rely on QTP’s record feature. But this is a scenario that occurs all most in every window of Oracle ERP Testing.
So instead of doing record playback every time, I tried to use some descriptive approach to make it a general case. So this can be treated as a common function across the scenario. This is code rather sub/procedure where we need to pass the form name as a parameter and this will close the form.
Sub ERP_CloseForm(sFrmName) If OracleFormWindow("short title:="& sFrmName).Exist Then OracleFormWindow("short title:="& sFrmName).CloseForm End If End Sub
How to Get Browser Title in QTP or UFT?
Function GetBrowserTitle() set sbrowser=Description.create() ' we are creating a blank object named as sbrowser. sbrowser("micclass").value="Browser" ' we are assigning the sbrowser object as "Browser set obj=Desktop.childObjects(sbrowser) icount=obj.count if(icount<>0) then GetBrowserTitle=Browser("creationtime:="&icount-1).GetROProperty("title") end if Set sbrowser=Nothing set obj=Nothing End Function
How to Count Number of open Browsers in Desktop Using QTP/UFT?
This is another important concept in Descriptive Programming in UFT.
For this the concepts we will be using are:
2. Descriptive programming
with ChildObejct we will see how many objects are present under desktop as a child.
And with the description, we will determine if the child matches our description
This is the function that returns the no of browser count.
Function count_Browser() Dim objDesc ‘As Description Dim objBrowsers ‘As Collection Set objDesc = Description.Create objDesc(“micclass”).Value = “Browser” Set objBrowsers = Desktop.ChildObjects(objDesc) for l_BrowserIndex = 0 to objBrowsers.Count - 1 count_Browser=objBrowsers.Count 'returns no of browser strTemp = (l_BrowserIndex).GetROProperty("hwnd") strTemp = (l_BrowserIndex).GetROProperty("title") Set objBrowser = Nothing Next count_Browser=strTemp 'returns the title Set objBrowsers = Nothing Set objDesc = Nothing End function
When we use this function:
1. When we need to understand how many browsers present.
2. If we have to do certain operations with any of the browsers. Like- let the first browser be open but second needs to be closed.
Code to Close a particular Browser:
Sub CloseBrowser(iHwnd) if browser("hwnd:="&iHwnd).Exist(3) then browser("hwnd:="&iHwnd).close end if end Function call CloseBrowser iHwnd
How to work with Editbox in UFT/QTP via descriptive programming?
Below is the Code related to EditBox.Frequently we need to get data from an edit box that is under a webpage and set the data to the box. This is another important concept in Descriptive Programming in UFT.
Below is the very popular code to Set and get data from the webpage.
You may create an object up to where it is necessary. (For more help …go to OR and check the hierarchy)
objname=Name of the edit box.
How to enter a value in the edit box
'FUNCTION : setWebEdit 'ARGUMENTS : obj,objname,entervalue 'DESCRIPTION : Enters value into webedit Function setWebEdit(obj,objname,sValue) If obj.WebEdit(objname).exist Then If Not obj.WebEdit(objname).GetROProperty("disabled") Then obj.WebEdit(objname).click obj.WebEdit(objname).Set sValue End If End If End Function
get properties of the edit box:
'FUNCTION : getWebEdit 'ARGUMENTS : obj,objname 'DESCRIPTION : Gets particular webedit's value 'RETURNS : value of webedit '************************************************************************************************************** Function getWebEdit(obj,objname) If obj.WebEdit(objname).Exist(5) Then If Not obj.WebEdit(objname).GetROProperty("disabled") Then obj.WebEdit(objname).click getWebEdit = obj.WebEdit(objname).GetROProperty("value") End If End If End Function
How to make the full screen a browser in UFT/QTP?
This small function will maximize the Browser.pls do an object spy and check the handiHwnd of the browser.This is another important concept in Descriptive Programming in UFT.
call maximize (iHwnd) Sub maximize(iHwnd) Browser("hwnd:="&iHwnd).Fullscreen End Sub
How to get Tooltip in UFT/QTP
I was in a project where there was a peculiar requirement to capture tooltips through a script. I got several requests also to write a code that will capture the tooltips. I tried with Google!! I got a very nice thread of motevich.
I found this is a great solution. This also solves the problem of capturing the alt property of the Image. Here is a solution for capturing Tooltips of image. Even in my project, I have used this code only.This is another important concept in Descriptive Programming in UFT.
Dim descImage, listImages, attrAltText, attrSrcText Browser("Main Page - Wikipedia,").Sync Browser("Main Page - Wikipedia,").Page("Main Page - Wikipedia,").Sync ' Create description for all images on a Web page. ' For that we use "html tag" property and its value "IMG" Set descImage = Description.Create descImage("html tag").value = "IMG" ' Get all images which match the above description Set listImages = Browser("Main Page - Wikipedia,").Page("Main Page - Wikipedia,").ChildObjects(descImage) ' Check tool tips of images For i = 0 To listImages.Count - 1 attrAltText = listImages(i).GetROProperty("alt") attrSrcText = listImages(i).GetROProperty("src") If attrAltText <> "" Then MsgBox "Image src: " & attrSrcText & vbNewLine & "Tool tip: " & attrAltText End If Next
How to Count, how many Buttons and Edit boxes available in Flight Reservation main window.
This code snipped is a mixture of OR and Descriptive programming. Till the navigation and reaching the main window, I have used OR and then I have used Descriptive programming. This is another important concept in Descriptive Programming in UFT.
Note: We can use a mixture of Descriptive and OR supported coding. But if we have started putting a description in the hierarchy, from then the OR support gets expired, we need to continue using Descriptive way to figure out the child objects.
If Not window("Flight Reservation").Exist (2) Then SystemUtil.Run "C:Program FilesMercury InteractiveQuickTest Professionalsamplesflightappflight4a.exe" Dialog("Login").Activate Dialog("Login").WinEdit("Agent Name:").Set "bcroy" Dialog("Login").WinEdit("Password:").Set "mercury" Dialog("Login").WinButton("OK").Click End If Set oDesc = Description.Create() oDesc("micclass").Value = "WinButton" Set Buttons = Window("text:=Flight Reservation").ChildObjects (oDesc) Num_Buttons = Buttons.Count() Set oDesc1=Description.Create() oDesc1("micclass").Value="WinEdit" Set Editboxes=Window("text:=Flight Reservation").ChildObjects (oDesc1) Num_Editboxes= editboxes.count () sum= Num_Buttons+Num_Editboxes Reporter.ReportEvent 2, "Res","Total Buttons: "& Num_Buttons &"Total Edit boxes: "& Num_Editboxes
How to Get Oracle ERP Status bar Message in QTP/UFT?
Function ERP_Get_StatusMessage() Set obj_ORAStatus=description.Create If oraclestatusline(obj_ORAStatus).exist Then ERP_Get_StatusMessage=OracleStatusline(obj_ORAStatus).getROProperty("message") End If Set obj_ORAStatus=nothing End Function
Troubleshoot of Descriptive programming
This is another important concept in Descriptive Programming in UFT.
Sometimes there may be two textboxes with the same name.
<input type="text" name="abc"> <input type="text" name="abc">
In that case we can code like: In this case, an index can be included in the collection to distinguish between these two objects
Set objDesc=Description.Create objDesc("htmltag").value="input" objDesc("name").value="abc" objDesc("index").value="0"
look at the code:
<input type="text" name="abc"> <input type="radio" name="abc">
In this case, even though the name is the same for two different objects, so regular coding will not help much. By using description we will be able to identify the objects properly.
Set objDesc=Description.Create objDesc("htmltag").value="input" objDesc("name").value="abc" Browser("Browser").Page("Page").WebEdit(objDesc).set "xyz" Browser("Browser").Page("Page").WebRadio(objDesc).click
Rule of thumb for description Programming
In the hierarchy of the page, if we are free to make any object as descriptive then the following child objects need to be descriptive in the same way. Otherwise, the mixture of the object repository will create confusion for UFT/QTP resulting in UFT or QTP will not be able to identify the object. This is another important concept in Descriptive Programming in UFT.
Browser("Browser").Page("Page").WebEdit(objDesc).set "xyz" // allowed Browser("Browser").Page(pageDescription).WebEdit(objDesc).set "xyz" // allowed Browser("Browser").Page(pageDescription).WebEdit("Test").set "xyz" // not allowed Browser(browserDescription).Page(pageDescription).WebEdit("Test").set "xyz" // not allowed
When using programmatic descriptions from a specific point within a test hierarchy we must continue to use programmatic descriptions from that point onward within the same statement. If we specify a test object by its object repository name after other objects in the hierarchy have been described using programmatic descriptions, UFT/QTP can not identify the object.
This link will also help you to understand how to automate the network-related tasks.