keyword driven scripts :
Function that reads the
excel sheet and loads the cell values into Global Variables. We are using
global variables because the variables are being used in multiple functions.
(To read more about global variables, you can read – Different ways to define
Global Variables in QTP)
'========================================
'
Function Name - fnReadExcel
'
Purpose - This function loads the excel sheet into QTP data table and stores
the cell values in Global Variables
'========================================
Function fnReadExcel(sSheetName)
'Add
the Data Sheet into QTP Data Table
DataTable.AddSheet("dtSheet")
'Import
the Excel Sheet into QTP Data Table
DataTable.ImportSheet
sExcelLocation, sSheetName, "dtSheet"
'Loop
through all the rows in the Data Sheet
iRow
= DataTable.GetSheet("dtSheet").GetRowCount
For iR
= 1 to iRow
'Set
the Current Row in the Data Sheet according to the loop counter
DataTable.SetCurrentRow
iR
'Capture
all the cell values in different variables
sParent
= DataTable("Parent", "dtSheet")
sParentProp
= DataTable("PProperty", "dtSheet")
sControl
= DataTable("Control", "dtSheet")
sControlProp
= DataTable("CProperty", "dtSheet")
sAction
= DataTable("Action", "dtSheet")
sDataValue
= DataTable("Data", "dtSheet")
'Call
the function that will convert the excel data into QTP readable format
If sParent
= "End" Then
Exit For
ElseIf sParent
<> "" Then
fnIdentifyParentHierarchy()
Else
'The
action is independent of the all the controls (refer NOTE 2 from the
article)
fnAction()
End If
Next
End Function
'=============
End Function ===============
2.The below function converts values from columns A and B of the excel
sheet into QTP readable format.
========================================
'
Function Name - fnIdentifyParentHierarchy
'
Purpose - This function converts the values in cells A and B into QTP readable
format
'========================================
Function fnIdentifyParentHierarchy()
'Split
Parent Property so that multiple objects casn be resolved
arrParentProp
= Split(sParentProp, ",")
'Resolve
the hierarchy of all the objects that are parent to the actual control
Select Case sParent
Case "Dialog"
Set obj_hierarchy
= Dialog(arrParentProp(0))
Case "Window"
Set obj_hierarchy
= Window(arrParentProp(0))
End Select
'Call
the function that will resolve the Control Object
fnIdentifyControl()
End Function
'=============
End Function ===============
3) This function converts data from cell C and D in QTP readable format.
Later, it combines values in Cells A, B, C and D to form one complete object on
which the action will be performed.
'========================================
'
Function Name - fnIdentifyControl
'
Purpose - This function converts the values in cells C and D into QTP readable
format and then combines values from Cells A, B, C and D to come up with a
single object
'========================================
Function fnIdentifyControl()
'Split
the Control Property
If sControl
<> "" Then
arrControlProp
= Split(sControlProp, ":=")
'Resolve
the Control object to obtain the complete heirarchy on which the action can be
performed
Set child_object
= Description.Create()
child_object("micclass").value
= sControl
child_object(arrControlProp(0)).value
= arrControlProp(1)
'Create
the object on which the action will be performed
Set control_object
= obj_hierarchy.ChildObjects(child_object)
Else
'Control
Object is the parent hierarchy on which the action will be performed (refer
NOTE 1 from the article)
Set control_object
= obj_hierarchy
End If
'Call
the function that will perform the necessary action on the object
fnAction()
End Function
'=============
End Function ===============
4) The below function performs Actions or Operations on the object which is
retrieved from the previous function.
'========================================
'
Function Name - fnAction
'
Purpose - This function performs action on the object based upon the defined
keyword
'========================================
Function fnAction()
'Perform
Action on the control_object based upon the keyword defined in the excel sheet
Select Case sAction
Case "SetValue"
control_object(0).Set sDataValue
Case "Click"
control_object(0).Click
Case "WindowClose"
control_object.Close
Case "Run"
SystemUtil.Run
sDataValue
End Select
End Function
'=============
End Function ===============
2 Different Ways to Create Global
Variables in QTP
Did you ever face any situation where you needed to create
global variables in QTP so that these global values can be used across multiple
actions/functions within your test script? How did you tackle the problem then?
If you would have noticed, QTP doesn’t provide any direct method to create
global variables. But you can use any of the below 2 methods to create
variables with Global Scope in QTP.
By using Environment Variables
This is the most commonly used method to create variables in
QTP with global scope. All you have to do is just create a User-Defined
Environment variable anywhere in your script (be it at the beginning of your
action, or even inside any of your functions). That’s all. Once your
environment variable is created, it would be available throughout your test
script (inside all your actions & functions within the test script).
Example:
'Declare Environment Variable (Global Variable)
Environment.Value("gloVarBlogName") =
"Automation Repository"
'Use Environment Variable (Global Variable)
msgbox Environment.Value("gloVarBlogName")
By using Function Library
You can also use a function library to create and use
variables with global scope in your QTP script. This is how it can be done.
1. Create a function library(.vbs file) and associate it to
your test script.
2. In your function library, declare a variable & assign
required value to it. Example:
'Declare variable in Function Library
Dim gloVarBlogName
gloVarBlogName = "Automation Repository"
Once you have defined the variable in the function library
& associated the function library with your test script, you can then use
this variable anywhere in your script. For Example, writing msgbox
gloVarBlogName in your action would display a message box with text –
Automation Repository
Other than the above two methods you can also store your
variables in some external file (such as excel sheet, text file, xml file etc) and then you can write code to
access these variable values from the external file. Usually this method is not
used much because anyways you can get the job done with environment variables
& function library with much less code.
Thanks for the useful information.
ReplyDeleteCan you please explain how to convert Input Parameters (e.g. in Excel Data sheet) into Environment Variables?
So that these parameters can be accessed Globally.
Really good information to show through this blog. I really appreciate you for all the valuable information that you are providing us through your blog.
ReplyDeletePen testing services
Vulnerability assessment services
Load Testing Services
Mobile app testing services