QTP对象映射应用-自动化测试
更新时间:2022-05-25 09:03:03 作者:多测师 浏览:115
一. 自动义对象应用
当要操作的一些对象不识别时,且这些对象可以映射成标准Window控件时,则可以考虑自定义这些对象,并映射到相似的Window控件。这样就可以调用标准Window控件运行的方法来操作自定义对象了。
要映射成相似的Window控件,则自定义的对象要有和标准Window控件相似的功能和操作。
此法可以处理一些看起来像是标准Window控件的对象,但并不是所有这类对象都可行。如Delphi控件,大部分可以如此处理,但一些控件则不行哦。
二. 自动义对象如何使用
首先,需要对不识别的控件进行自定义,并做好与标准window控件的映射;
其次,可以通过录制或描述性编辑调用这些自定义的对象;
然后,就是像对待能识别的对象一样进行操作即可。
注意:
在移动脚本运行时(比如拷贝脚本至其他虚拟机运行),需要先运行自定义对象的定义脚本,然后再运行脚本。
最好的方法是在要使用时,脚本中运行定义自定义对象的脚本,执行完成后则删除自定义的对象,恢复到原有配置。而由于QTP不提供删除单个自定义对象的功能,而提供了恢复默认配置的功能,因此只好使用恢复到默认对象配置的功能了。
例:添加一个自定义对象的定义脚本如下:
DimApp 'As Application
‘如果是在已经运行的QTP中运行,则可以用GetObject获取运行的QTP程序对象
‘如果是直接用VBS运行QTP,则可以用CreateObject建立QTP程序对象。
SetApp =GetObject("","QuickTest.Application")
‘建立一个自定义对象“ttoolbar”,并映射到标准控件“WinToolbar”
App.Options.ObjectIdentification("WinToolbar").CreateUserDefinedObject("ttoolbar")
App.Options.ObjectIdentification("ttoolbar").OrdinalIdentifier = "location"
App.Options.ObjectIdentification("ttoolbar").MandatoryProperties.RemoveAll()
App.Options.ObjectIdentification("ttoolbar").MandatoryProperties.Add("nativeclass")
App.Options.ObjectIdentification("ttoolbar").MandatoryProperties.Add("text")
App.Options.ObjectIdentification("ttoolbar").AssistiveProperties.RemoveAll()
App.Options.ObjectIdentification("ttoolbar").AssistiveProperties.Add("window id")
App.Options.ObjectIdentification("ttoolbar").EnableSmartIdentification =False
App.Options.ObjectIdentification("ttoolbar").BaseFilterProperties.RemoveAll()
App.Options.ObjectIdentification("ttoolbar").OptionalFilterProperties.RemoveAll()
以上脚本也可以通过“Generate Script...”导出,然后删掉不是自定义的对象定义脚本,并稍微修改一下即可。
恢复默认对象配置的方法如下:
App.Options.ObjectIdentification.ResetAll
三. 如何自定义对象
1. 选择“Tools”>>“Object Identification”;
2. 在“Environment”框中选择“Standard Windows”,则“User-Defined…”按钮将变为可用;
3. 点击“User-Defined…”按钮,将显示“Object Mapping”对话框;
4. 单击指向手,然后单击要将其类作为用户定义的类添加的对象。用户定义的对象的名称将显示在“Class Name”框中。(其实这个“Class Name”框也可以自己填,如果自己清楚该填什么的话)
提示:按住Ctrl键,可以更改窗口焦点或执行右键单击或鼠标悬停(以显示上下文菜单)等操作。注意:按Ctrl键时,您不能从Windows任务栏中选择应用程序,因此,必须确保要访问的窗口没有最小化。
5. 在“Map to”框中,选择要将用户定义的对象类映射到的标准对象类,然后单击“Add”。类名和映射将添加到对象映射列表中。
6. 如果要将其他对象映射到标准类,请对每个对象重复步骤4-5。
7. 单击“OK”。“Object Mapping”对话框关闭,您的对象作为用户定义的测试对象添加到标准Windows测试对象类列表中。注意:您的对象具有角上带有一个红色U的图标,标识它为用户定义的类。
8. 为用户定义的对象类配置对象标识设置,方式与任何其他对象类一样。
四. 例子
以下举一个例子,就拿我们公司正在用的Delphi开发的“XX系统”为例吧:
*操作如下:点击ToolBar中的打开按钮->在文本区域写入内容->点击ToolBar中的关闭按钮->关闭窗口
*XX系统的界面就不给了,不好意思。
[直接录制]如果不加载插件,则QTP对Delphi程序是不识别的,录制的代码如下:
**************************************************************************************
Window("XX系统").Activate
Window("XX系统").WinObject("TToolBar").Click 266,39
Window("XX系统").WinObject("TNoteEditor").Click 371,50
Window("XX系统").WinObject("TNoteEditor").Type "fdsfasd"
Window("XX系统").WinObject("TToolBar").Click 570,32
Window("XX系统").WinObject("TToolBar").Click 327,36
Window("XX系统").Close
**************************************************************************************
[定义自定义控件再录制]
*定义自定义控件:
*定义自定义控件后再录制的脚本如下:
**************************************************************************************
Window("XX系统").Activate
Window("XX系统").WinToolbar("TToolBar").Press "打开"
Window("XX系统").WinEditor("TNoteEditor").Type "fdsafsdafdas"
Window("XX系统").WinToolbar("TToolBar").Press "保存"
Window("XX系统").WinToolbar("TToolBar").Press "关闭"
Window("XX系统").Close
**************************************************************************************
[改造后的脚本]
*稍微改造一下,脚本如下,这样就可以自动调用程序进行笔录操作,然后关闭程序:
**************************************************************************************
SystemUtil.Run "C:\Program Files\XXXXsoft\ XX系统\P_XXXX.exe"
‘这里只是为了说明如何使用映射,不然以下这个循环会有一定的问题的哦,需要改造一下。
Do while not Dialog("提示").WinButton("确定").Exist(0)
wait 1
Loop
Dialog("提示").WinButton("确定").Click
Window("XX系统").Activate
Window("XX系统").WinToolbar("TToolBar").Press "打开"
Window("XX系统").WinEditor("TNoteEditor").Type "dfdghfdgfsgfds"
Window("XX系统").WinEditor("TNoteEditor").Type micReturn
Window("XX系统").WinEditor("TNoteEditor").Type "gfdsgfd"
Window("XX系统").WinEditor("TNoteEditor").Type micReturn
Window("XX系统").WinToolbar("TToolBar").Press "保存"
Window("XX系统").WinToolbar("TToolBar").Press "关闭"
window("XX系统").Close
**************************************************************************************
*如果是在其他机器上运行,则还需要先运行以下定义自定义控件的代码:
**************************************************************************************
Dim App 'As Application
Set App = GetObject("","QuickTest.Application")
'Configuration of user-defined objects
'Object identification configuration for user-defined object "ttoolbar"
App.Options.ObjectIdentification("WinToolbar").CreateUserDefinedObject("ttoolbar")
App.Options.ObjectIdentification("ttoolbar").OrdinalIdentifier = "location"
App.Options.ObjectIdentification("ttoolbar").MandatoryProperties.RemoveAll()
App.Options.ObjectIdentification("ttoolbar").MandatoryProperties.Add("nativeclass")
App.Options.ObjectIdentification("ttoolbar").MandatoryProperties.Add("text")
App.Options.ObjectIdentification("ttoolbar").AssistiveProperties.RemoveAll()
App.Options.ObjectIdentification("ttoolbar").AssistiveProperties.Add("window id")
App.Options.ObjectIdentification("ttoolbar").EnableSmartIdentification = False
App.Options.ObjectIdentification("ttoolbar").BaseFilterProperties.RemoveAll()
App.Options.ObjectIdentification("ttoolbar").OptionalFilterProperties.RemoveAll()
'Object identification configuration for user-defined object "tnoteeditor"
App.Options.ObjectIdentification("WinEditor").CreateUserDefinedObject("tnoteeditor")
App.Options.ObjectIdentification("tnoteeditor").OrdinalIdentifier = "location"
App.Options.ObjectIdentification("tnoteeditor").MandatoryProperties.RemoveAll()
App.Options.ObjectIdentification("tnoteeditor").MandatoryProperties.Add("attached text")
App.Options.ObjectIdentification("tnoteeditor").MandatoryProperties.Add("nativeclass")
App.Options.ObjectIdentification("tnoteeditor").AssistiveProperties.RemoveAll()
App.Options.ObjectIdentification("tnoteeditor").AssistiveProperties.Add("window id")
App.Options.ObjectIdentification("tnoteeditor").EnableSmartIdentification = False
App.Options.ObjectIdentification("tnoteeditor").BaseFilterProperties.RemoveAll()
App.Options.ObjectIdentification("tnoteeditor").OptionalFilterProperties.RemoveAll()
**************************************************************************************
*在脚本运行结束时需要运行以下脚本,以清除自己的配置,防止影响别人脚本的运行:
**************************************************************************************
App.Options.ObjectIdentification.ResetAll
Set App = Nothing
**************************************************************************************
以上内容为大家介绍了自动化测试中的QTP对象映射应用,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/
下一篇:初学QTP的一点总结-自动化测试