Monday, November 2, 2009

QTP Register / Unregister a Function - RegisterUserFunc, unRegisterUserFunc

It would be better to read function libraries (if you don’t know what these are) before going down further.

What do you mean by Registering a Function?

You can register a public function to a test object to enable the function (operation) to be performed on a test object.

What are the ways to register a function in QTP?

Register a function through Function Definition Generator.

Register a function manually.

How to register a function manually? Give examples.

You can manually register your function to a test object by adding a RegisterUserFunc statement after your function as shown in the following example:

RegisterUserFunc "WebEdit", "MySet", "MySetFunc"

In this example, the MySet method (operation) is added to the WebEdit test object using the MySetFunc user-defined function. If you or the Subject Matter Expert choose the WebEdit test object from the Item list in the Keyword View, the MySet operation will then be displayed in the Operation list (together with other registered and out of the box operations for the WebEdit test object), or if in the Expert View, you press dot (period) after webedit object, you will get MySet in the IntelliSense drop-down list.

RegisterUserFunc: Enables you to add new methods to test objects or change the behavior of an existing test object method during a run session. When you use this statement, QuickTest uses your user-defined function as a method of a specified test object class for the remainder of a run session, or until you unregister the method.

Another RegisterUserFunc example with little bit more detail

This may not be useful in real word scenario, but it is just a simple example to give you a feel of RegisterUserFunc.

See the below screenshot. It shows all the operations which you can apply on a Page object (also known as IntelliSense). Just write the below line in QTP and press dot (period).



If you write some function and register it to any test object as shown below, it shows up with all the default operations in IntelliSense popup. Just write the below code in QTP and press dot (period). You don’t have to Run anything. Here the my_multiply method (operation) is added to the Page test object using the multiply user-defined function. [see the below screenshot – my_multiply is shown in IntelliSense popup]



Important points about registering a function

1. You can choose to override the functionality of an existing operation. For example you can write your own CheckProperty function and override its default functionality.

2. When you register a function to a test object, you can optionally define it as the default operation for that test object. This instructs QuickTest to display the function in the Operation column (Keyword View), by default, when you choose the associated test object in the Item list. In Function Definition Generator, register by checking the checkbox as shown below:



And manually to define a function as the default function, you define the value of the fourth argument of the RegisterUserFunc statement as True. For example:

RegisterUserFunc "WebEdit", "MySet", "MySetFunc", True

3. A registered or global function can only be called from a test or component after it is added to the test script or a function library that is associated with the test or the component's application area.

4. QuickTest clears all function registrations at the beginning of each run session.

5. When you register a function, it applies to an entire test object class. You CANNOT register a method for a specific test object.

6. If you want to call a function from additional test objects, you can copy the RegisterUserFunc line, paste it immediately after another function and replace any relevant argument values.

7. If the function you are registering is defined in a function library, it is recommended to include the RegisterUserFunc statement in the function library as well so that the method will be immediately available for use in any test using that function library.

8. You can re-register the same method to use different user-defined functions without first unregistering the method. However, when you do unregister the method, it resets to its original QuickTest functionality (or is cleared completely if it was a new method), and not to the previous registration.

9. 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.

How to unregister a function?

To unregister a function simply use UnRegisterUserFunc:

To unregister my_multiply function from our above example use:

unRegisterUserFunc "Page", "my_multiply"

UnregisterUserFunc: Instructs QTP to stop using the current registration of the method. If the unregistered method is a defined QTP method for the object, the functionality of the method returns to the standard QTP functionality.