Part 7: QTP and VBScript | Writing Sub Procedures & Functions in QTP
Many a times when you are writing your test scripts, you might encounter some lines of code that need to be executed multiple times during the course of the script. Rather than repeating the same code in your script multiple number of times, the common practice is to create a function for it and call the function repeatedly. Let’s see how you can do this in QTP. This article would cover the following concepts -
- – What are procedures in VBScript and how they are used
- – What are Subs in QTP
- – Examples of how you can create Subs in QTP
- – Examples on passing parameters in Subs
- – Functions in QTP and their features
- – Creating Functions in QTP and passing parameters to a Function
- – Difference between a Sub and a Function
- – Example on how to return a value from a Function
- – Passing parameters by Value and by Reference
Let’s get started with these concepts..
Procedures in QTP
The grouping of the lines of code to execute it repeatedly can be broadly classified as Procedures. In QTP, you have two different types of procedures – Sub Procedures and Functions. Both of them work the same way expect for some minor differences. We’ll cover both these types of procedures in the later part of the article.
Executing a Procedure in QTP
Any Procedure (Function/Sub) in VBScript has two main aspects which are mandatory for executing it in VBScript or for that matter any other programming language. These are -
- Function (Sub) Definition or Declaration: Function Definition is the actual code that you want to run as part of the function
- Function Call: This is a statement which executes your Function or Sub
Let’s see an example of this where we would create a function to find the sum of two numbers and display the result.
Sample Code 1: Structure of a Procedure (Function & Sub)
'Structure for Sub '==================== '1) Sub Definition - Actual Code Sub fnSum() var1 = 5 : var2 = 10 sum = var1 + var2 msgbox "The Sum of Numbers is: " & sum End Sub '2) Executing Sub Call fnSum() 'Call keyword is not mandatory to execute a Sub fnSum() 'Structure for Function '====================== '1) Function Definition - Actual Code Function fnSum() var1 = 5 : var2 = 10 sum = var1 + var2 msgbox "The Sum of Numbers is: " & sum End Function '2) Executing Function Call fnSum() 'Call keyword is not mandatory to execute a Function fnSum()
In the above example you can see that Call statement can also be used while calling a function/sub. To know more about Call statement you can check the difference between Call statement and normal function call.
Subs in QTP
A Sub Procedure in QTP -
- - is a collection of statements that are enclosed between Sub and End Sub statements.
- - can be used both with and without parameters.
- - does not return any value.
Passing Parameters in a Sub Procedure
Consider a situation where you want to run the same set of code multiple times but with different set of data. In such a case, you can pass the data to your Sub in the form of parameters. This way you can make the code generic which can work with multiple set of data. Let’s see an example for the same.
Sample Code 2: Using Parameters in a Sub
Dim v1, v2 v1=5 : v2=10 fnSum v1, v2 'Passing parameters using variables fnSum 10, 20 'Passing parameters as literals fnSum v1+10, v2*2 'Passing parameters as expression 'Sub Definition Sub fnSum(var1, var2) sum = var1 + var2 msgbox "The sum of numbers is: " & sum End Sub
Functions in QTP
A Function in QTP -
- - is a set of statements that are enclosed between Function and End Function statements.
- - can be used with and without parameters
- - can return a value.
The difference between a function and a sub is that a function can return a value, but a sub can’t return any value.
Passing Parameters in a Function
You can pass parameters in a function the same way it’s done for a sub. Let’s see an example for this.
Sample Code 3: Using Parameters in a Function
Dim v1, v2 v1=5 : v2=10 fnSum v1, v2 'Passing parameters using variables fnSum 10, 20 'Passing parameters as literals fnSum v1+10, v2*2 'Passing parameters as expressions 'Function Definition Function fnSum(var1, var2) sum = var1 + var2 msgbox "The sum of numbers is: " & sum End Function
Returning a value from a Function in QTP
One additional advantage that Functions have over Sub is that a Function can return a value. To return a value from a function, we need to take care of the following two things -
- 1) To return a value from a function, you need to use the statement functionName = ReturnValue, where functionName is the actual name of the function and ReturnValue is the value you want to return.
- 2) To capture the returned value, you need to use the statement someVariable = functionName() while calling the function.
Let’s understand this with the help of an example.
Sample Code 4: Returning value from a Function
Dim result 'variable that will capture the result result = fnSum(10, 20) 'parameters should be passed using parenthesis when the function returns a value msgbox "The sum of the numbers is: " & result 'Function Definition Function fnSum(var1, var2) sum = var1 + var2 'return the result fnSum = sum End Function
Passing Parameters to a Function/Sub – Pass By Value & Pass By Reference
You can pass parameters to a function or sub procedure by value or by reference. Let’s see what both these terms mean.
Passing Parameters by Value (byVal). With this way of passing parameters, only a copy of the original parameters is passed. This means that whatever modifications we make to the parameters inside the function, it doesn’t affect the original parameters.
Sample Code 5: Passing parameters by value to a function
Dim val val=5 'Function Call fnFunc val msgbox "Original Value: " & val 'msgbox displays value 5 'Function Definition Function fnFunc(byVal val) val = val + 2 msgbox "New Value: " & val 'msgbox displays value 7 End Function
In the above example you would see that the new value get changed to 7 but it doesn’t get reflected to the original value which still shows the value as 5 only.
Passing Parameters by Reference (byRef). In this case, the reference of the original value is passed as the parameter to the function. Therefore, whatever change is done to the parameter inside the function, the same is reflected in the original parameter also. By default, values are passed by reference in a function. i.e., even if you don’t use byRef keyword, the parameters are passed by reference.
Sample Code 6: Passing parameters by reference to a function
Dim val val=5 'Function Call fnFunc val msgbox "Original Value: " & val 'msgbox displays value 7 'Function Definition Function fnFunc(ByRef val) val = val + 2 msgbox "New Value: " & val 'msgbox displays value 7 End Function
Since the original parameter is passed as reference to the function, both the original and new value has the updated value 7.
This was all about Functions and Sub Procedures in QTP. Please feel free to provide your feedback for this article and the entire QTP VBScript tutorial series. You can also leave a comment if you would like any other details to be covered here.