Function Libraries Association with the Script.



In most of our scripting needs, whether we are designing a new framework or some other task, we generally try to take an approach where we can make as much as generic script and thus, will have some reusable logic. To keep this reusable function we need to maintain the function libraries so that multiple scripts can use them. Now, in order to use this function library with multiple test cases, we need to associate these function libraries with our scripts.

There are multiple techniques by which we can associate our function libraries to our scripts. The selection of the technique solely depends on the framework approach, business requirements, automation strategy etc. as each of them has some associated pros and cons.

Below mentioned are the four (4) techniques by which we can associate function libraries to our scripts.

      1.    Using Test Settings window.

This is the most common approach to associate function libraries to the test cases/scripts. To use this method just navigate to File =>  Settings =>  Resources =>   Associate function library pane.




Click on the “+” button and select the function libraries you want to associate with the tests. This method has one limitation as in case if the location of the test or function libraries are going to change then this test will still be looking on the same specified locations and hence, will cause failure.

      2.    Using “ExecuteFile” method.

The second approach is to use the ExecuteFile method. The syntax to use this method is ::

ExecuteFile “sPath_of_Filewith_Name.vbs”

This method will run all the statements placed in the vbs file and all the functions, subroutines and other elements from the file (function library) will be available to the action as global entities. While in debugging mode, if we are calling any function from this file then there may be a case when the execution pointer may not be pointing at the correct line.
This approach could be very handy when you don’t want to associate a file but in some specific scenario, you want to access few functions from that particular file.

      3.    Using Automation Object Model.

As we know, Automation object model approach is generally taken when we want to hit the internal methods of the tool or when we want to automate the tool itself. Its like we are creating an external script which will open the tool, load the tests, perform all of the relevant settings and then run the desired script and so on.
Here, below mentioned code snippet shows how to make the reference of the tool object, launch it and open the test and associate function library file. ::

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

objQTP.Open "Path_of_test", False, False
Set objLibColl = objQTP.Test.Settings.Resources.Libraries

objLibColl.Add "Path_of_File_With_Name.vbs", 1


We can use this same statement to add multiple files as well by using a pipe symbol (|) as a separator and also we can opt to change this code a bit to not to launch the tool and so and so.
This approach is perfect when our framework approach suggests that we are not manually opening a tool or we are running QTP from other tool like QC or through a scheduler etc.

      4.    Using Load Function Library Method

This is the feature which has been added in QTP 11 and this statement allows you to load one or more function libraries to your tests and hence, the functions, subs etc from the function library are all available to the code which is below to this load function library statement in your test.

LoadFunctionLibrary "Path_with_Name1.vbs", "Path_with_Name2.vbs"

Here we have associated two (2) function libraries simultaneously and this way, we can access all the procedures from these two libraries. 
This function library association approach is perfect when the location of the function library is expected to change very often or in a scenario where function libraries are placed on the local machines and there are multiple machines to play the same script, then we can regularize or parameterize the path part and make it a regularized statement.


No comments:

Post a Comment