User-defined functions

Relevant for: GUI tests and components

For tests or scripted components, if you have segments of code that you need to use several times in your tests or you want to add additional functionality, you may want to create a user-defined function.

You create the functions in VBScript.

A user-defined function encapsulates an activity (or a group of steps that require programming) into a keyword (also called an operation). By using user-defined functions, your tests or components are shorter, and easier to design, read, and maintain. You or a Subject Matter Expert can then call user-defined functions from an action or a component by inserting the relevant keywords (or operations) into that action or component.

Global Functions

A user-defined function is automatically defined as a global function. You can call global functions by typing them in the step or selecting them from the lists displayed in the following locations:

  • The Operation box in the Step Generator, when the Functions category is selected (for function libraries)

  • The Operation column in the Keyword View, when the Operation item is selected from the Item list

  • The Editor, when using the statement completion feature

Back to top

Functions registered to test objects

You can also register a user-defined function as a method for a UFT One test object class (type). A registered method can either override the functionality of an existing test object method for the duration of a run session, or be registered as a new method for a test object class. You can call the test object method by typing it in the step or selecting it from the list of operations available for the test object.

For more details, see Registered user-defined functions and Create and register a user-defined function using the Function Definition Generator.

Back to top

Functions during run-time

During run-time, UFT One searches the function libraries for the specified function in the order in which they are listed in the Solution Explorer. This order determines the function library priority.

For tests, UFT One searches for the specified function in the action before searching the function libraries.

If UFT One finds more than one function that matches the function name in a specific action or function library, it uses the last function it finds in that action or function library.

If UFT One finds two functions with the same name in two different function libraries, it uses the function from the function library that has the higher priority. To avoid confusion, we recommend that you verify that within the resources associated with a test or application area, each function has a unique name.

Back to top

User-defined function naming

When you create a user-defined function, do not give it the same name as a built-in function (for example, GetLastError, MsgBox, or Print).

Similarly, do not use VBScript registered words (for example, cStr, F1, ESC) for function names.

Built-in functions take priority over user-defined functions, so if you call a user-defined function that has the same name as a built-in function, the built-in function is called instead.

For a list of built-in functions, see the Built-in functions list in the Step Generator (Design > Step Generator).

Back to top