How to Manage Library Files Inside HP QuickTest Professional-QTP?
Library files act as a placeholder for storing global variables, classes, and functions that can be used across multiple scripts. Once Library files are created, they have to be associated by the QTP test script so that all the code inside the library is available to the QTP script.
All the code inside the Library files is executed before the action.
These are usually plain text files, however, QTP supports library files with following extensions:
1) .qfl
2) .vbs
3) .txt
Out of the above three types of extensions, the most commonly used ones are .txt and .vbs.
Procedure to Create a Library file with .vbs and .txt extension:
Step-1: Launch Notepad application.
Step-2: Write the code inside notepad which is syntactically correct.
Step-3: Save the file as �filename.vbs� or �filename.txt� and remember to keep the double quotes while providing the library name.
Procedure to Create a Library file with .qfl extension:
This type of library file can only be created inside QTP.
Step-1: Launch QTP and select File->New->Function Library.
Step-2: A new tab gets opened inside the QTP expert view. Now this library can be saved with any extension be it .qfl, .txt and .vbs.
Associating Library files with QTP:
As mentioned above, the code inside the library file can only be utilised by QTP once we associate the library file with QTP.
The steps for associating a library file with QTP are as under:
Step-1: Navigate to File->Settings and on Test Settings dialog, navigate to Resources tab and you will be a screen similar to the one shown below if you are using QTP 11.
Step-2: Click on the plus green button which will enable a blank row inside Associated function libraries section and then click on the … button to browse the file from the system.
You will see a screen something similar to the one given below:
Step-3: You may click on the Check Syntax button to verify that all the code inside the library is correct.
Step-4: In case you want to associate the library with all subsequent tests, click on the Set as Default button. This will associate the library with all future QTP tests without the need for going to resources tab and associating the library manually, which can save some time.
You can associate multiple library files with your QTP script and can access and edit the library file from QTP interface by selecting Resources->Associated Function Libraries and selecting the desired library. The library gets opened inside the QTP editor in a separate tab that you can edit easily within QTP interface.
Loading Library files at run time through the code:
There are two ways of associating a library at run time: 1) ExecuteFile and 2) LoadFunctionLibrary.
LoadFunctionLibrary is a new method that is introduced in QTP 11. LoadFunctionLibrary and ExecuteFile are quite similar statements in that both load the library file at run time, however, there are few differences to note:
Comparison between ExecuteFile and LoadFunctionLibrary:
1) ExecuteFile can load only a single library while LoadFunctionLibrary can load multiple libraries at the same time by providing comma-separated paths. Example: Execute �C:Lib1.vbs�. LoadFunctionLibrary �C:Lib1.vbs�,�C:Lib2.vbs�
2) Files loaded through ExecuteFile cannot be debugged while files loaded through LoadFunctionLibrary can be debugged as the associated library is opened as a read only file inside QTP editor.
3) Libraries loaded through ExecuteFile can only be accessed through the Action, which has the ExecuteFile statement. For example, in Action1 we have the ExecuteFile statement so if we try to access any code inside the Library in Action2, it is not possible. However, libraries laoded through LoadFunctionLibrary can be accessed from any action.
4) Libraries loaded through ExecuteFile are unloaded automatically once the action ends.
5) Create a class inside a library load the library using ExecuteFile statement. Now, the class can be instantiated inside an action using the new operator which doesn�t happen if the library is loaded through Resources tab inside Settings. This is not possible through LoadFunctionLibrary.
Both ExecuteFile and LoadFunctionLibrary statements, however, accepts relative paths if the absolute path is already defined under Tools->Options and inside Folders tab.
Using Multiple Library Files in HP QuickTest Professional – QTP:
If we have associated multiple library files inside the Resources tab, then there are few things to note carefully:
1) Library files are loaded from bottom to the top. It means if a function with same name is present in both the files, then the function inside the top library will be used inside any of the QTP actions because it is loaded lastly and it will redefine the original function.
2) In case we have associated two Library files with only one library having the statement �Option Explicit�. Then QTP will ignore the �Option Explicit� statement irrespective of the order these libraries are loaded. �Option Explicit� statement has to be present inside all the associated library files in order to enforce variable declaration but only inside Library files. In case, we want to enfore variable declaration inside Action1, then we need to have the statement �Option Explicit� inside Action1 as well.
Remember that:
Only the public functions and public variables inside the Library can be accessed inside the Action. Any function or a variable that is declared, as private can not be accessed inside the Action, however, it can be accessed inside the other associated libraries.
Using classes inside the Library files:
If we define a class inside an associated library, then in order to access the class we have to use code something similar to the one given below:
Class YogiClass
End Class
Copy the above code inside the library and associate it with your QTP script. Then inside the Action, add the following code:
Set oClass = New YogiClass
Run the QTP script. You will get an error something similar to the one given below:
This error looks quite strange. The only logical explanation I can think of here is that probably New operator can only be used inside the namespace where the class is defined or in other words, we can only instantiate a class object inside the same scope as the class.
A possible workaround is to instantiate the class inside a function and return the object from within the function.
Class YogiClass
Public my_var
End Class
Function Func_QTP
Set Func_QTP = New YogiClass
End Function
Copy the above code inside the library and associate it with your QTP script. Then inside the Action, add the following code:
Set oClass = Func_QTP
oClass.my_var = 10
Msgbox oClass.my_var
This time you will get the output as 10.
I hope you would have liked this article, thus would like to have your comments & feedback on the same.
Many More Articles on HP QuickTest Professional

An expert on R&D, Online Training and Publishing. He is M.Tech. (Honours) and is a part of the STG team since inception.
Thats a fantastic article on library files.
Cheers,
Rushikesh
Hi,
the explanation is easily understandable and very useful.
Does this work in QTP 9.2 also.
thanks and regards
Hi Rajeshwari,
The above code should work on QTP 9.2 except the LoadFunctionLibrary statement which works only on QTP11.
Please let me know if have any other issues.
Regards,
Yogindernath
Good Article on Function Libraries..Thanks Buddy…..
Crystal Clear..awesome
Thanks for sharing.
Can we load single function from library file at run time in UFT 12.1, not whole library I want load single function only.
Is it possible ?