site
stats
By Anish Pillai Anish Pillai Posted under QTP Basic Stuff

4 Different Ways to Associate Function Libraries to your QTP Scripts

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×

Most of the times, when you are creating test scripts or are designing a new QTP Framework, you would be trying to come up with reusable functions which you would have to store in the function library. Now, in order to use this function library with multiple test cases, you need to associate this function library with your scripts. This article explains the 4 methods that will help you in associating the function libraries in QTP Test Cases.


Based on the type of framework you are using, you can use any of the following methods to associate function libraries to your QTP Script –

  • 1) By using ‘File > Settings > Resources > Associate Function Library’ option in QTP.
  • 2) By using Automation Object Model (AOM).
  • 3) By using ExecuteFile method.
  • 4) using LoadFunctionLibrary method.



Let’s see in detail how each of these methods can be used to map function libraries to your test scripts.

1. Using ‘File > Settings > Resources > Associate Function Library’ option from the Menu bar

This is the most common method used to associate a function library to a test case. To use this method, select File > Settings option from the Menu bar. This will display the ‘Test Settings’ window. Click on Resources from the left hand side pane. From the right hand side pane, click on the ‘+’ button and select the function library that needs to be associated with the test case.

Associate function Library to QTP

Associate function Library to QTP


2. Using AOM (Automation Object Model)

QTP AOM is a mechanism using which you can control various QTP operations from outside QTP. Using QTP Automation Object Model, you can write a code which would open a QTP test and associate a function library to that test.

Example: Using the below code, you can open QTP, then open any test case and associate a required function library to that test case. To do so, copy paste the below code in a notepad and save it with a .vbs extension.

'Open QTP
Set objQTP = CreateObject("QuickTest.Application")
objQTP.Launch
objQTP.Visible = True

'Open a test and associate a function library to the test
objQTP.Open "C:\Automation\SampleTest", False, False
Set objLib = objQTP.Test.Settings.Resources.Libraries

'If the library is not already associated with the test case, associate it..
If objLib.Find("C:\SampleFunctionLibrary.vbs") = -1 Then ' If library is not already added
  objLib.Add "C:\SampleFunctionLibrary.vbs", 1 ' Associate the library to the test case
End


3. Using ExecuteFile Method

ExecuteFile statement executes all the VBScript statements in a specified file. After the file has been executed, all the functions, subroutines and other elements from the file (function library) are available to the action as global entities. Simply put, once the file is executed, its functions can be used by the action. You can use the below mentioned logic to use ExecuteFile method to associate function libraries to your script.

'Action begins
ExecuteFile "C:\YourFunctionLibrary.vbs"

'Other logic for your action would come here
'.....


4. Using LoadFunctionLibrary Method

LoadFunctionLibrary, a new method introduced in QTP 11 allows you to load a function library when a step runs. You can load multiple function libraries from a single line by using a comma delimiter.

'Some code from the action
'.....

LoadFunctionLibrary "C:\YourFunctionLibrary_1.vbs" 'Associate a single function library
LoadFunctionLibrary "C:\FuncLib_1.vbs", "C:\FuncLib_2.vbs" 'Associate more than 1 function libraries

'Other logic for your action would come here
'.....



This was all about the different ways using which you can associate function libraries to QTP Scripts. What are your views on this article? Can you think of any other points which I have missed and can be added here? Please let us know your views using the comments section. Happy Reading.. :–)

If you enjoyed this article, you can join our blog to get free email updates directly in your inbox.

To check out more tutorials, visit our QTP Tutorials page. You can also check the Archives page to view the list of all our articles.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×
  • Thanks for the useful info.

    <a href="" target="_blank">QTPbook</a>

  • Nice Pillai..!!! ๐Ÿ™‚

  • Pingback: QTP Frameworks : Designing QTP Modular Framework from scratch - Automation Repository - Automation Repository()

  • varma

    its really a valuable info..

    thanks a lot..

    • Anish10110

      Thanks Varma.. ๐Ÿ™‚

  • Pingback: Designing Data Driven Framework in QTP – Part 2 - Automation Repository - Automation Repository()

  • sachin

    Good article for QTP Beginner.Nice work

    I have to add one more point here for Execute Method and Load function library

    Debugging of Libraries using Execute Method is not supported.For this Load function library feature is introduced in QTP 11.

    Please confirm if i missed anything.

    • Anish10110

      Yes Sachin… I have also read the same thing somewhere. But I haven't checked this on my own.. Have you tried it out??

  • abhilash

    Can you please describe this method : Using AOM (Automation Object Model)

    • Anish10110

      Hi Abhilash,

      AOM is a mechanism using which you can control an application and make it do things without directly doing anything on the applications. For example, suppose you have Microsoft Outlook installed in your computer. Now you can write some code in VBSrcipt, that will open outlook and send mails on your behalf. Now with this method, you didn't directly open outlook, typed the mail and clicked on Send button. The VBScript code did this for you. So in a way you controlled Outlook from outside using some programming/scripting language.

      The same way things can be done in QTP also. You dont need to directly open QTP, load a test case, associate function library and object repositories. You can write some code that will do all these things for you.

      Now, the obvious thing would be, how can this be achieved. Well this is done using AOM. AOM is a set of functions and other code that an application will expose for you. You can then use this code to control the application. So Microsoft Outlook, QTP etc would expose various functions and objects, which you can use and then Send Mails, Run tests and do any desired operations.

      To read more about QTP AOM, refer to QTP help in your machine (if QTP is installed there). The location would be something similar to this- – C:Program Files (x86)HPQuickTest Professionalhelp. Here you fill find a file named 'Automation Object Model'.

  • kumar

    Hi,

    Yes I tried with "LoadFunctionLibrary" & "ExecuteFile" both the methods, in that, "LoadFunctionLibrary" method will allow to debug while "ExecuteFIle" method will directly execute the file.

    Thanks "sachin" to bringing this out.

  • Prerna

    It would be nice to know the performance comparison of these various methods, which ones faster and is most recommended over other methods, thats what i am struggling with these days

    • Anish10110

      I'm not sure how many people choose one of these methods based on the performance aspects because most of the times the function library is not huge enough and/or is not called enough times to really find any significant difference in the performance. Mostly the choice is made based upon the framework type. Whats your thought in this?

  • pravin

    Hello Friends,

    I just want to know, is there any way in QTP using which I can open all the "associated function libraries" at One-shot??? I am asking this because, In my keyword engine I have more than 8 different function Libraries attached and opening each and everything, everytime is time consuming. Please let me know if their is anyway out for this..? Thanks

    • Anish10110

      Hi Pravin,

      How are the function libraries currently being associated??

      If you associate all the function libraries with your test cases then all the function libraries will be loaded together once the test case is run. Also, have you tried the LoadFunctionLibrary method to associate all the libraries at one go? Eg – LoadFunctionLibrary "C:FuncLib_1.vbs", "C:FuncLib_2.vbs", ……..

  • QTPRostrum

    Hi Anish,

    Can you tell me the difference between Execute File method and LoadFunctionLibrary method?

    My guess is that , may in the order in which we can use these two methods. e.g At the starting of Action or anywhere in Action.

    Please advise.

    Cheers,

    Rishish

    • Saju

      by execute file method we can have oly one file from local whereas by load function library we can have multiple files.
      Eg: ExecuteFile "C://Function.vbs"
      LoadFunctionLibrary "C://Function1.vbs", "C://Function2.vbs"……………

  • Pingback: Different Ways to Associate Function Libraries to our QTP Scripts | QTP Challenges 4 Test Experts()

  • Meera

    What is the difference between Executefile and Loadfunctionlibrary statements?

    • Amith CJ

      Three primary differences:

      1. If you have a user defined class in your library file, you need to use ExecuteFile. QTP will not load them if you use LoadFunctionLibrary.

      2. ExecuteFile can load one library file per statement while you can load many library files in a single LoadFunctionLibrary statement (you need to separate library file paths using a comma).

      3. ExecuteFile is a VB thing is around good old days. LoadFunctionLibrary is a relatively newer one in QTP (not sure when it was added).

      • Muhammad Nadeem

        Good Explanation Amit ..Thank you

  • Firas

    good ideas for beginners,

    but you need to know that Go To Function Does not work here ๐Ÿ˜‰

  • John Chow

    On option 2(AOM) how do I go about opening a test that is in QC

  • sadashiv

    guys there are 5 ways to share the function library to qtp tool
    1)Loadfunction library
    2)Execute file
    3)AOM
    4)file > settings>Resources>add
    5)Test pane having method called available Keywords in the main test so that we can click der and we can associate FL.

  • Stephan Coetzee

    Hi,

    Thank you for the informative article.

    I do however have one question: What is the best way to update the function library if I were to update object names in the OR (Object Repository)? According to me the function library is not automatically updated when making changes to the OR, which would mean that I will have to update the function library manually every time objects' name or position changes inside the OR.

    To overcome this problem I am currently storing all of my re-usable actions (i.e. functions that references the objects in the OR) in a QTP script. This QTP script then serves as my library for re-usable actions. Then I reference these re-usable actions in all of my other QTP scripts.

    I currently only use my external function library to store functions that don't reference the OR (e.g. mathematical/date functions). I do this because then there is no need to manually update the function library every time the objects in the OR is updated.

    Any comments on this will be appreciated.

    Regards, Stephan Coetzee

    • Amith CJ

      Hi Stephan,
      There is no way to automatically update the function libraries for the references to OR elements. You need to do it manually. You need to design your scripts and function libraries in such a way that you don't have to update them too frequently. Consider these points and think trough:
      1. It may not be a great idea to write functions for the set of steps which are prone to frequent UI changes.
      2. How about using re-usable actions instead which will allow you to automatically update the names of OR elements when you change them in the OR?

  • Sumit Kumar Verma

    what is the script in QTP to run both (flight 3a & 4a) flight reservation app?

  • Ashok Kakade

    Hi All

    Can we Load only one function from Function Library which have multilple functions ?

  • Pingback: Four Different Ways to Associate Function Libraries to our QTP Scripts | QTP Automation Challenges ...!()

  • thanks a lot…it helped me immediately

  • Joseph

    byby

  • Joseph

    In the AOM Approach,
    1) FIND command is used to search with the entire file hierarchy, I guess when a file is added it just displays the name of file alone. So how FIND is performed
    2) What is the correct navigation steps to get to Associate Function library. Is it File-Settings or Test -> Settings
    -You are using "Set objLib = objQTP.Test.Settings.Resources.Libraries".
    and it is also mentioned in the beginning of discussion
    -By using โ€˜File > Settings > Resources > Associate Function Libraryโ€™ option in QTP

  • Tamil

    I'm getting object required error while using this Execute File. Can anyone assist me on this??

    ExecuteFile "D:OTPAutomationVBSGlobalVariables.vbs"

    Thanks

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×