如何改写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"

相关文章推荐

通过eclipse方法来操作Hadoop集群上cassandra数据库(包括创建Keyspace对象以及往数据库写入数据)

通过eclipse方法来操作Hadoop集群上cassandra数据库(包括配置cassandra,创建Keyspace对象以及往数据库写入数据); 创建map/reduce工程,命名为Cassand...

我用这个方法来执行查询操作

  • 2009年10月06日 10:23
  • 3KB
  • 下载

html5 使用FileReader对象的readAsDataURL方法来读取图像文件

使用FileReader对象的readAsDataURL方法来读取图像文件 jquery2000 2014-4-3 13:52:27 阅读(3234) 评论(0) FileReader...

使用FileReader对象的readAsDataURL方法来读取图像文件

FileReader对象的readAsDataURL方法可以将读取到的文件编码成Data URL。Data URL是一项特殊的技术,可以将资料(例如图片)内嵌在网页之中,不用放到外部文件。使用Data...
  • baiasp
  • baiasp
  • 2017年05月07日 16:07
  • 195

使用FileReader对象的readAsDataURL方法来读取图像文件

readAsDataURL方法会使用base-64进行编码,编码的资料由data字串开始,后面跟随的是MIME type,然后再加上base64字串,逗号之后就是编码过的图像文件的内容。data:im...

用简单的方法来访问GObject对象的私有属性

这里只用到了最简单的方法,给我的感觉就是使用g_object_get/set函数来完成对已经安装的属性的存取工作。 目前我们还没有涉及到‘信号’这个NB的东西,连interface'接口'是什么,我...
  • xbl1986
  • xbl1986
  • 2011年08月26日 15:23
  • 761

【Java】【TIJ】实现一个类:不允许用构造方法来直接创建类实例(对象)

某些情况下,也许我们不希望客户使用

android利用handle对象的sendMessage()方法来实现网络图片的下载(附详细源码)

1. 传递Message。用于接受子线程发送的数据, 并用此数据配合主线程更新UI。 在Android中,对于UI的操作通常需要放在主线程中进行操作。如果在子线程中有关于UI的操作,那么就需要把数据...

Objective-C 通过description方法来打印对象

C++中可以直接打印对象指针,打印的就是该指针指向的虚拟内存地址,Java中通过打印对象可以输出对象的虚拟内存地址,OC中同样可以通过打印对象指针来输出对象的虚拟内存地址,也提供了descriptio...
  • oktears
  • oktears
  • 2014年11月09日 14:01
  • 2091
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:如何改写QTP自带方法来同时操作多个子对象
举报原因:
原因补充:

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