Monday, August 16, 2010

User Defined Functions

1.       If you have repeatable steps in a component, consider using a user defined function. User defined functions makes your component look shorter, easier to maintain, read and design. A user defined function stored in a function library can then be called from within a component.
2.       Functions can be written manually or can be created using QTP’s function definition generator. The screenshot below shows the generator with various options.
3.       To use an Option Explicit statement in a function library associated with your component, you must include it in all the function libraries associated with the component. If you include an Option Explicit statement in only some of the associated function libraries, QTP ignores all the Option Explicit statements in all function libraries.
4.       Each function library must have unique variables in its global scope. If you have 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. If you need to use more than one variable with the same name in the global scope, include a Dim statement only in the last function library (since function libraries are loaded in the reverse order).
5.       By default, steps that use user-defined functions are not displayed in the test results tree of the Test Results window after a run session. If you want the function to appear in the test results tree, you must add a Reporter.ReportEvent statement to the function code. For example, you may want to provide additional information or to modify the component status, if required.
6.       If you delete a function in use from an associated function library, the component step using the function will display the “?” icon. In subsequent run sessions for the component or business process test, an error will occur when the step using the non-existent function is reached.
7.       If another user modifies a function library that is referenced by a component, or if you modify the function library using an external editor (not QTP), the changes will take effect only after the component is reopened.
8.       When more than one function with the same name exists in the function library, the last function will always be called. To avoid confusion, make sure that you verify that within the resources associated with an application area or component, each function has a unique name.
9.       If you want to call a function from additional test objects, you can use the RegisterUserFunc option which is usually placed immediately after the function in question and replace any relevant argument values. It is recommended to include it in the function library so that the method will be immediately available for use in any component using that function library.
10.   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 QTP functionality (or is cleared completely if it was a new method), and not to the previous registration.
11.   For example, suppose you enter the following statements:
RegisterUserFunc "Link", "Click", "MyClick"
RegisterUserFunc "Link", "Click", "MyClick2"
UnRegisterUserFunc "Link", "Click"
12.   After running the UnRegisterUserFunc statement, the Click method stops using the functionality defined in the MyClick2 function, and returns to the original QTP Click functionality, and not to the functionality defined in the MyClick function.

No comments: