There are two ways (usually) to associate the library file to a test.
1. Resources pane of Test Settings (File->Settings) dialog (has advantages like files are in global scope –all the actions in a test can use those, can be debugged).
2. ExecuteFile (Local scope - When you run an ExecuteFile statement within an action, you can call the functions in the file only from the current action, and you cannot debug a file that is called using an ExecuteFile statement).
3. Another way (obviously it uses the first one in some way).
We will start with a very simple example and go on to elaborate more on ExecuteFile.
Create a new .vbs script as shown below. I saved it under c:\ as add.vbs.
Now open a new test in QTP and write few lines as below and run the test. It will show the value of a as 5 in message box.
Above we are using ExecuteFile function to include the add.vbs file we created earlier. After including that file we are calling the function from that file and showing its return value in a message box.
When you run your test, the ExecuteFile statement executes all global code in the function library making all definitions in the file available from the global scope of the action's script.
The syntax of ExecuteFile:
Where File is a string - the absolute or relative path of the file to execute.
The ExecuteFile statement utilizes the VBScript ExecuteGlobal statement.
ExecuteFile - Important points from QTP Guide.
When you run an ExecuteFile statement within an action, you can call the functions in the file only from the current action.
To make the functions in a VBScript file available to your entire test, add the file name to the associated function libraries list in the Resources pane of the Test Settings dialog box.
In addition to the functions available in the associated function libraries, you can also call a function contained in any function library (or VBscript file) directly from any action using the ExecuteFile function.
You can also insert ExecuteFile statements within an associated function library.
You cannot debug a file that is called using an ExecuteFile statement, or any of the functions contained in the file. In addition, when debugging a test that contains an ExecuteFile statement, the execution marker may not be correctly displayed.
ExecuteFile - Try this
Now my add.vbs and QTP test are like as shown below, i.e. one more variable z is added. Now what do you think it will show (for z) when the test is run.
On running this QTP test it will show 7 for msgbox z.
And guess what will it show now for the msgbox z if the add.vbs and QTP test are as below:
QTP Function Library
QTP Function Library - Example 1
QTP Function Library - Example 2
QTP Function Library - Example 3
QTP Function Library - Example 4