Tips for Working with User-Defined Functions in Qtp

Share This Post -

When working with user-defined functions in QTP, consider the following tips and guidelines:

  • For an in-depth view of the required syntax, you can define a function using the Function Definition Generator and experiment with the various options.
  • When you register a function, it applies to an entire test object class. You cannot register a method for a specific test object.
  • 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.
  • It is recommended to include the RegisterUserFunc statement in the function library so that the method will be immediately available for use in any component using that function library.
  • 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.
  • 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).
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • For example, suppose you enter the following statements:
  • RegisterUserFunc "Link", "Click", "MyClick"

    RegisterUserFunc "Link", "Click", "MyClick2"

    UnRegisterUserFunc "Link", "Click"

    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.