Sunday, July 31, 2011

Real Time Descriptive Programming

Descriptive Programming
Let’s get on with a simple program to understand this concept.
On Performing a Record function using QTP on a program (Assumption – Program static in Nature), The Quick test recognizes the appropriate object used and adds them to the repository.  On executing the quick test program, the application would execute without any errors because the QTP would use the Mandatory\Assistive properties to recognize the objects used in the program.
Assume a new Textbox object is displayed with Properties which are dynamically generated. For e.g. name property is generated dynamically based on the current date like ‘TextB_2007_01_01”. Now when this object is added to the repository, the possibility of QTP program working is very less.
The ideal solution is set to set the name property at runtime i.e. write a code and set the property of the variable.
 Assumption it is a webedit box
Dim curr_date
Curr_date = year (now) &”_”& Month (now) &"_"& Day (now)
‘Curr_date would contain the current date. For e.g. Curr_date = “2007_01_01”
 Browser (“…”).Page (“….”).Webedit (“Name: = TextB_” & Curr_date).Set “Hello”
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 syntax is:
TestObject("PropertyName1:=PropertyValue1","...","PropertyNameX:=PropertyValueX")
Why and when do we need Descriptive Programming
Possible reasons to use Descriptive Programming are
When the object properties used in the application are dynamic in nature i.e. to be more precise the properties used to recognize are not constant and requires special handling.
To restrict the object repository to a limited size to avoid performance execution issues.
When the application to tested is not developed completely for testing and designing of Automation scripts need to commence.
Also some certain scenarios, where the Object or the control cannot be recognized. This will be dealt in the forthcoming sections.
Things to do to understand Descriptive Programming
In order to exercise the benefits of descriptive programming, the user has to follow at least the minimum guidelines give below           
Understand the Object or control which requires descriptive programming.
This would revolve around understanding various properties of object and should answer questions such as what is the Class \ MICLASS property of the object? This property holds values such as whether it is Webedit or IMAGE or WEBLIST.
Can the Object be uniquely recognized with available properties such as Name, ID, HTML TAG?
Is the Object just required to recognize sub elements within object?
Assume a Web application named “DEMO” needs to be tested. The Application is designed such that browser title keeps changing based on the module.
Always the title has “DEMO:” as a Constant string followed by the module name.
Now in an Object Repository, instead of storing multiple Browser objects in the repository. The user can use regular expression to such as “DEMO : .*”.
This would help in recognizing the sub elements provided the user Checks the regular expression property.

If these are .NET custom controls, Refer the QTP Help.
What’s a Description Object?
This is a generic object which can be used to mould (i.e. programmatically configured) itself as an object in the application.
Assume a Webedit object needs to use to set data.
‘ Create a Generic description using the below syntax
Set EditDesc = Description.Create()
EditDesc("Name").Value = "userName"
EditDesc("Index").Value = "1"
Browser("…").Page("….").WebEdit(EditDesc).Set "MyName"
Let’s take this above example to a different level.
Various methods of using Description Object
Now you want to count the number of webedit objects in a Web table.
Here are going to use CHILDOBJECTS property of web table to solve.
‘Define you Webedit object using the properties you require.
Set editdesc=Description.create()
Edit(“micclass”).value=”Webedit”
Set ctr=Browser(“…”).Page(“…”).Webtable(“TABLE1”).Childobjects(editdesc)
‘Ctr object here is an array of objects where each object is similar to ‘description object
Msgbox ctr.count
To print the name of each element in this array we can do the following
For i = 0 to ubound(ctr)
            Msgbox Ctr(i).Name
Next
To skim through the properties of each webedit object in the array
Set editdesc=Description.create()
Edit(“micclass”).value=”Webedit”
Set wbtab_arr =Browser(“…”).Page(“…”).Webtable(“TABLE1”).Childobjects(editdesc)
For each obj1 in wbtab_arr
Temp_name = Obj1.Name
Temp_value = obj1.value
Next
To scan the properties of web object.
Let’s take webtable and proceed
Set wbtab_prop =Browser(“…”).Page(“…”).Webtable(“TABLE1”). GetTOProperties
            For k= 0 to wbtab_prop.count – 1
                        Msgbox Wbtab_prop(i).name
                        Msgbox wbtab_prop(i).value
            Next

Note: For Regular expression, use the wbtab_prop(i).regularexpression.
Tips and Tricks which can be used in a Description Programming
Navigating through each Cell in a Web table.
Webtabobj= Browser(…).Page(“….”).Webtable(“table1”).object
Rowcnt= Webtabobj.Rowcount
For i = 1 to Rowcnt
            For j = 1 to Webtabobj. ColumnCount(i)
                                    Msgbox Webtabobj.getcelldata (i,j)
            Next
Next

Setting value “TEST1” in all the webedit controls in a webtable
            Webtabobj= Browser(…).Page(“….”).Webtable(“table1”).object
Rowcnt= Webtabobj.Rowcount
For i = 1 to Rowcnt
                        For j = 1 to Webtabobj. ColumnCount(i)
                                                Set tempwebedit= webtabobj.chilitem(i,j,”Webedit”,0)
                                                Tempwebedit.set “TEST1”
                        Next
Next
Setting up property of object dynamically
We can use either use an environment variable or Set to property to perform this .
You can declare a environment variable in the QTP script.
            Environment(“Demotitle”)= “DEMO:.*”
            ‘Check the Regular expression Check Box if necessary
Using “Set to property” also we achieve this task.
            Syntax : object.SetTOProperty Property, Value

Removing property of an object dynamically
Syntax : object.Remove “Property”
e.g:
                                    ‘to delete the html tag property from the collection
                                     object.remove “html tag”

The following points will also be covered
Get all elements in HTML tag
Active element in webpage

 
 

No comments:

Post a Comment