- WebEx training notes:
-
Function Library:
Two ways of adding function library:
1. Static Association: You can add as many
functions as you want in Function Library and associate them with your code, by
following the path: Test Settings -> Resources->
Associate Function Library
2. Run Time Association: Adding Programmatically:
ExecuteFil(“<path>”)
Here
once you mention the above code, you run those functions by calling by there
name. IF you are calling them without using ExecuteFile method, they script
will throw error.
Create Function Library:
- File -> New -> Function Library
We can add as many funcitons wanted to Function Library, then associate them to make use of them.
Function library - Has all business logic.
- Source for the below content: http://www.automationrepository.com/2011/09/associate-function-library-to-qtp-script/
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.Once functions are associated with function library, we can use call method.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
| '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.
1
2
3
4
5
| 'Action begins ExecuteFile "C:\YourFunctionLibrary.vbs" 'Other logic for your action would come here '..... |
Help file:
After you run an ExecuteFile statement, you can call the functions in the loaded file only within the scope of the calling action or component.
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 or component that contains an ExecuteFile statement, the execution marker may not be correctly displayed.
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.
1
2
3
4
5
6
7
8
| '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 '..... |
Help file:
To load a function library during a run session, insert a LoadFunctionLibrary statement or ExecuteFile statement in your action, scripted component, or function library. When you run the test, this statement runs all global code in the specified function library, making all definitions in the file available for use. For details on these statements, see the Utility Objects section of the HP UFT Object Model Reference for GUI Testing.
In a test: After you run a LoadFunctionLibrary statement, the functions in the file are available to your entire test, until the end of the run session.
In a component: LoadFunctionLibrary works in the same way as ExecuteFile. After you run the statement, the functions in the file are available only within the scope of the calling component.
LoadFunctionLibrary enables you to debug the functions in the function library during run-time.
No comments:
Post a Comment