QTP Registering a Function Using the Function Generator
QTP Registering a Function Using the Function Generator:
You can register a public function to a test object to enable the function (operation) to be performed on a test object. When you register a function to a test object, you can choose to override the functionality of an existing operation, or you can register the function as a new operation for the test object.
After you register a function to a test object, it is displayed as an operation in the Keyword View Operation list when that test object is selected from the Item list, as well as in IntelliSense and in the general Operation list in the Step Generator (for function libraries). When you register a function to a test object, it can only be called by that test object.
If you choose to register the function to a test object, the Function Definition Generator automatically adds the argument, test_object, as the first argument in the Arguments area in the top-right corner of the Function Definition Generator. The Function Definition Generator also automatically adds a RegisterUserFunc statement with the correct argument values immediately after your function definition.
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, by default, when you or the Subject Matter Expert choose the associated test object in the Item list. When you define a function as the default function for a test object, the value True is specified as the fourth argument of the RegisterUserFunc statement.
If you do not register the function to a specific test object, the function is automatically defined as a global function. Global functions can be called by selecting the Functions category in the Step Generator (for function libraries), or the Operation item in the Keyword View. A list of global functions can be viewed alphabetically in the Operation box when the Functions category is selected in the Step Generator (for function libraries), in the Operation list when the Operation item is selected from the Item list in the Keyword View and when using IntelliSense.
QuickTest searches the function libraries in the order in which they are listed in the Resources pane. If QuickTest finds more than one function that matches the function name in a specific function library, it uses the last function it finds in that function library. If QuickTest 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, it is recommended that you verify that within the resources associated with an application area, each function has a unique name.
Tip: If you choose not to register your function at this time, you can manually register it later 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).
You can also register your function to other test objects by duplicating (copying and pasting) the RegisterUserFunc statement and modifying the argument values as needed when you save the function code in a function library.
To define this 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
Note: A registered or global function can only be called from a component after it is added to a function library that is associated with the component’s application area.
To register the function to a test object:
- Select the Register to a test object check box. The options in this area are enabled, and a new argument, test_object, is automatically added to the list of arguments in the Arguments area in the top-right corner of the Function Definition Generator. (The test_object argument receives the test object to which you want to register the function.)
Note: If you clear the Register to a test object check box, the default test_object argument is automatically removed from the Arguments area (unless you renamed it).
- Select a Test object from the list of available objects. For example, for the sample VerifyProperty function, you might want to register it to the Link test object.
- Specify the Operation that you want to add or override for the test object.
- To define a new operation, enter a new operation name in the Operation box. For example, for the sample VerifyProperty function, you may want to define a new VerifyProperty operation.
- To override the standard functionality of an existing operation, select an operation from the list of available operations in the Operation box.
- If you want the function to be displayed as the default operation in the Operation column when you or the Subject Matter Expert choose the associated item, select the Register as default operation check box.
For example, if you were to define the VerifyProperty operation as the default operation for the Link test object, the value True would be defined as the fourth argument of the RegisterUserFunc statement, and the syntax would appear as follows:
RegisterUserFunc “Link”, “VerifyProperty”, “VerifyProperty”, True
After you specify the test object registration information, you specify additional arguments for the function
Let us take an example where we create a method intModSelect, and register it to the WebList object.So now whenever we call the WebList.Select , the script will execute the intModSelect function. The intModSelect function takes 2 parameter, the WebList TestObject and the the value to select in the list.
Public Function intModSelect(ByRef test_object,ByVal strVal)
If Not test_object.Exist Then
Reporter.ReportEvent micFail,”intModSelect”,”Test Object does not exist”
strAllItem = test_object.GetROProperty(“all items”)
If InStr(strAllItem,strVal)>0 Then
Reporter.ReportEvent micPass,”intModSelect”,”Selected the Value <” & strVal &”>”
Reporter.ReportEvent micFail,”intModSelect”,”Value <” & strVal &”> does not exist in the weblist.Unable to select”
‘ TODO: add function body here
RegisterUserFunc “WebList”, “Select”, “intModSelect”
Browser(“Google Advanced Search”).Page(“Google Advanced Search”).WebList(“ResultPerPage”).Select(“200 results”)