1. 创建即使用Dictionary对象

' 创建Dictionary对象
Set Dic = CreateObject("Scripting.Dictionary")
' 添加Dictionary的key和value
For Iterator = 1 To 3 Step 1
 Dic.Add CStr(Iterator),Iterator & "_Value"
Next
' 循环读取Dictionary的key和value
For Iterator = 1 To Dic.Count Step 1
 Msgbox  Dic.Item(CStr(Iterator))
Next

DicArray = Dic.Items
'For I = 0 To Dic.Count
For I = 0 To UBound(DicArray)
 Msgbox DicArray(I)
Next

' 判断是否存在某个Key,如果存在,则把其去掉
If Dic.Exists("2") Then
 Msgbox Dic.Item("2")
 Dic.Remove("2")
End If
Msgbox Dic.Count
' 清空所有 Key和Value
Dic.RemoveAll()
Msgbox Dic.Count

2. 把Dictionary添加到注册表中QTP的保留对象

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

最新文章

  1. Android自定义Dialog(美化界面)
  2. TranslateAnimation 运行动画后实际位置不正确问题
  3. MyEclipse调用Matlab打包函数
  4. September 16th 2016 Week 38th Friday
  5. 初识python中的类与对象
  6. 使用repeater实现gridview的功能
  7. iOS webView的一些基本用法
  8. JPA的泛型DAO设计及使用
  9. 有哪些适合学生参与的 C++,网络编程方面的开源项目?
  10. Bash中的特殊变量和位置参量
  11. bzoj1492[NOI2007]货币兑换Cash cdq分治+斜率优化dp
  12. GNU make 汇总
  13. 基于【CentOS-7+ Ambari 2.7.0 + HDP 3.0】搭建HAWQ数据仓库04 —— 安装HAWQ插件PXF3.3.0.0
  14. Snort规则大探秘
  15. Oralce分析函数
  16. easyUI combobox下拉框很长,easyUI combobox下拉框如何显示滚动条的解决方法
  17. 79.ZYNQ内部私有定时器中断
  18. Python下json中文乱码解决办法
  19. Servlet------>jsp jstl核心标签库
  20. B. Mike and Fun---cf548B(暴力求解)

热门文章

  1. Android应用程序开发之图片操作(二)——工程图片资源的加载及OOM的处理
  2. 关于JSON.stringify()与JSON.parse()
  3. SpringCloud-Eureka-服务注册是如何发起的
  4. JavaScript中的柯里化
  5. Fabric管理组件的使用
  6. DFSORT
  7. react-jsx和数组
  8. Session过期,如何跳出iframe框架页的问题
  9. 每天一个linux命令:which(17)
  10. POJ 3348 Cows (凸包模板+凸包面积)