QTP tutorials & interview questions


Search more on QTP here

Function Library

Function Libraries in QTP

If you have repeatable steps in a test or an action then consider using a user-defined function. User-defined functions will make your tests look shorter, easier to maintain, read and design. A user-defined function can then be called from within an action.


Advantages of Function Library (functions)

1. Time and resources can be saved by implementing and using user-defined reusable functions.

2. User-defined functions can be stored in a function library or within an action in the test.

3. User-defined functions can be registered as a method for a QTP test object.

[It is advisable not to give user-defined functions same name as built-in functions (refer to the Built-in functions list in the Step Generator (Insert > Step Generator, in theStep Generator dialog box choose Built-in functions from Library Combo box, it will show all of the Built-in functions in Operation Combo box))]

If the function is stored in a function library then we have to associate that function library to a test so that the test can call all the public functions listed in that library.

Functions in an associated function library are accessible:

a) From Step Generator (for tests and function libraries),

b) From the Operation column in the Keyword View, or

c) Can be entered manually in the Expert View.

Functions directly stored in an action in a test can be called from within that action only making them private from the outside world.

A Private function can also be created in a function library and this private function can only be called from within the function library itself in which it is defined.

Functions can be created manually or by using Function Definition Generator.

It is easy to create a function library:

1. Open a new function library (File->New->Function library).

2. Add content to it (your function). Associate it with a test (File-> Associate Library with Test).

3. You are ready to go.

Many different function libraries can be opened simultaneously as each opens in its own separate window and you can work on them separately.

If a test is open you can view all the function libraries associated with it. (Resources->Associated Function Libraries.)

You can save function library either from File->Save or right click on function library tab on the top of function library and choose save.


Example 1 - Simple example of a Function Library and test

Example 2 - Example using private and public functions in function library

Example 3 - Example of registering a function to a test object by creating a new operation

Example 4 - Example of overriding a copy operation of a WinEdit Class objects

Some important points regarding Function Libraries.

When we register a function, it applies to an entire test object class and it's not possible to register a method for a specific test object.

If we use options like Run from step or Debug from step, to begin running a test from a point after method registration was performed in a test step (and not in a function library), QTP does not recognize the method registration because it occurred earlier to the beginning of the current run session and this all is due to the reason that QTP clears all method registrations at the beginning of each run session.

If you want to use Option Explicit statement in Function Library, you must include it in all the function libraries associated with the test otherwise QuickTest ignores all the Option Explicit statements in all function libraries.

Steps using user-defined functions are not displayed in the test results tree of the Test Results window by default. For function to appear in the test results tree, you must add a Reporter.ReportEvent statement to the function code.

If a function library that is referenced by a test is modified by you or any other user using an external editor, the changes will take effect only after the test is reopened.

Always make sure that each function has a unique name, if more than one function with the same name exists in the test script or function library, QTP will always call the last function because QuickTest searches the test script for the function prior to searching the function libraries.

Most important of all: If you register a method within a reusable action, it is strongly recommended to unregister the method at the end of the action (and then re-register it at the beginning of the next action if necessary), so that tests calling your action will not be affected by the method registration.

We can re-register the same method with different user-defined functions without first unregistering the method. When it is unregistered it is reset to its original QTP functionality e.g.

RegisterUserFunc "WebEdit", "Set", "MySet"

RegisterUserFunc "WebEdit", "Set", "MySet2"

UnRegisterUserFunc "WebEdit", "Set"

After running the UnRegisterUserFunc statement, the Set method stops using the functionality defined in the MySet2 function, and returns to the original QuickTest Set functionality, and not to the functionality defined in the MySet function.

If there are two associated function libraries that define the same variable in the global scope using a Dim statement or define two constants with the same name, the second definition causes a syntax error. Include a Dim statement only in the last function library (since function libraries are loaded in the reverse order). If you need to use more than one variable with the same name in the global scope.


Also See:
ExecuteFile