在QTP中使用Dictionary对象来替代环境变量
2010-01-08 23:07
423 查看
Dictionary对象经常用来存储对象
,把Dictionary添加到注册表中QTP的保留对象
,则可以用于替代QTP的环境变量(Environment),在Action之间共享数据
。
下面的脚本摘自QTP的CodeSamplesPlus并做了点修改,添加了点注释:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'As an alternative to using environment variables to share values between actions
' you can use the Dictionary object.
'The Dictionary object enables you to assign values to variables that are accessible from all actions (local and external)
'called in the test in which the Dictionary object is created.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 把Dictionary添加到注册表,这样可以在使用GlobalDictionary时有Intelisence智能感应提示
'in order to have intelisence for the Dictionary object, and have it recognized by other actions, it is added to the registry
Dim WshShell
Set WshShell =CreateObject("WScript.Shell")
WshShell.RegWrite "HKCU/Software/Mercury Interactive/QuickTest Professional/MicTest/ReservedObjects/GlobalDictionary/ProgID", "Scripting.Dictionary","REG_SZ"
Set WshShell = Nothing
'clearing the Keys if they exist in the object 清空Dictionary中的项
If GlobalDictionary.Exists("AgentName") Then
GlobalDictionary.Remove("AgentName")
End If
If GlobalDictionary.Exists("Password") Then
GlobalDictionary.Remove("Password")
End If
If GlobalDictionary.Exists("OrderNumber") Then
GlobalDictionary.Remove("OrderNumber")
End If
'add 3 keys to the Dictionary object 添加项
GlobalDictionary.Add "AgentName", "Mercury"
GlobalDictionary.Add "Password","Mercury"
GlobalDictionary.Add "OrderNumber", 0
' 使用GlobalDictionary中的数据
'login to Mercury Flight application using the Dictionary objects we just defined.
Dialog("Login").WinEdit("Agent Name:").Set GlobalDictionary.Item("AgentName")
Dialog("Login").WinEdit("Agent Name:").Type micTab
Dialog("Login").WinEdit("Password:").SetSecure GlobalDictionary.Item("Password")
Dialog("Login").WinButton("OK").Click
'inserting an order in the flight application
Window("Flight Reservation").WinObject("Date of Flight:").Type "111111"
Window("Flight Reservation").WinComboBox("Fly From:").Select "Denver"
Window("Flight Reservation").WinComboBox("Fly To:").Select "Frankfurt"
Window("Flight Reservation").WinButton("FLIGHT").Click
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
Window("Flight Reservation").WinEdit("Name:").Set "6"
Window("Flight Reservation").WinRadioButton("Business").Set
Window("Flight Reservation").WinButton("Insert Order").Click
'Window("Flight Reservation").ActiveX("Threed Panel Control").WaitProperty "text", "Insert Done...", 10000
Window("Flight Reservation").WinObject("AfxWnd40").WaitProperty "text", "Insert Done...", 10000
'saving the Order number in the Datatable, and then saving it in a dictionary item
Window("Flight Reservation").WinEdit("Order No:").Output CheckPoint("Order No:")
GlobalDictionary.Item("OrderNumber") = datatable.Value ("Order_No_text_out",dtGlobalSheet)
'closing the application
Window("Flight Reservation").Close
'reports to the report all the Keys & items found in the dictionary object
For i=0 to GlobalDictionary.Count-1
KeysArray = GlobalDictionary.keys
ItemsArray = GlobalDictionary.Items
reporter.ReportEvent Done,"Reporting Dictionary Item Number : " & i , "Key : " & KeysArray(i) & " , Item : " & ItemsArray(i)
Next
' 调用Action2
RunAction "Action2", oneIteration
Action2的代码:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This code segment also exists in the main Action.
' in this reusable action, the value are avaiable ONLY when they are called from Test "Dictionary"
' calling this action as a stand alone, will result in an error since those Keys & values won't be valid.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GlobalDictionary中的数据可以跨Action使用
'reports to the report all the Keys & items found in the dictionary object
For i=0 to GlobalDictionary.Count-1
KeysArray = GlobalDictionary.keys
ItemsArray = GlobalDictionary.Items
reporter.ReportEvent Done,"Reporting Dictionary Item Number : " & i , "Key : " & KeysArray(i) & " , Item : " & ItemsArray(i)
Next
,把Dictionary添加到注册表中QTP的保留对象
,则可以用于替代QTP的环境变量(Environment),在Action之间共享数据
。
下面的脚本摘自QTP的CodeSamplesPlus并做了点修改,添加了点注释:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'As an alternative to using environment variables to share values between actions
' you can use the Dictionary object.
'The Dictionary object enables you to assign values to variables that are accessible from all actions (local and external)
'called in the test in which the Dictionary object is created.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 把Dictionary添加到注册表,这样可以在使用GlobalDictionary时有Intelisence智能感应提示
'in order to have intelisence for the Dictionary object, and have it recognized by other actions, it is added to the registry
Dim WshShell
Set WshShell =CreateObject("WScript.Shell")
WshShell.RegWrite "HKCU/Software/Mercury Interactive/QuickTest Professional/MicTest/ReservedObjects/GlobalDictionary/ProgID", "Scripting.Dictionary","REG_SZ"
Set WshShell = Nothing
'clearing the Keys if they exist in the object 清空Dictionary中的项
If GlobalDictionary.Exists("AgentName") Then
GlobalDictionary.Remove("AgentName")
End If
If GlobalDictionary.Exists("Password") Then
GlobalDictionary.Remove("Password")
End If
If GlobalDictionary.Exists("OrderNumber") Then
GlobalDictionary.Remove("OrderNumber")
End If
'add 3 keys to the Dictionary object 添加项
GlobalDictionary.Add "AgentName", "Mercury"
GlobalDictionary.Add "Password","Mercury"
GlobalDictionary.Add "OrderNumber", 0
' 使用GlobalDictionary中的数据
'login to Mercury Flight application using the Dictionary objects we just defined.
Dialog("Login").WinEdit("Agent Name:").Set GlobalDictionary.Item("AgentName")
Dialog("Login").WinEdit("Agent Name:").Type micTab
Dialog("Login").WinEdit("Password:").SetSecure GlobalDictionary.Item("Password")
Dialog("Login").WinButton("OK").Click
'inserting an order in the flight application
Window("Flight Reservation").WinObject("Date of Flight:").Type "111111"
Window("Flight Reservation").WinComboBox("Fly From:").Select "Denver"
Window("Flight Reservation").WinComboBox("Fly To:").Select "Frankfurt"
Window("Flight Reservation").WinButton("FLIGHT").Click
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
Window("Flight Reservation").WinEdit("Name:").Set "6"
Window("Flight Reservation").WinRadioButton("Business").Set
Window("Flight Reservation").WinButton("Insert Order").Click
'Window("Flight Reservation").ActiveX("Threed Panel Control").WaitProperty "text", "Insert Done...", 10000
Window("Flight Reservation").WinObject("AfxWnd40").WaitProperty "text", "Insert Done...", 10000
'saving the Order number in the Datatable, and then saving it in a dictionary item
Window("Flight Reservation").WinEdit("Order No:").Output CheckPoint("Order No:")
GlobalDictionary.Item("OrderNumber") = datatable.Value ("Order_No_text_out",dtGlobalSheet)
'closing the application
Window("Flight Reservation").Close
'reports to the report all the Keys & items found in the dictionary object
For i=0 to GlobalDictionary.Count-1
KeysArray = GlobalDictionary.keys
ItemsArray = GlobalDictionary.Items
reporter.ReportEvent Done,"Reporting Dictionary Item Number : " & i , "Key : " & KeysArray(i) & " , Item : " & ItemsArray(i)
Next
' 调用Action2
RunAction "Action2", oneIteration
Action2的代码:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This code segment also exists in the main Action.
' in this reusable action, the value are avaiable ONLY when they are called from Test "Dictionary"
' calling this action as a stand alone, will result in an error since those Keys & values won't be valid.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GlobalDictionary中的数据可以跨Action使用
'reports to the report all the Keys & items found in the dictionary object
For i=0 to GlobalDictionary.Count-1
KeysArray = GlobalDictionary.keys
ItemsArray = GlobalDictionary.Items
reporter.ReportEvent Done,"Reporting Dictionary Item Number : " & i , "Key : " & KeysArray(i) & " , Item : " & ItemsArray(i)
Next
相关文章推荐
- 深入Javascript函数与闭包(执行环境、变量对象与作用域链)使用详解
- 深入Javascript函数、递归与闭包(执行环境、变量对象与作用域链)使用详解
- QTP使用异步VBS进程并通过系统环境变量传值
- 【QTP】环境变量的使用
- 深入Javascript函数、递归与闭包(执行环境、变量对象与作用域链)使用详解
- 如何在QTP中添加和使用环境变量(1)
- 在QTP中使用Dictionary对象
- QTP环境变量的使用
- Linux上安装配置使用Tomcat说明文档和JDK环境变量配置
- Oracle中替代变量(动态参数)的使用
- 使用Spring application对象存储全局变量,统计链接的点击量
- 初学ubuntu(二)vi编辑器的使用和配置java环境变量
- 使用QTP管理wap页面对象心得小结 [ 光影人像 东海陈光剑 的博客 ]
- 在Java程序中使用系统环境变量
- Docker 生产环境之配置对象 - 配置对象使用自定义元数据
- 《Linux命令行与shell脚本》笔记--第5章:使用Linux环境变量
- gcc使用的一些环境变量
- jenkins 可以使用的环境变量
- 设置PATH环境变量,及CRegKey类的使用
- 把php.exe加入系统环境变量-使用命令行可快速执行