Thursday, June 3, 2010

Reuse Your QTP Code

In this article, I am going to explain you how can we reuse the code which is written for web application.
In the below class, I have written function fnc_set to set value in a WebEdit.

Class FirstClass
Private VarBrowserObj
Private VarWebEditObj
Private VarSetValue
'**********Let Property*****************************************
Public Property Let fnc_VarSetValue(val)
VarSetValue=val
End Property
'**********Set Property****************************************
Public Property Set fnc_VarBrowserObj(val)
Set VarBrowserObj=val
End Property

Public Property Set fnc_VarWebEditObj(val)
Set VarWebEditObj=val
End Property
'**************Funtions and Procedures**********************
Public Function fnc_set
Browser(VarBrowserObj).WebEdit(VarWebEditObj).Set VarSetValue
End Function
End Class

Here, I am creating an object for the above class and setting up values and objects for class properties.

Public objClass
'Creating object for class
Set objClass=new FirstClass

'Calling Let to assign value for VarSetValue property
objClass.fnc_VarSetValue="QTP"


'Calling Set property method for Browser object
Dim objBrowser:Set objBrowser=Description.Create()
objBrowser("name").Value="Google"
Set objClass.fnc_VarBrowserObj=objBrowser


'Calling Set property method for WebEdit object
Dim objWebEdit:Set objWebEdit=Description.Create()
objWebEdit("name").Value="q"
Set objClass.fnc_VarWebEditObj=objWebEdit

'Calling fnc_set function for Web operation
objClass.fnc_set

You may think that the above class can be used only for web application. But we are able to use for other application also.

'Calling Let to assign value for VarSetValue property
objClass.fnc_VarSetValue="mercury"

'Calling Set property method for Dialog object
Dim objDialog:Set objDialog=Description.Create()
objDialog("text").Value="Login"
objDialog("micclass").Value="Dialog"'Here is the trick, you need to inlcude micclass property along with main property.
Set objClass.fnc_VarBrowserObj=objDialog

'Calling Set property method for WinEdit object
Dim objWinEdit:Set objWinEdit=Description.Create()
objWinEdit("attached text").Value="Agent Name:"
objWinEdit("micclass").Value="WinEdit"
Set objClass.fnc_VarWebEditObj=objWinEdit

objClass.fnc_set 'Setting value in winedit with same function.

No comments:

Post a Comment