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

Difference between Actions and Functions in QTP/UFT 11.5


Comparison between Action and functions in QTP/ UFT 11.5 is explained in this article. We discuss on how action and functions are similar and difference between two in UFT 11.5/ QTP



Parameter
Action
Functions
Feature Type
Action is a feature provided by QTP/UFT
Function is a feature of VBScript
Return Value
We can pass as well as return multiple values using input and output parameters
A function returns a single value although values of multiple variable can be passed using public variable with global scope
Assets
An Action has local repositories, datatable, Active screen etc  associated with it
Function cannot have local repositories, datatable  and other QTP features associated with it
Multiple function can be created in an action
Multiple Actions cannot be created in a function.
Size and Performance
Size of the test increases with increase in number of actions as there is data table, OR associated with each of the test.
Performance of using function is better compared to action and consumes less size.
Actions
An Action is associated with a single test
Function library can be associated with multiple tests and can be loaded at runtime.
Maximum Allowed
There can be maximum 120 actions in a test and If any test has 120 actions in it, it will definitely not a good test script
There is no limit of number of function used in a test.
Maintenance Cost
Maintenance cost is higher in case of Action, as we need to open each test to make change at action. While multiple function library being vbs files can be updated easily.
Similarity between the Two
The purpose of using Actions or function is to provide modularity to the test scripts and should be used to create structured tests.
Extension
Extension of action script is .mts while function library can have extension as .vbs, .qfl.



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 use Dictionary object in QTP for creating data driven tests

This below code explains how to use scripting.dictionary to read data from excel object and create a dictionary from the excel. Using dictionary object, we can data drive a test and easily use the test data in test script based on dictionary Key-value pair. 


For details on scripting.dictionary object, see Dictionary Object in QTP

Below code explains how to use dictionary object for creating data driven tests using Dictionary object:

 call func_getDictionaryData("c:/test.xlsX", "Sheet1", 5)  
 Function func_getDictionaryData(strExcelFile,strsheetName, iRow)  
 On error resume next  
 'Create an instance of Excel object   
  Set objExcel = Createobject("Excel.Application")  
  objExcel.visible = false  
  ''open the workbook and the specified worksheet, These two are required as argument to the function  
  Set objExcelbook = objExcel.Workbooks.Open(strExcelFile)  
  Set objExcelsheet = objExcelbook.Worksheets(strsheetName)  
  ''Get the rows and column count of the excelsheet  
 intColCnt = objExcelsheet.UsedRange.Columns.Count  
 intRowCnt = objExcelsheet.UsedRange.Rows.Count  
 If (iRow>intRowCnt) Then  
   msgbox "Row number provided in function is greater than the rows in the sheet"  
 else  
  ''Create a dictionary object  
  Set objDictdta = Createobject("Scripting.dictionary")  
  For i=1 to intColCnt  
     dictKey = Trim(objExcelsheet.Cells(1,i))  
     dictVal = Trim(objExcelsheet.Cells(iRow,i))  
     objDictdta.Add dictKey,dictVal  
  Next  
 End If  
 objExcel.close  
 Set objExcel = nothing  
 objDictdta.close  
 Set objDictdta = nothing  
 If (err.number>0) then  
    msgbox "error in the file: "+ err.description  
 End If  
 End Function