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



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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值