How to call functions from a function library

Using a function library

QuickTest Professional does not support declaring functions in one Action and using them in another Action.

If you include your functions in a loaded library written in VBScript, you may call those functions from any location in the test. This is particularly useful when you want to reuse code that performs certain steps.

You call a function procedure using the function name, followed by the argument list. If your function returns a value, the argument list should appear within parentheses.

QuickTest Professional 6.0 or higher
1. Create the function library file.
2. Associate the function library with the test script. For information on associating a function library, refer to Document ID 46019 – How to associate a function library with a test.
3. Call the function in the script, as needed.

Example:
‘ Function library is loaded in File -> Settings -> Resources tab.
‘ Call the MakeReverseUpper function.
RevName = MakeReverseUpper ("Homer")
MsgBox RevName

To enter a function call using the Keyword View, use the Step Generator. If you have registered the function to a QuickTest Professional Test Object (using RegisterUserFunc), you will call the function as you would a built in QuickTest Professional test method.

QuickTest Professional 5.6 or below
1. Create a FileSystemObject.
2. Open the library file.
3. Read the file in to a variable.
4. Execute the library code (within the variable).

The following example illustrates the use of a function that converts a string to uppercase and reverses the order of the letters.

Example:
‘ Test script:
Dim fso, f, libcode, RevName

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:\Libcode.txt", 1)
libcode = f.ReadAll

Execute libcode

RevName = MakeReverseUpper ("Homer") ‘ Function returns "REMOH"

MsgBox(RevName)

‘ Function Library (Filename: C:\Libcode.txt)
Function MakeReverseUpper (s)
rev = StrReverse (s)
MakeReverseUpper = UCase (rev)
End Function

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s