写了个简单的关键字框架Demo,基于XML+DP。
DP(Descriptive Programming)描述性编程可以说是搭建关键字框架必不可少的。
QTP中可以使用XMLUtil来读取XML文件。
关键字包括测试对象和操作。
把测试对象和测试用例(操作序列)都存在XML文件中,例如:
<Framework>
<Objects>
<Object>
<Name>Login Dialog</Name>
<ParentName></ParentName>
<ClassName>Dialog</ClassName>
<Properties>
<Property Name="text">Login</Property>
</Properties>
</Object>
<Object>
<Name>Agent Name</Name>
<ParentName>Login Dialog</ParentName>
<ClassName>WinEdit</ClassName>
<Properties>
<Property Name="attached text">Agent Name:</Property>
</Properties>
</Object>
<Object>
<Name>Password</Name>
<ParentName>Login Dialog</ParentName>
<ClassName>WinEdit</ClassName>
<Properties>
<Property Name="attached text">Password:</Property>
</Properties>
</Object>
<Object>
<Name>OK</Name>
<ParentName>Login Dialog</ParentName>
<ClassName>WinButton</ClassName>
<Properties>
<Property Name="text">OK</Property>
</Properties>
</Object>
</Objects>
<TestCase>
<Step ID="1">
<ObjectName>Login Dialog</ObjectName>
<Operation>Activate</Operation>
<OperationValue></OperationValue>
</Step>
<Step ID="2">
<ObjectName>Agent Name</ObjectName>
<Operation>Set</Operation>
<OperationValue>mercury</OperationValue>
</Step>
<Step ID="3">
<ObjectName>Password</ObjectName>
<Operation>Type</Operation>
<OperationValue>mercury</OperationValue>
</Step>
<Step ID="4">
<ObjectName>OK</ObjectName>
<Operation>Click</Operation>
<OperationValue></OperationValue>
</Step>
</TestCase>
</Framework>
从XML文件中取出测试对象:
Function GetObjectFromXML(XMLFile)
' 创建一个Dictionary对象用来存储所有对象的描述
Set obj_Dic = CreateObject("Scripting.Dictionary")
Set Objects = XMLUtil.CreateXMLFromFile(XMLFile)
Set Object = Objects.ChildElementsByPath("/Framework/Objects/Object/Name")
Set ParentName = Objects.ChildElementsByPath("/Framework/Objects/Object/ParentName")
Set ObjectClassName = Objects.ChildElementsByPath("/Framework/Objects/Object/ClassName")
Set ObjectProperties = Objects.ChildElementsByPath("/Framework/Objects/Object/Properties")
For i=1 to Object.Count
obj_Name = Object.Item(i).Value
obj_ClassName = ObjectClassName.Item(i).Value
Set PropertyCollection = ObjectProperties.Item(i).ChildElements
For j=1 to PropertyCollection.Count
obj_PropertyName = PropertyCollection.Item(j).Attributes.ItemByName("Name").Value
obj_PropertyValue = PropertyCollection.Item(j).Value
Next
' 描述出对象来
obj_Description = obj_ClassName & "(" & Chr(34) & obj_PropertyName & ":=" & obj_PropertyValue & Chr(34) & ")"
' 添加到Dictionary中
obj_Dic.Add obj_Name, obj_Description
Next
' 补全对象的在对象树中的路径(把父对象加在前面)
Set obj_DicWithFullName = obj_Dic
For i=1 to Object.Count
obj_Name = Object.Item(i).Value
obj_ParentName = ParentName.Item(i).Value
If obj_ParentName <> "" Then
' 这里作为Demo只考虑了有一个父亲的情况,对于有祖父、祖祖父...的情况,应该在XML中用“.”隔开,在这里读入并解析出来
obj_DicWithFullName.Item(obj_Name) = obj_Dic.Item(obj_ParentName) & "." & obj_Dic.Item(obj_Name)
Else
obj_DicWithFullName.Item(obj_Name) = obj_Dic.Item(obj_Name)
End If
Next
Set GetObjectFromXML = obj_DicWithFullName
End Function
从XML文件中取出测试用例并执行:
Function RunTestCaseFromXML(XMLFile,Obj_Dic)
Set Objects = XMLUtil.CreateXMLFromFile(XMLFile)
Set Object = Objects.ChildElementsByPath("/Framework/TestCase/Step/ObjectName")
Set ObjectOperation = Objects.ChildElementsByPath("/Framework/TestCase/Step/Operation")
Set ObjectOperationValue = Objects.ChildElementsByPath("/Framework/TestCase/Step/OperationValue")
For i=1 to Object.Count
obj_Name = Object.Item(i).Value
obj_ObjectOperation = ObjectOperation.Item(i).Value
obj_OperationValue = ObjectOperationValue.Item(i).Value
' 描述出对象执行的操作来
If obj_OperationValue="" Then
ObjectRun = Obj_Dic.Item(obj_Name) & "." & obj_ObjectOperation
Else
ObjectRun = Obj_Dic.Item(obj_Name) & "." & obj_ObjectOperation & " " & Chr(34) & obj_OperationValue & Chr(34) &""
End If
' 执行测试动作
Execute ObjectRun
Next
End Function
在QTP的Action中只需要三行代码:
SystemUtil.Run "C:/Program Files/HP/QuickTest Professional/samples/flight/app/flight4a.exe"
Set objs = GetObjectFromXML("ObjectTree.xml")
RunTestCaseFromXML "ObjectTree.xml",objs