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

转载 2011年01月21日 16:14:00



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"

QTP针对对象的参数化实例

参考文章地址:http://www.51testing.com/?26649/action_viewspace_itemid_608.html     在自动化测试当中,有时为了模拟真实效果,或者为...
  • achang21
  • achang21
  • 2013年09月15日 10:16
  • 745

reflow

浏览器为了重新渲染部分或整个页面,重新计算页面元素位置和几何结构(geometries)的进程叫做 reflow。由于 reflow 是一种浏览器中的用户拦截(user-blocking)操作,所以了...
  • u598975767
  • u598975767
  • 2016年05月26日 10:37
  • 143

如何查看QTP对象的属性列表

查看QTP对象的属性列表方法有两个,以下分别说明。 方法一:对象仓库绿色加号图标 通过单击"录制"按钮,将对象录制到对象仓库中,依此单击"Resources"-->"Object Repository...
  • testerYu
  • testerYu
  • 2015年06月16日 09:30
  • 2239

qtp 怎样添加界面中没有被操作录制的对象到对象库

qtp 怎样添加界面中没有被操作录制的对象到对象库
  • u013493370
  • u013493370
  • 2015年12月31日 09:58
  • 542

thinkphp3.2 多表事务的实例

多表事务不同于单表   1.  需要使用空白的Model 来开启以及使用事务 2.   如果不是在Model里面, 需要使用  $model->table() 来进行insert update del...
  • cominglately
  • cominglately
  • 2017年08月28日 12:16
  • 597

在QTP中如何管理对象库(转)

 在QTP中,如果不能有效的管理对象,将大大加大后期脚本的维护成本。我们需要的是一个干净整洁的对象库,但由于QTP本身对于对象的管理操作的局限性,我们只有运用有限的方法,来应对无限的可能发生的情况。在...
  • xiaohe119
  • xiaohe119
  • 2007年04月27日 13:46
  • 523

QTP运行对象属性和对象库属性的区别和用法

1.      SetToProperty :     修改对象库中对象的属性 2.      GetRoProperty :   获取对象库中对象的属性 3.      GetToProperty ...
  • erix1991
  • erix1991
  • 2014年01月10日 10:06
  • 1147

QTP 无法识别web对象原因

1、插件选择错误    处理方式:重新启动QTP,勾选 WEB插件; 2、在安装QTP后,或者禁用IE浏览器里的一些ActiveX控件后,正常录制QTP事,对象识别不了;    ...
  • wangsy0202
  • wangsy0202
  • 2016年08月01日 19:54
  • 1174

QTP----如何批量添加对象到对象库

今天开始研究QTP,看到QTP对象库的时候,发现一个问题,单个添加对象到对象库很简单,打开object spy,点击你想添加的对象就可以了。但是,如果在某个window窗口上,我想把所有的控件对象都添...
  • jiasz527
  • jiasz527
  • 2012年07月31日 16:08
  • 1009

QTP对象的方法(一)

一.对象的方法 Activate:object.Activate [Item] 激活对象 Item—列表中选择(名字:用引号;数字索引:不带引号;非必填项) 例1:Browser("Demo ...
  • sunnyken_97
  • sunnyken_97
  • 2016年08月19日 17:40
  • 1212
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:如何改写QTP自带方法来同时操作多个子对象
举报原因:
原因补充:

(最多只允许输入30个字)