关闭

如何改写QTP自带方法来同时操作多个子对象

标签: objectObjectOBJECTQTPqtp
959人阅读 评论(0) 收藏 举报
分类:



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"
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:168459次
    • 积分:2270
    • 等级:
    • 排名:第17179名
    • 原创:49篇
    • 转载:28篇
    • 译文:9篇
    • 评论:19条
    最新评论