Pages

Tuesday, November 13, 2012

Quick Test Professional (QTP) - RegisterUserFunc Object


This object is used to add new methods to a test object or to override existing ones. The syntax is :

RegisterUserFunc TOClass, Method, FunctionName, SetAsDefault

If we want to override the SetFunction for WebEdit test object then we need to create a new function and then assign it using the RegisterUserFunc object as follows:

'New set method for webEdit objects
Sub NewSet(Object , newValue)
  Object.Set Ucase(newValue)
End Sub

RegisterUserFunc "WebEdit", "Set", "NewSet"
Browser("Browser").Page("Page").WebEdit("Text").Set "Nikhil"

The last statement in our example now calls out NewSet function and changes the input value
to upper case and then assign the value.

If we want to use the NewSet function to handle different types of test objects, then we can use GetTOProperty function to determine which type of object has been passed to the funtion.

If we want define a new Set method for all the objects like "WebEdit", WebCheckBox", WebList", "WebRadioButton" etc, then we can create a function like one shown below:

'Generic set method for various objects
Sub GenericSet(Object, newValue)

  'Get the type of the object 
  sObjectType = Object.GetTOProperty("micclass")

  Select Case sObjectType
    Case "WebEdit", "WebCheckBox"
      WebSet Object, newValue

    Case "WebList", WebRadioButton"
      WebSelect Object.newValue
  End Select
End Sub

WebSet and WebSelect functions used in the code given above are user defined functions that are being used to do operation on specific types of object.

No comments:

Post a Comment