Dictionary Object in UFT

Learn Dictionary Object in UFT or QTP Quickly

Dictionary Object A fantastic Approach to solve Key Value Pair in QTP/UFT

Do you know how to use Dictionary Object in UFT? How we utilize the key-value pair to make the program more dynamic? If your answer is no, stick to this post till last to understand in detail.

Dictionary Object:

Dictionary Object is a Microsoft product and we can use it in UFT/QTP. The key and value both are mandatory for Dictionary Object. Items /elements can be stored in the array. Each member of this array is having a unique key. The key is used to retrieve detailed information.

There are numerous approaches to utilize Dictionary Objects however the most well-known utilization of Dictionary Object is to keep the incentive in an Array and allude each an incentive with a one of a kind Key or Index.

Fundamentally, Dictionary Object is only an Array that contains values that are alluded by one of a kind keys. For each incentive in Dictionary Object, there is one of a kind key related to esteem. Utilizing Key name, we can call related esteem anyplace in the content.

This is the primary contrast among Array and Dictionary Object. An Array can have filed an incentive as numeric though, in word reference object, file esteem can be a string. Consequently while alluding any an incentive with key, a key can have any name related to esteem.

Exception:

It can store all possible values–string, integer, etc but can not store an array.

Where it can be used?

Model-1

This is a unique coding technique where the fields are getting changed but the total no of fields is given. The edit box sequence is dynamic. They may appear without any order.
Total number of questions is fixed -5

Scenario-1:
The sequence of questions are : 5,3,2,1,4

Scenario-2:
The sequence of questions are : 1,3,2,5,4

Scenario-3:
The sequence of questions are : 5,3,1,2,4
and so on…

Likewise, we can have 5!=120 possible combinations for 5 questions. If the question set is more like 10. Then the possible combinations will be 10!=3628800
Developers introduced these challenge questions just to make sure the site is not accessed by bot. If we have to test this kind of application, we need to create these many scripts along with data. Which looks impossible and makes no sense to automate.

Also, it is not cost-effective. Now to resolve this we can use the Dictionary object. Instead of creating so many combinations, we will read 10 questions and store them in some model-X along with the test data that needs to be inserted.

Once we get the question from UAT, we will pass the question to the model-X,model-X will intern return me the test data. Our test script will then sendKeys those values to the UAT. The model-X is termed as a Dictionary object where the questions will be keys and the corresponding test data will be the values.

READ  Concept of Absolute Path and Relative Path In QTP

The dictionary object was utilized with the index(key) being a string. On account of cluster or Array, the list can be ONLY numeric. At that point, we have some undeniable preferences like referencing an incentive with a significant key and so forth. Hence we can not utilize array in these scenarios

Model-2:

When there is a lot of test data to be read and apply to a UAT, it is always better to use Dictionary Object as the data fetch from Excel will be inefficient as there would be too many data seek. Using of dictionary object will surely make the execution faster.

The dictionary object was utilized with the index(key) being a string. On account of cluster or Array, the list can be ONLY numeric. At that point, we have some undeniable preferences like referencing an incentive with a significant key and so forth. Hence we can not utilize array in these scenarios

Looking at Dictionaries and Arrays:

  • Keys are utilized to recognize the things in a Dictionary object
  • You don’t need to call him to change the extent of the Dictionary object
  • While erasing a thing from a Dictionary, the rest of the things will consequently move up
  • Dictionary Object can’t be multidimensional, Arrays can
  • Dictionary Object has more worked in capacities than Arrays
  • Lexicons work superior to exhibits on getting to irregular components much of the time
  • Dictionary Object work superior to clusters on finding things by their substance

 

The following code delineates the usage of a Dictionary object:

Dictionary Object in UFT is an important concept while making the code more dynamic. Please pay attention in the subsequent sections.

Create:

To Create a dictionary we need to use the following command:


Dim myVar // Create a variable.
Set myVar = CreateObject("Scripting.Dictionary")

Include/add Method

The Add strategy is utilized to include another key/thing pair to a Dictionary object.

Add methods accepts two arguments

  1. Key for the item
  2. Value or the item

Dim myVar // Create a variable.
Set myVar = CreateObject("Scripting.Dictionary")
myVar.Add "a", "Apple" // Add some keys and items.
myVar.Add "b", "Banana"
myVar.Add "c", "Chip"
There are 3 ways to Add to Dictionary
  • myVar.Add “1”,”Apple”
  • myVar.Item(“2″)=”Banana”
  • myVar(“3″)=”Chip”

Down to the post I have given the list of properties of a dictionary object. Item is a property of the Dictionary object and the third takes advantage of the default property of the Dictionary.

ComapreMode:

The compareMode is another important concept while studying Dictionary Object in UFT .

The CompareMode property is utilized to set and restore the key’s string examination mode which decides how keys are coordinated while turning upward or looking.

In simple words, it determines in which “mode” the dictionary’s Item property compares for equality while testing key. The options are:

  • Text(1:vbTestCompare)
  • Binary(0:vbBinaryCompare)
  • Database(2:vbDataBaseCompare)

Mostly during comparison, we take the default Text mode and checks with case insensitive.


myVar.CompareMode = vbTextCompare 
 

Exists Method

The Exists method is another important concept while studying Dictionary Object in UFT .

READ  How To Solve Synchronization Failures In Selenium or QTP?

The Exists() method or technique is utilized to decide if a key exists in the predefined Dictionary. Returns True if it does and false otherwise. It is important to check the existence of the key before we read an Item from Dictionary.


If myVar.Exists("c") Then
msg = "Specified key for the dictionary exists."
Else
msg = "Specified key for the dictionary doesn't exist."
End If
KeyExistsDemo = msg

Key Property

The Key property gives us a chance to change the key estimation of a current key/thing pair.The Keys strategy is utilized to retrieve the majority of the keys in a specific Dictionary article and store them in a cluster.

Thing/Item Property

The item property is another important concept while studying Dictionary Object in UFT .

The Item property enables us to retrieve the estimation of a thing in the accumulation assigned by the predefined key contention and furthermore to set that esteem by utilizing item value. It gives an insight on data storage inside Dictionary objects.

The below things can be done using Item Property:

  1. We can add a new item to Dictionary Object(I have shown already)
  2. We can update an item in Dictionary Object(Same technique)
  3. We can read an Item from Dictionary Object
myValue=dictionary.Item("2")

Things Method

The Things method is another important concept while studying Dictionary Object in UFT.

The Items strategy is utilized to retrieve the majority of the things in a specific Dictionary article and store them in an exhibit.

a = myVar.Items ' Get the items.
For i = 0 To myVar.Count -1 ' Iterate the array.
s = s & a(i) & "
" ' Create return string.
Next
DicDemo = s

Evacuate/Remove Method

The Evacuate/Remove method is another important concept while studying Dictionary Object in UFT .

The Remove technique is utilized to evacuate a solitary key/thing pair from the predetermined Dictionary object.It is also important to use an Exist() method before use of remove() method. Otherwise it will throw an error.

RemoveAll Method

The removal technique is utilized to expel all the key/thing sets from the predefined Dictionary object.

myVar.Remove("b") ' Remove second pair.
a = myVar.RemoveAll ' Clear the dictionary. 

Summary of Dictionary Object:

This table gives details on Dictionary Object in UFT.

Method or Property Return Description
Methods
Add NA Adds a new member to the dictionary
Exists Boolean true false test if a given key is in
the dictionary
Items array Returns all the values in a dictionary in the form of an array
Keys array Returns all the keys in a dictionary in the form of an array
Remove NA Removes the item corresponding to the
given key
RemoveAll NA make the dictionary empty by removing all members
Properties
Count int Returns the number of items in a
dictionary
Item Variant Sets and returns an item for the
specified key
Key NA Changes an item’s key

Difference between Array and Dictionary Object in VBScript or UFT?

Understanding Array and Dictionary object difference is another important concept while studying Dictionary Object in UFT.

Imagine a table like below:

1 2
Name Roll Phone
1 xyz 1001 89654566
2 abc 1002 78893839

These kinds of data structures are tough to read via Array. As we need to loop through the entire data structure using loop variables to get the correct data. One variable will go along with the columns and another one through rows.

READ  QTP Recognize Weblist As Webedit Problem Solved

Dictionary Objects, on the other hand, solve these kinds of issues by using an Associative Array concept. It means each item in the rows has a unique key associated with it. Understanding this concept makes the job half done while studying Dictionary Object in UFT.

Both arrays and dictionaries are holders and can be perused sequentially (for example arrays can be listed by methods for a file and dictionaries by methods for a key). Yet, while arrays keep up the request among objects, dictionaries not.

What’s more, with dictionaries you have the likelihood to get to a particular element with a particular key in an easier to understand way (a memory aide one).Alternatively, we can use comments.

An Array holds an index value as numeric though, in dictionary object, record esteem can be a string. Thus while alluding any an incentive with key, a key can have any name related to esteem.

While both can be used in our scripts, Dictionary objects, in general, are used as global variables and arrays we use as local variables.

Array:
  1. An array is alike holder object which holds a fixed number of estimations of a solitary kind.
  2. We can have a dynamic array.
  3. Array does not have meany methods like Dictionary.
  4. Arrays can be dynamic in qtp/uft or some other programming ex: Redim to change the array estimate
  5. Before utilizing an array we should announce the extent of an array.
  6. If we need to expand the array measure in the content we need to utilize Redim articulation to build the array.
  7. QTP/UFT does not have any technique to discharge the memory on the off chance that any component isn’t required.
  8. We can have a multidimensional array to work with.
  9. Deletion from inside of an Array costs more as other elements need to move up. For this, we need to create one more array and transfer the remaining items. This back and forth operation come with a huge cost in terms of speed.
Dictionary:

Dictionary object has numerous builtin capacities which can include, expel, get to the components utilizing one of a kind key. This is another concept while studying Dictionary Object in UFT.

  1. QTP/UFT does not have any idea like dynamic Dictionary
  2. setting up the size of the dictionary in QTP/UFT is not required.
  3. Not required to utilize any announcement to include additional elements, just use include method.
  4. Dictionary supports many methods like add, delete, etc.
  5. It can expel components that are never again valuable.
  6. We do not have a multidimensional Dictionary to work with.
  7. The deletion process is super easy. The internal mechanism of the Dictionary automatically moves the elements up upon the deletion of an item.

What’s the final Call?

The dictionary object was utilized with the record-index(key) being a string. On account of the array, the list can be ONLY numeric. At that point, we have some undeniable favourable circumstances like referencing an incentive with an important key and so forth. While Array gives us the flexibility to use duplicate entry, the Dictionary object does not allow duplicate entry.

Dictionary Object is the more useful and versatile replacement of an Array.

References:

Share and Enjoy !

3 thoughts on “Learn Dictionary Object in UFT or QTP Quickly”

  1. Amazing post. Articles which have meaningful and insightful comments are more enjoyable, at least to me.
    It is interesting to read what other people thought and
    how it relates to their customers, as their perspective could possibly help you in the future.

    King regards,
    Thompson Valenzuela

Leave a Comment

Your email address will not be published. Required fields are marked *