Using Library files in QTP


Using Library files in QTP
Author(s): (1) tarun_lalwani
Written On: 19-Sep-2005
Keywords:
QTP, Quick Test, Quick Test Pro
Introduction:
This document demonstrates on who to use Library files in QTP
Library files:
Library file is nothing but a VB script file containing code in VB Script format. Library files in QTP are used to declare functions that are needed to be used across actions. There are two ways to load a Library file in to your test:
1. Specify the Library file in the settings of the script
Create the VB Script file that you need and save to your PC

In QTP go to Test->Settings… Menu as shown in the image below

Go to the Resources tab of the Test Settings window and Click on “+” button. A new line in the list box will appear

Click on “…” button to choose the location of the file

If you specify 2 or more library having a function with same name then QTP will execute the one that comes first i.e. by looking at the order you specify the files. There is no need to specify an order for dependent library file Ex- if file2 uses functions defined in file1 then it is not necessary to specify file1 above file2 any order will work fine.

Click on the “Check Syntax” button to verify that the script is a valid one

The VB script file has been associated with your test and functions it can be used in all the actions present in the script. If you click on the “Set as Default” button then all the library files specified in your current test would be automatically added to the new tests that you create from now on.
Variables, classes, functions etc… declared in the Library files are accessible to all the Actions present with in the test. Specifying library files using this method makes it impossible to restrict the use of the Library to specific actions.
2. Loading the library file at run-time
Instead of specifying the library file in test settings we can load it at run-time by using the execute file function.
ExecuteFile “C:\Documents and Settings\tarun_lalwani\Desktop\Demo.vbs”
This would load all the variables, classes, structures etc… declared in the Library but they would be accessible on in the action that has called the ExecuteFile function i.e. if Action1 loads a library file using ExecuteFile then everything present in the library file would only be accessible to Action1 and not any other action.
To Load a library file present in Test Director the path can be specified as “[Quality Center] Subject\PathInTD”
Once a Library file is loaded using ExecuteFile it can’t be unloaded. It is unloaded automatically when the action ends.
The below table gives advantages of both the methods
Specifying Library File in Test Setting Loading Library File at Run-Time
For declaring Global Variable, Functions, classes etc…
Add all global variables to a VB Script file and specify it in settings. For restricting functions of the library to specific Actions
For loading libraries depending upon conditions at run-time. Ex-
If Condition1 then
ExecuteFile “C:\File1.vbs”
Else
ExecuteFile “C:\File2.vbs”
End If








Reference:
1. “Mercury QuickTest Professional, User’s Guide, Version 8.0.1”