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

QTP使用dictionary 对象的更多相关文章

  1. VBS基础篇 - Dictionary对象

    Dictionary是存储数据键和项目对的对象,其主要属性有Count.Item.Key,主要方法有Add.Exists.Items.Keys.Remove.RemoveAll. '建立字典 Dim ...

  2. VB中的Dictionary对象

    VB中的Dictionary对象 Dictionary对象不是VBA或Visual Basic实时语言的具体存在的部分,它是存在于Microsoft Scripting Runtime Library ...

  3. 存储数据键和项目对的类(Dictionary对象)

    存储数据键和项目对的类(Dictionary对象) <% Class Dictionary Public Copyright, Developer, Name, Version, Web Pri ...

  4. Dictionary 对象

    Dictionary 对象 对象的存储数据键/项对. 语法 Scripting.Dictionary 说明 Dictionary对象相当于 PERL 关联数组. 项目,可以是任何形式的数据,存储在数组 ...

  5. WebAPI接口返回ArrayList包含Dictionary对象正确解析

    一.问题提出 为了减少流量,将key-value(键值对)直接输出到Dictionary<string, string>,接口返回结果如下: 其中{}里面内容如下: 上图显示600是键,4 ...

  6. 使用QTP测试Web对象

    加载Web插件先启动QTP,再启动浏览器,否则Web元素识别不了最新版本QTP11支持的浏览器:IE:6.7.8Firefox:3.0.x.3.5.QTP支持直接访问DOM(Document Obje ...

  7. [转载]使用QTP测试Windows对象

    Desktop对象的使用: 通过Desktop对象,可以访问Windows的桌面顶层对象.Desktop对象包括CaptureBitmap.ChildObjects.RunAnalog方法. (1)  ...

  8. QTP设置共享对象库

    第一步:把需要加到共享对象库中的各个用例脚本的对象库,分别导出成.tsr文件. 操作方法:先用QTP打开已经录制完毕的脚本后,选择Resources-->Object Repository.然后 ...

  9. QTP VBScript RegExp对象的运用

    下面来讲讲RegExp对象: Vbs提供了针对正则表达式的一个非常实用的类,就是RegExp Global属性:代表全局匹配 IgnoreCase属性:大小写忽略 Pattern属性:正则表达式 Ex ...

随机推荐

  1. CentOS7安装MySQL5.7.20

    参考1:https://www.cnblogs.com/technology-huangyan/p/10484838.html 参考1:https://blog.csdn.net/aiyowei110 ...

  2. 修改ps工具栏字体大小

     修改ps工具栏字体大小 先改电脑分辨率或者改首选项--界面---文字,退出后,重新打开,但你会发现问题还是没解决,我们接着往下  找到文件夹安装目录下的photoshops.exe启动文件(查找方法 ...

  3. ORACLE数据字典DBA视图

    select  *  from  DBA_ALL_TABLES   --显示数据库中所有表的描述 select  *  from  DBA_CATALOG  --列出所有数据库标,视图,同义词和序列 ...

  4. ROT13加密和解密

    问题 ROT13(回转13位)是一种简易的替换式密码算法.它是一种在英文网络论坛用作隐藏八卦.妙句.谜题解答以及某些脏话的工具,目的是逃过版主或管理员的匆匆一瞥.ROT13 也是过去在古罗马开发的凯撒 ...

  5. JS面向对象——工厂模型

    工厂模型抽象了创建具体对象的过程,以下示例代码说明工厂类型: <!DOCTYPE html> <html> <head> <title>工厂模型< ...

  6. LeetCode Array Easy 485. Max Consecutive Ones

    Description Given a binary array, find the maximum number of consecutive 1s in this array. Example 1 ...

  7. R语言ggplot2软件包

    相比r语言自带软件包,ggplot2有以下特色 图形语法的核心:统计图形是数据向几何对象属性的一个映射.

  8. Elasticsearch单机安装_集群搭建_索引基本操作_Head插件安装与基本操作_ik分词器配置_logstash实现数据同步_教程

    一.Elasticsearch单机安装 1.将es安装包传到服务器上 这是安装包 这里我是用的是WinSCP上传工具,上传到/home/plugins文件下. 进入Xshell,验证一下是否上传成功. ...

  9. HDU 5634 线段树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5634 题意:给定一个长度为n的序列,有m次操作.操作有3种: 1 l,r :区间[l,r]的值变成ph ...

  10. windows和mtu值修改

    前言 有时候我们需要修改mtu值来对付乱七八糟的网络问题 windows修改方法 1.netsh interface ipv4 show subinterfaces 查询到目前系统的MTU值 2.ne ...