vb script objects

4

Click here to load reader

Upload: gcreddy

Post on 30-May-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: VB Script Objects

8/9/2019 VB Script Objects

http://slidepdf.com/reader/full/vb-script-objects 1/4

QTP Training

VB Script Objects

************************************************

Visit:

www.gcreddy.com

for QTP Scripts and Other Info

************************************************

Dictionary Object

 Dictionary Object that stores data key, item pairs.

A Dictionary object is the equivalent of a PERL associative array/Hash

Variable. Items can be any form of data, and are stored in the array.

Each item is associated with a unique key. The key is used to retrievean individual item and is usually an integer or a string, but can be

anything except an array.

Creating a Dictionary Object:

Set objDictionary = CreateObject("Scripting.Dictionary") Dictionary Objects Methods:

Add Method 

Adds a key and item pair to a Dictionary object

Exists Method

Returns true if a specified key exists in the Dictionary object, false if itdoes not.

gcreddy 1

Page 2: VB Script Objects

8/9/2019 VB Script Objects

http://slidepdf.com/reader/full/vb-script-objects 2/4

QTP Training

Items Method

Returns an array containing all the items in a Dictionary object.

Keys Method

Returns an array containing all existing keys in a Dictionary object.

Remove Method

Removes a key, item pair from a Dictionary object.

RemoveAll Method

The RemoveAll method removes all key, item pairs from a Dictionaryobject.

 Example:

Dim citiesSet cities = CreateObject("Scripting.Dictionary")

cities.Add "h", "Hyderabad"cities.Add "b", "Bangalore"

cities.Add "c", "Chennai"

Dictionary Objects Properties:Count Property

Returns the number of items in a collection or Dictionary object. Read-

only.

CompareMode Property

Sets and returns the comparison mode for comparing string keys in aDictionary object.

Key Property

Sets a key in a Dictionary object.

Item Property

Sets or returns an item for a specified key in a Dictionary object. Forcollections, returns an item based on the specified key. Read/write.

gcreddy 2

Page 3: VB Script Objects

8/9/2019 VB Script Objects

http://slidepdf.com/reader/full/vb-script-objects 3/4

QTP Training

 Examples:

1) add Elements  AddDictionary

Set  objDictionary  =  CreateObject("Scripting.Dictionary")

objDictionary.Add  "Printer  1",  "Printing" objDictionary.Add  "Printer  2",  "Offline"

objDictionary.Add  "Printer  3",  "Printing"

  2) Delete All Elements fromDictionary

Set  objDictionary  =  CreateObject("Scripting.Dictionary")

objDictionary.Add  "Printer  1",  "Printing" objDictionary.Add  "Printer  2",  "Offline"objDictionary.Add  "Printer  3",  "Printing"

colKeys  =  objDictionary.Keys

Wscript.Echo  "First  run:  "

For  Each  strKey  in  colKeys

  Wscript.Echo  strKeyNext

objDictionary.RemoveAllcolKeys  =  objDictionary.Keys

Wscript.Echo  VbCrLf   &   "Second  run:  "For  Each  strKey  in  colKeys  Wscript.Echo  strKeyNext

3) Delete One Element from a Dictionary

Set objDictionary = CreateObject("Scripting.Dictionary")

objDictionary.Add "Printer 1", "Printing"

objDictionary.Add "Printer 2", "Offline"objDictionary.Add "Printer 3", "Printing"

gcreddy 3

Page 4: VB Script Objects

8/9/2019 VB Script Objects

http://slidepdf.com/reader/full/vb-script-objects 4/4

QTP Training

colKeys = objDictionary.Keys

Wscript.Echo "First run: "

For Each strKey in colKeys

Wscript.Echo strKeyNext

objDictionary.Remove("Printer 2")

colKeys = objDictionary.Keys

Wscript.Echo VbCrLf & "Second run: "

For Each strKey in colKeysWscript.Echo strKey

Next

4) List the Number of Items in a Dictionary

Set  objDictionary  =  CreateObject("Scripting.Dictionary")

objDictionary.Add  "Printer  1",  "Printing" objDictionary.Add  "Printer  2",  "Offline"objDictionary.Add  "Printer  3",  "Printing"

Wscript.Echo  objDictionary.Count

5) Verify the Existence of a Dictionary Key

Set objDictionary = CreateObject("Scripting.Dictionary")

objDictionary.Add "Printer 1", "Printing"

objDictionary.Add "Printer 2", "Offline"objDictionary.Add "Printer 3", "Printing"

If objDictionary.Exists("Printer 4") Then

Wscript.Echo "Printer 4 is in the Dictionary."ElseWscript.Echo "Printer 4 is not in the Dictionary."

End If 

gcreddy 4