Create and work with a user-defined function
Relevant for: GUI tests and components
This topic describes how to work with a user-defined function.
Prerequisites - Open the function library or test
Determine whether you want to store the function in an action or in a function library.
If you insert the function in a function library, the function will be accessible to any associated test.
If you insert the function directly in an action in the Editor, it can be called only from within the specific action.
Create a new function library or action, open an existing one, or click on the tab of an open function library or action to bring it into focus.
Create the function
You can define functions manually or using the Function Definition Generator, which creates the basic function definition for you automatically.
Tip: If you want to add a comment about your function, you can add a comment immediately above the function name with @description line and the string describing the function. This description is displayed as a custom tooltip in UFT One's autocomplete window. For example
'@Description This function displays a Hello World message box. Function Hello_World MsgBox "Hello world" End Function
Then, when you use UFT One's autocomplete menu, you can see the tooltip:
If you want to register the function to a test object class, define it as a public function, and make sure that it expects the test object as the first argument.
If you want to override an existing test object method, make sure that after the test object argument, your function accepts the same number of arguments as the method it overrides.
Register the function to a test object class - optional
You can register your function as a new method for the test object class, or you can register it using an existing method name to (temporarily) override the existing functionality of the specified method.
You can perform this step manually, or using the Function Definition Generator Dialog Box:
Add a RegisterUserFunc statement in your action or function library. The name of the test object operation you register cannot contain spaces. In this statement, you can also instruct UFT One to use the function as the default operation for the test object class.
RegisterUserFunc "WebEdit", "MySet", "MySetFunc", True
After this statement runs (during the run session), the MySet method (operation) is added to the WebEdit test object class using the MySetFunc user-defined function, and defined to be the default operation (as specified in the last argument of the statement).
If you or the Subject Matter Expert choose the WebEdit test object from the Item list in the Keyword View, the MySet operation is selected automatically in the Operation column. It is also displayed in the Operation list together with other registered and out of the box operations for the WebEdit test object.
|Using the Function Definition Generator||
If you use the Function Definition Generator Dialog Box to create your function definition, a RegisterUserFunc statement is automatically added immediately after the definition if you select the Register to a test object option.
If the function you are registering is defined in a function library, we recommend including the RegisterUserFunc statement in the function library as well so that the method will be immediately available for use in any test or component using that function library.
Associate the function library with a test or application area
If you inserted the code in a function library, you must associate the function library with a test or application area to enable tests and components to access to the user-defined functions.
Alternatively, you can add a LoadFunctionLibrary statement to your test or component to load the function library during the run session and access its functions.
Call the function
In your test, component, or function library, do one or both of the following:
Create steps that call your user-defined function as a global function.
Run the user-defined function by calling the test object method to which it is registered.
Navigate to the function's definition - optional
You can navigate directly from a function call to the function's definition.
In the Editor, in an action or in a function library, click in the step containing the relevant function.
Perform one of the following:
Select Search > Go to > Definition.
Right-click the step and select Go to Definition from the context menu.
UFT One activates the relevant document (if the function definition is located in another function library) and positions the cursor at the beginning of the function definition.
Unregister the function - optional
If you do not want your function to remain registered until the end of the run session, add an UnregisterUserFunc statement in your test or function library.
If you register a method within a reusable action, you should 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 are not affected by the method registration.