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. 【五一qbxt】day7-1 引水入城

    [noip2010 洛谷p1514]引水入城 Before: 线段覆盖问题#1:(我们所需要的) 一个区间,若干条线段,现在求最少多少条线段覆盖满整个区间 区间长度8,可选的覆盖线段[2,6],[1, ...

  2. TMS320C6455BCTZA 原厂订购 原装正品

    作为一家科研公司,保证芯片的原厂品质和正规采购渠道是科学严谨的研发工作中重要的一环,更是保证研发产品可靠.稳定的基础.而研发中所遇到的各种不可预测的情况更是每个工程师向技术的山峰攀登中时会遇到的各种难 ...

  3. javaweb各种框架组合案例(四):maven+spring+springMVC+spring data jpa(hibernate)【失败案例】

    一.失败案例 1. 控制台报错信息 严重: Exception sending context initialized event to listener instance of class org. ...

  4. 自定义 异步 IO 非阻塞框架

    框架一 自定义Web异步非阻塞框架 suosuo.py #!/usr/bin/env python # -*- coding: utf-8 -*-# # __name__ = Web_Framewor ...

  5. css3系列--概述

    CSS3分成了不同类别,称为“modules”.而每一个“modules”都有于CSS2中额外增加的功能,以及向后兼容.CSS3早于1999年已经开始制订.[18]直到2011年6月7日,CSS 3 ...

  6. AGC020C Median Sum

    高端操作qaq 又双叒叕读错题了= = 然后重新读题发现不会做了 于是瞅了一波题解 我靠要不要这么暴力呜呜呜 直接bitset O(n^3/w)QAQ 就是f[i]表示i是否能被搞出来 然后我们先不看 ...

  7. hive之压缩

    对数据进行压缩可以节约磁盘空间,提高系统吞吐量和性能,但是压缩和解压缩会增加CPU的开销. 1.hive的压缩编/解码器 BZip2和GZip压缩率高,但是需要消耗较多的CPU开销.LZO和Snapp ...

  8. 51nod 1384:全排列(STL)

    题目链接 记住next_permutation函数的用法,另外string在这里比char[]慢好多啊.. //#include<bits/stdc++.h> //using namesp ...

  9. CALayer的mask属性

    可以对图层按path进行指定裁剪 //#import "ViewController.h" // //@interface ViewController () // //@end ...

  10. PHP curl_init函数

    curl_init — 初始化一个cURL会话 说明 resource curl_init ([ string $url = NULL ] ) 初始化一个新的会话,返回一个cURL句柄,供curl_s ...