Monday, July 16, 2012

QTP - Dictionary Object


   QTP Supports Dictionary objects which will be used to store data in a Key & Item combination. Dictionary object is one of the feature of VB Script.We can read the Item from the dictionary object using the key. Also, dictionary objects support various operations for storing data effectively. we can even say dictionary objects are another form of dynamic array which becomes more handy than array.

Creating Dictionary Objects:
Dim oDict
Set oDict = CreateObject("scripting.dictionary")

We can create dictionary object using CreateObject method. In the above example oDict is the dictionary object created.

Adding values to Dictionary Object:

oDict.Add "Key1","Item1"
oDict.Add "Key2","Item2"

Read a item from Dictionary Object:

Msgbox oDict.Item("Key1")

Using Item method, we can read the item corresponding to a key.
"Item1" will be retrieved when executing the above line.

Read all items from Dictionary Object:

For each vItem in oDict.Items
   msgbox vItem
Next


Removing a key from Dictionary Object:

oDict.Remove ("Key1")

When executing the above the line, the "Key1" key along with it's item will be removed from the Dictionary.

Removing all keys from Dictionary Object:
oDict.RemoveAll

Changing a key in Dictionary Object:

oDict.Key("Key1") = "Key3" 'Set key for Key1 to Key3
Msgbox oDict.Item("Key3")


In the above lines, we are assigning/changing the 'Key1' to 'Key3'. In the Msgbox 'Item1' will be displayed as it's corresponding Key name is changed to 'Key3'.
If you read all the keys from the Dictionary then 'Key3' & 'Key2' will be retrieved and there won't be any key with 'Key1' name as it's changed.

Well, we have already seen how to read all the Items from the dictionary. Here is the script to read all the Keys from the dictionary.

For each vKey in oDict.Keys
 msgbox vKey
Next


You are having a question rite? I am able hear that you are asking whether Dictionary objects supports to check if a specific key is present in the dictionary or not. Here is the way to do that..

Msgbox oDict.Exists("Key3")      'It will Return True
Msgbox oDict.Exists("Key1")      'It will Return False


It will return either True/False based on the presence of the Key in the dictionary.
Also, we can get the count of the key/Item pair exists in the dictionary.

Msgbox oDict.Count   'It will Return 2

One more advantange of dictionary object is that we can use any number of dictionary objects in our script and we can have any number of items in the dictionary as well. How easy and useful this dictionary object is!!!  Well, we can create nested dictionary object also which we will see in another post.. :)

Wednesday, July 4, 2012

Using VBS file in QTP


     Hello All! In this post we are going to see how to execute VBS files in QTP. Before getting started, VBS is a VBScript file contains block of VBscript that will be used by QTP Tests.  In most cases, we will be having the user defined functions and global variables in VBS files.

     There are two ways by which we can execute VBS files using QTP. One simple way is that add the VBS files to the resources list of the test. By doing so, all the VBS files assicated as resources will be executed when we start the QTP Test execution.

To Add VBS to Resources:
1. Select File - > Settings
2. In Test Settings dialog, select 'Resources' tab.
3. Click on the "+" symbol and add the VBS files to resources.

   The second method is "ExecuteFile" function.
Syntax:
ExecuteFile "<VBS File Name with Path>"
Ex:
ExecuteFile "C:\Test.VBS"

We can use ExecuteFile method to load a VBS file during run time. Where as the first method will be used to execute the VBS files when the QTP Script execution starts.