여러 개의 하위 객체를 동시에 조작하기 위해 QTP 자체 방법을 재정의하는 방법

12310 단어 objectqtp
At times, there are situations where we need to execute the same operation on multiple objects. Example of such situations is listed below
  • Checking/Unchecking all Check Boxes on a web page
  • Setting all the Text Boxes to blank value
  • Selecting the first possible value for every WebRadioGroup

  • The usual way to do it in Descriptive Programming (DP) is to create a Description object and then use ChildObjects method to get all objects matching this description. The code shown below demonstrate the same
    'Create a description for web checkbox
    Dim oWebChkDesc
    Set oWebChkDesc = Description.Create
    oWebChkDesc("micclass").value = "WebCheckBox"
    oWebChkDesc("html tag").Value = "INPUT"
     
    'Get all objects matching this description
    Dim allCheck, oCheckBox
    Set allCheck = Browser("KnowledgeInbox").Page("KnowledgeInbox").ChildObjects(oWebChkDesc)
     
    Dim i
     
    For i = 0 to allCheck.Count - 1
    	Set oCheckBox = allCheck(i)
    	oCheckBox.Set "ON"
    Next

    The problem with above approach is the re-usability factor. The code we wrote is not elegant when such operations need to done in number of scripts or multiple no. of times in a script. It is nice to have something within the framework which allows re-usable way of performing such operation. The code shown below can achieve the same
    'Description: The function calls a method on a object by it's name
    'Parameters:
    '@Obj - The object on which the methods needs to be called
    '@MethodName - The name of the method to be called
    '@Params - Parameter to be passed to the Method. In case of mulitiple
    ' parameters use Array
    Function CallAllByName(Obj, MethodName, Params)
    	'If the Params is not an Array we make it an Array
    	'This makes it flexible to pass a single parameter without
    	'creating an array for the same
    	If VarType(Params) < vbArray Then Params = Array(Params)
     
    	Dim objDesc
     
    	'Try to Extract the description from the object
    	Set objDesc = Obj.GetTOProperties()
     
    	'Generate the call statement parameters
    	Dim i
    	Dim paramCallText
     
    	paramCallText = " "
    	For i = 0 to UBound(Params)
    		paramCallText =  paramCallText & "Params(" & i & "),"
    	Next
     
    	'Removed the trailing ","
    	If Right(paramCallText,1) = "," Then paramCallText = Left(paramCallText, Len(paramCallText)-1)
     
    	If objDesc.Count = 0 Then
    		'The object is an derived object returned from ChildObjects
    		'We Can't do anything special with this. Just try to execute the method
    		'on the Object passed to this function
                    Execute "Obj." & MethodName & paramCallText
    	Else
    		Dim oParent
     
    		'Get the Test Object's parent
    		Set oParent = Obj.GetTOProperty("parent")
     
    		'Get all childs matching current object description
    		Dim allChilds
    		Set allChilds = oParent.ChildObjects(objDesc)
     
    		If allChilds.Count = 0 Then
    			'No matching objects were found. So let us just try to Set the value
    			Execute "Obj." & MethodName & paramCallText
    		Else
    			'We now have multiple objects matching this description
    			'Peform set operation for all childs
    			For i = 0 to allChilds.Count - 1
    				Execute "allChilds(i)." & MethodName & paramCallText
    			Next
    		End If
    	End If
    End Function

    The function CallAllByName shown extract the description from the object passed to it. GetTOProperties method returns a description from the current object. There is once exception where this method doesn’t work, when the objects have been retrieved from the ChildObjects. The function can be used in the following manner
    Function SetAll(Obj, Text)
    	CallMultiObjectMethod Obj, "Set", Text
    End Function
     
    RegisterUserFunc "WebCheckBox", "SetAll", "SetAll"
    RegisterUserFunc "WebEdit", "SetAll", "SetAll"

    Note: In case the method being called takes multiple parameter we need to pass them using Array
     
    Function SetAll(Obj, Text1, Text2)
    	CallMultiObjectMethod Obj, "Set", Array(Text1, Text2)
    End Function
    

    We can use the SetAll method as shown in above code in two ways
    Method 1
    Browser("KnowledgeInbox").Page("KnowledgeInbox").WebCheckBox("CheckBoxes").SetAll "ON"

    In this method we change the properties of the “CheckBoxes” object in the Object Repository to match all the objects we want to operate
    Method 2
    In this method we again create a description object and then used it inside the TestObject to call the SetAll method
    'Create a description for web checkbox
    Dim oWebChkDesc
    Set oWebChkDesc = Description.Create
    oWebChkDesc("micclass").value = "WebCheckBox"
    oWebChkDesc("html tag").Value = "INPUT"
    Browser("KnowledgeInbox").Page("KnowledgeInbox").WebCheckBox(oWebChkDesc).SetAll "ON"

    좋은 웹페이지 즐겨찾기