QTP使用dictionary 对象

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

时间: 2024-10-29 19:11:12

QTP使用dictionary 对象的相关文章

JavaScript如何创建dictionary对象

对于JavaScript来说,其自身的Array对象仅仅是个数组,无法提供通过关键字来获取保存的数据,jQuery源码中提供了一种非常好的方式来解决这个问题,先看一下源码: function createCache() { var keys = []; function cache(key, value) { // Use (key + " ") to avoid collision with native prototype // properties (see Issue #157

VB中的Dictionary对象

VB中的Dictionary对象 核心归纳:1.更换键名,用obj.key("xx")="newxx"2.更换键值或访问指定键: (1)重设键值:obj.item("xx")="newxx", (2)取得键值:obj.item("xx")="newxx" 中的KEY键“xx”未设定时,会在对象的后面增加一个键值对.3.获得条目数:obj.count (从1开始)4.增加键值对:obj.a

VBS基础篇 - Dictionary对象

Dictionary是存储数据键和项目对的对象,其主要属性有Count.Item.Key,主要方法有Add.Exists.Items.Keys.Remove.RemoveAll. '建立字典 Dim Dict : Set Dict = CreateObject("Scripting.Dictionary") '添加键值对 Dict.Add "Key1", "Item1" Dict.Add "Key2", "Item2

Dictionary 对象

Dictionary 对象 对象的存储数据键/项对. 语法 Scripting.Dictionary 说明 Dictionary对象相当于 PERL 关联数组. 项目,可以是任何形式的数据,存储在数组中. 每个项目相关联的唯一键. 密钥用于检索单个项目和通常是整数或一个字符串,但可以是任何除数组. 以下代码演示如何创建Dictionary对象. Dim d 'Create a variable Set d = CreateObject(Scripting.Dictionary) d.Add "a

使用QTP测试Web对象

加载Web插件先启动QTP,再启动浏览器,否则Web元素识别不了最新版本QTP11支持的浏览器:IE:6.7.8Firefox:3.0.x.3.5.QTP支持直接访问DOM(Document Object Model),可以通过DOM来访问HTML标签.例如:On Error Resume Next Set Doc = Browser("Google").Page("Google").Object '  循环获取Page中的所有对象For Each Element

delphi使用VB的dictionary对象

var Dict:Variant; n,ncount,i:byte; kArr,iArr:array of variant; Ts,Ts2:TStringList; kv,iv,FGF:string; procedure TForm1.Button1Click(Sender: TObject); begin Dict:= CreateoleObject('scripting.dictionary'); Ts:=tstringlist.Create;  Ts2:=tstringlist.Creat

VBScript/QTP 的常用COM对象列表

众所周知,我们经常在脚本中创建一些对象来实现某些特定的功能.尤其是当我们使用QTP的描述性编程时,需要创建这些对象. 下边是我们经常在QTP或VBScript中用到的对象列表: Set objEmail = CreateObject("CDO.Message" ) Set objIE = CreateObject("InternetExplorer.Application" ) Set objInet = CreateObject("InetCtls.In

JavaScript中创建字典对象(dictionary)实例

这篇文章主要介绍了JavaScript中创建字典对象(dictionary)实例,本文直接给出了实现的源码,并给出了使用示例,需要的朋友可以参考下 对于JavaScript来说,其自身的Array对象仅仅是个数组,无法提供通过关键字来获取保存的数据,jQuery源码中提供了一种非常好的方式来解决这个问题,先看一下源码: 复制代码代码如下: function createCache() { var keys = []; function cache(key, value) {  // Use (k

QTP学习笔记1

QTP中获取当前路径 实属转帖,方便以后查找~ QTP9.2版本,以下内容适合QTP和vbs. 路径一:QTP的测试当前路径 environment("TestDir") 使用environment("TestDir")能获取当前测试的绝对路径,不包括最后的"\" 方法: Path = environment("TestDir") 路径二:QTP安装目录下bin目录的路径 WshShell.CurrentDirectory (