Sunday, February 6, 2011

VBScript for QTP Functions

Functions VBScript provides two different types of procedures.
§                    Function. A procedure that executes and then returns a result to the statement that called it.
§                    Subroutine. A procedure that executes but does not return a result.

Functions

VBScript functions are collections of related VBScript statements that are called and executed as a unit. Functions can also return a result to calling statements. The syntax for creating a function is
[Public | Private] Function name [(arglist)]  statements End Function
Private and Public are optional keywords. The Private keyword is used to set up a function that can only be called from within the script where it has been defined. When used, the Public keyword allows the function to be called by other scripts. Name identifies the function’s name. Arglist is used to specify one or more arguments that that can be passed to the function.To return a result to the statement that called the functions you must be sure that the subroutine has a variable of the same name as the subroutine. The value that the subroutine assigned to this variable is what will be returned.The following example shows a WSH VBScript that has two functions.
' * Script Name:   Script 3.vbsOption Explicit
Const Message = “Welcome to KR Testing Solutions”
Dim string1
Dim string2
‘ *********** Main processing section ****************
‘ Collect the First String
string1 = GetFirstString()
‘ Collect the Second String
string2 = GetSecondString()
‘ Display message
WScript.Echo Message & ” ” & string1 & ” ” & string2
‘ ********* Subroutines & Functions go here **********
function GetFirstString()
GetFirstString = InputBox(“What is your 1st String?”)
End Function

function GetSecondString()
GetSecondString = InputBox(“What is your 2nd String?”)
End Function

Subroutines

Subroutines work almost exactly like functions except that they are unable to return any results to the calling statement. The syntax for creating a subroutine is shown here.
[Public | Private] Sub name [(arglist)]  statements End Sub

No comments:

Post a Comment