Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

How to work with Dictionaries using scripting.dictionary object in QTP

Dictionary Object stores data key, item pairs. Each item is associated with a unique key.The key is used to retrieve an individual item and is usually a integer or a string, but can be anything except an array.

Creating a dictionary object:

We can create an instance of dictionary object as shown below:
Set odict = CreateObject("Scripting.Dictionary")
odict.add "country", "India"
odict.add "Capital", "Delhi"  

 
We have created a dictionary object odict, and added 2 key item collection in the dictionary object.
 

Below are the methods of dictionary object:


1.  Add Method - Adds a key and item pair to a Dictionary object.

odict.add "Currency", "Rupee" 


2.  Exists Method - Returns true if a specified key exists in the Dictionary object, false if it does not.

odict.exists(“country”) will return true  for dictionary defined in above examples.



3.  Items Method - Returns an array containing all the items in a Dictionary object.aite = odict.items(“country”)



4.  Keys Method - Returns an array containing all existing keys in a Dictionary object.aite = odict.keys(1)



5.  Remove Method - Removes a key, item pair from a Dictionary object.odict.remove("Currency")



6. RemoveAll Method - The RemoveAll method removes all key, item pairs from a Dictionary object.odict.removeAll()


Properties of Dictionary object

1. Count Property - Returns the number of items in a collection or Dictionary object.Intcount = odict.count


2. Item Property - Sets or returns an item for a specified key in a Dictionary object. For collections, returns an item based on the specified key.ItemVal = odict.Item("Capital")   - gets value of the keyodict.Item("Capital") = “MUMBAI” – sets value of  the key.



3. Key Property - Sets a key in a Dictionary object.odict.Key("Capital") = "City"odict.item(“City”) will now give value of item stored in key Capital before. 


Comparing Dictionaries Vs Arrays


Below is the comparison between array and dictionary objects
  •  Dictionary  has methods to add new items and check for existing items.
  •  On Deleting a particular item from a dictionary, all the subsequent items automatically shift up.
  • You use keys to identify dictionary items. Keys can be any data type while arrays have numeric values as key
  •  A dictionary can't be multidimensional while array can be.
  •  It is much easier to find an item value based on keys as compared to arrays.
  •  Keys in Dictionary can have unique values only
  •  Dictionary can be used as Global variable declaration. so that any test can access the values from it in the run time. Will discuss this in detail in a separate post.

All About Working with datatables and data Sheets in QTP: Methods and properties

Methods for Datatable object


Following are the methods and properties for interaction with data tables in QTP

1. Datatable.AddSheet  

Adds the specified sheet to the run-time Data Table
datatable.AddSheet(strSheetName)

2. DeleteSheet

Deletes the specified sheet from the run-time Data Table.
Datatable.DeleteSheet(strSheetName)

3. Export Method

Saves a copy of the run-time Data Table in the specified location.
datatable.Export(strExcelFile)

4.ExportSheet Method 

Exports a specified sheet of the run-time Data Table to the specified file.
DataTable.ExportSheet(FileName, DTSheet)

5.GetCurrentRow

Returns the current (active) row in the first sheet in the run-time Data Table (global sheet).
row = DataTable.GetCurrentRow

6. GetRowCount Method

Returns the total number of rows in the longest column in the first sheet in the run-time Data Table (global sheet).
DataTable.GetRowCount

7. GetSheet Method:

Returns the specified sheet from the run-time Data Table.
MyParam=DataTable.GetSheet ("MySheet").AddParameter("Rower", "9")

8. GetSheetCount

Returns the total number of sheets in the run-time Data Table.
intSheet = datatable.getSheetCount

9.  Import Method:

 Imports the specified Microsoft Excel file to the run-time Data Table.
DataTable.ImportSheet(FileName, SheetSource, SheetDest)

10. ImportSheetMethod:

Imports a sheet of a specified file to a specified sheet in the run-time Data Table.
DataTable.ImportSheet(FileName, SheetSource, SheetDest)

11.SetCurrentRow Method:

Sets the specified row as the current (active) row in the run-time Data Table.
DataTable.SetCurrentRow(RowNumber)
DataTable.GetSheet("MySheet").SetCurrentRow(2)

12. SetNextRow Method 

Sets the row after the current (active) row as the new current row in the run-time Data Table.
DataTable.SetNextRow
DataTable.GetSheet("MySheet").SetNextRow


13. GlobalSheet Property 

 Returns the first sheet in the run-time Data Table (global sheet).
 DataTable.GlobalSheet.AddParameter "Name", "Nitin"

14. LocalSheet Property

Returns the current (active) local sheet of the run-time Data Table.
DataTable.LocalSheet.AddParameter "Name", "Nitin"


Methods For DTSheet Object:


Below are the methods to work with the specified sheet in datatable.

1. AddParameter Method 

Adds the specified parameter (column) to the sheet in the run-time Data Table, sets the value of the first row to the specified value

DataTable.GetSheet("dtGlobalSheet").AddParameter "Name","Nitin"
DataTable.AddSheet("MySheet").AddParameter("Name", "Nitin")
paramname = DataTable.LocalSheet.AddParameter("Name", "Nitin").Name 

2. DeleteParameter Method

Deletes the specified parameter from the sheet in the run-time Data Table.
DataTable.GetSheet("MySheet").DeleteParameter("Name") 

3. GetCurrentRow Method

Returns the row number of the current (active) row in the run-time Data Table sheet. 
row = DataTable.GetSheet("MySheet").GetCurrentRow 

4. GetParameter Method 

Retrieves the specified parameter from the run-time Data Table sheet. 
DataTable.GetSheet("ActionA").GetParameter("Date").RawValue

GetParameter("ParamName").value retrieves or sets the value of the cell in the current (active) row of the parameter in the run-time Data Table. 
DataTable.GetSheet("Action1").GetParameter("Destination").Value="Pithoragarh"

DataTable.GetSheet("Action1").GetParameter("Destination").ValueByRow(7) - This gets value for parameter Destination in sheet Action 1 in row 7.

5. GetParameterCount Method 

Returns the total number of parameters (columns) in the run-time Data Table sheet.
paramcount = DataTable.GetSheet("Test").GetParameterCount

6. GetRowCount Method

 Returns the total number of rows in the longest column in the run-time Data Table sheet.
 rowcount = DataTable.GetSheet("Test").GetRowCount


Descriptive programming examples and comparison with Object Repository

Comparing Descriptive Programming with Object Repository


Suppose we record on Google page for testing and then click back for Search button, steps will be recorded in QTP as shown below. An object reposiory stores all the objects used in the recording. Issue with Object repository is that we need to associate object repository with test for execution and if the project size increase, it becomes difficult to maintain the repository.

Browser("Google").Page("Google").WebEdit("q").Set "TESTING"
Browser("Google").Page("Google").WebButton("btnG").Click
Browser("Google").Page("testing - Google Search").Link("Search").Click

Suppose we do not want to use objects from object repository, we can use descriptive programming to describe the objects.


Descriptive programming can be implemented in following two ways:

1. Using Static description:

Suppose we have to define description for 
Browser("Google").Page("Google").WebEdit("q").Set "TESTING"

We need to do the following:


1. Use object spy to identify the object in the page, mostly for html pages, html id is unique for the object, so will use html Id as property for the object.




Browser("title:=Google").Page("title:=Google").WebEdit("html id:=gbqfq").Set "TESTING":

Also in case of multiple object with similar properties, for example, multiple instances of browser, we can use ordinal identifier to identify the object

the above code using ordinal identifier and using regular expressions can be written as:

Browser("title:=.*","index:=0").Page("title:=Goog.*").WebEdit("html id:=gbqfq","index:=0").Set "TESTING":


2. Using Description Object


In descriptive programming, we can create description object and define the description of the object. For above code, we can create description object for each of the following:

Set objdescBR = description.Create
objdescBR("title").value ="Google"
Set objdescPg = description.Create
objdescPg("title").value ="Google"
Set objdescEdit = description.Create
objdescEdit("html id").value ="gbqfq"
objdescEdit("index").value =0

Browser(objdescBR).Page(objdescPg).WebEdit(objdescEdit).Set "TESTING1"

Description object can be very useful if we want to perform similar actions of multiples, for e.g checking multiple checkbox, providing same text in multiple edit box and so on. 

We will describe some examples on Descriptive Programming


1. Getting total number of links in the page:


Set objdescBR = description.Create
objdescBR("title").value ="Google"
Set objdescPg = description.Create
objdescPg("title").value ="Google"
Set objdesc = description.Create
objdesc("micclass").value ="Link"
Set objLinks = Browser(objdescBR).Page(objdescPg).childobjects(objDesc)
Msgbox objLinks.count

Similar to this example, we can fine multiple objects type in the page based on class of the object, Please give it a try for Images, and checkbox in the page


2. We can also like clicking on a link with a particular name or entering value in a particular edit box with code similar to as  below:


Call clickLink("Orkut")

Public Function clickLink(LinkName)
Set objdescBR = description.Create
objdescBR("title").value ="Google"
Set objdescPg = description.Create
objdescPg("title").value ="Google"
Set objdesc = description.Create
objdesc("micclass").value ="Link"
Set objLinks = Browser(objdescBR).Page(objdescPg).childobjects(objDesc)
For i = 0 to objLinks.count - 1
If (objLinks(i).getroproperty("name") = LinkName) then
objLinks(i).click
End If
Next
End Function