Introduction:
This
document is useful for creating functions
and sub-procedures in keyword driven QTP Testing.This illustrates the basic
differences between the Fuctions and Sub-Procedures.It also explains how to
associate procedures to QTP Tests.
Difference between
Functions and Sub Procedures:
In QTP there are 2 types of procedures:
·
Sub Procedures
·
Function Procedures
Sub Procedure:
A
sub procedure is a series of VBScript statements, enclosed by Sub and End Sub
statements which perform actions but do not return a value. A sub procedure can
take arguments. If a sub procedure doesn’t receive any arguments, its Sub
statement must include an empty parenthesis ().
Sub ConvertTemp()
temp =
InputBox("Please enter the temperature in degrees F.", 1)
MsgBox "The
temperature is "& Celsius(temp) & " degrees C."
End Sub
|
Function
Procedure:
A
function procedure is a series of VBScript statements enclosed by the Function
and End Function statements. A function procedure is similar to a sub procedure
but it can return value to the calling function. A function procedure can take
arguments (constants, variables or expressions that are passed to it by a
calling procedure). If a function procedure has no arguments, it Function
statement must include an empty set of parenthesis. A function returns a value
by assigning a value to its name in one or more statements of the procedure.
Since VBScript has only one base data type, a function always returns a
variant.
Function
Celsius(fDegrees)
Celsius
= (fDegrees - 32) * 5 / 9
End
Function
|
So
to get data out of a procedure a Function is used. Since Function can return a
value while a Sub cannot.
The
following example shows two calls to the procedure. One uses the Call method,
while the other doesn’t.
Call
ConvertTemp()
ConvertTemp
Note: The Parentheses
are omitted when the Call statement is not being used.
Associating
Procedures to QTP tests:
Followings
are the steps that need to be performed for associating Procedures (Sub or
Function) to a QTP Test:
1. First save the created
Function or Sub as a Function Library file (*.qfl , *.vbs , *.txt)
2. Then Open the Test to
which the Function has to be associated with.
3. Then Go to File
>>Settings >>Resources

4. Click on the + sign to
add the to be associated Function Library file to the test.

5. Then browse for the
function libraries.

6. Select the requisite
function and associate the same to the Test.

7. Then Use the function in
the test wherever required.



0 comments :
Post a Comment