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

Showing posts with label Data Tables. Show all posts
Showing posts with label Data Tables. Show all posts

Tips for Managing test data in QTP test scripts

Test Data management is an important factor for a robust automation framework. Test Data should be easily used by test scripts and should cover both positive and negative test scenarios.One of the key for successful automation is to avoid hard coded values and data in the test. To avoid hard coding test data in the script and running the same test script with multiple test data, we should take care of points explained in this article for using test data in the automation framework:


  • Test data should be maintained in external data source which can be an excel, csv or database.
  • Test data should be flexible. Suppose we need additional column to be used by test scripts.Test data should be used by test script in such a manner that changing the columns order or adding new column does not make any change in the test script.
  • Let me explain the above point with an example. Suppose initial excel file from which test data was read has three columns of data, Password, UserName and LoginResult. Suppose if a new column has been added for dateofBirth.  Adding this new column before Loginresult should not impact the script execution.

Password
UserName
DateofBirth
LoginResult
TEST
User1
22-06-1984
FAIL
TEST
User2
13-05-1998
FAIL
T20
T200
12-09-1908
PASS
ODI
T500
12-01-2013
FAIL


  • For this, we should have the column name in the first row as header and should use the test data from next row onward in the test script.The header fields name should be short but self explanatory. In the above example, Password, UserName, DateofBirth and LoginResult are header.
  • The test script should provide enough flexibility to execute a given row of data or not. To provide this we should use a column for Execution Flag which will indicate whether to execute the test with the test data in the row or skip to next row. Test script should be created in manner that they can be executed and iterated with multiple rows of data.
  •  Change in test data from one source to another, i.e. database to excel should have minimum impact on the test script execution.
  • We can consider using test generator tools which can generate random data for testing an application.
  • While preparing test data, we should dry run the test script with both positive and negative data to validate script behavior on both positive and negative conditions.

How to use Test data from external sources:


  • Test data can be imported from external source using import method or using excel object.
  • In case of using import methods, we will import a sheet (using import or importsheet method) or excel file into QTP data table and extract the data and use in test using get methods.
  •   In case of using Excel application, we can import the data in a two dimension array and then use data from the array or using scripting.dictionary to use data from the array.
  • Below is the algorithm of using scripting.dictionary to read the data and use in test scripts.
    • Create an array (two dimensional) for the data in the excel sheet.
    • From the array, read the top row data as header information and loop through rest of the column as data.Store the value as key-value pair e.g : Key Password will have value as ODI for the last row of the iteration. 
Below article shows how to use dictionary object to data drive a test in QTP:




How to share values between various actions in a test in QTP


Below are the various ways by which values can be transferred between various actions in a test.

Sharing Values via the Global Data Table

Value generated in one action can be shared  with other actions in your test by storing the value in the global Data Table. Each Action in the test can access the global data table and hence can access data from the global data table. For details on how to access various in a data table, see Working With data Tables in QTP


Sharing Values Using Environment Variables

Environment variables are also shared between actions and can be used to share information between various action in the test. Also values can be parameterized using environment variable. To create an environment variable, loading environment variable, and details on environment variable, Please see post on Environment Variables in QTP

Sharing Values Using the Dictionary Object

Values between various actions in a test can be done creating a global dictionary object, and using properties of dictionary object.Below are the steps to share values between actions using globaldictionary object:

To use the Dictionary object, you must first add a reserved object to the registry (in HKEY_CURRENT_USER\Software\Mercury Interactive\QuickTest Professional\MicTest\ReservedObjects\) with ProgID = "Scripting.Dictionary". For example: HKEY_CURRENT_USER\Software\Mercury Interactive\QuickTest Professional\MicTest\ReservedObjects\GlobalDictionary


Once object has been added, we can add and remove values to the Dictionary in one action and retrieve the values in another action from the same test using scripting dictionary methods.For details on Scripting Dictionary, Please see Working with dictionary Object


 

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