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

Showing posts with label Advanced QTP. Show all posts
Showing posts with label Advanced QTP. Show all posts

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  

How to Export/Import tests in QTP

Test in QTP can be exported to a zipped file with all the objects. In a similar manner, test from previously exported file can be imported to a QTP test.


Export Test – A test can be exported to a zipped file following below steps

export test in QTP

  •  In QTP/UFT, Navigate to File>Export Test
  • Provide the path of the location where exported zip file will be saved.
  • The test is exported to a zip file saved at the location provided.

 

Import Test – Similarly we can import the test in zip file to a QTP test or solution following below steps

import test in QTP

  • In QTP/UFT, Navigate to File>Import Test.
  • Provide the path of the location from where zip file will be imported.
  •  Provide the path of qtp test where the zip file will be converted to a test.

How to load multiple function libraries at runtime from Quality Center in QTP

This post explains how to load multiple libraries at runtime from Quality Center in a QTP test. As the test size grows, it is better to associate or load function library at runtime with all the tests. 

Note we should associate an initialization library with each of the test and then perform following work in the initialization library. 


  • Load all the function library with the test
  • Load the shared object Repository with the test.
  • Remove the local object repository with the test to avoid object conflict
  • Load the environment variables to be used across test
  • Close all instances of application.
  • Providing the reporter configuration for the test. e.g: allowing reporting in QTP Reporter on scenario of error only


In the current post, we will focus only on how to load multiple libraries at runtime from Quality Center or local folder.


In the case of using local folder structure , the path [QualityCenter] Subject\Project_Name \......   changes to c:\QTP_Automation/......

 Const strLibraryNames = "[QualityCenter] Subject\Project_Name\QTP\Libraries\Library1.vbs>[QualityCenter] Subject\Project_Name\QTP\Libraries\Library2.vbs>[QualityCenter] Subject\Project_Name\QTP\Libraries\Library3.vbs>[QualityCenter] Subject\Project_Name\QTP\Libraries\Library4.vbs"  
 callfunc_LoadFunctionLibrary(strLibraryNames)  
 Function func_LoadFunctionLibrary(strLibraryName)  
  ''Load multiple Library Files seperated by '>'  
  collLibrary = split(strLibraryName, ">")  
    ''Now collLibrary is an array with each element in array storing the path of library  
  For i = 0 to ubound(collLibrary)  
  LoadFunctionLibrary collLibrary(i)  
  Next   
 End Function  

Manipulating and working with Word document using vbscript in QTP

In this article, we will discuss how to manipulate microsoft word document using QTP and VBScript. Learning working with microsoft word can help the users to generate reports of test results in word document . We will discuss on code for following problems in this article 

a. Creating a new Word document using vbscript in QTP
b. Opening an existing word document
c. Appending at the beginning and end of a word document. 
d. Reading content of a word document.
e. Formatting text of a word document.

We will discuss on capturing images and working with table in MS word using vbscript in some another article: 


Example 1 - Creating a new word document using vbscript. Writing some text in the document and closing the same


' Instantiate an object of word application 
Set objWord = CreateObject("Word.Application")
' Add a new item of word application object
objWord.Documents.Add
' Adding some text in the word document
objWord.Selection.TypeText "testing time"
' Save the document as provided
objWord.ActiveDocument.SaveAs "C:\qtptesting.docx"
' Close instance of word application
objWord.Quit
' Release the object for word document.
Set objWord=Nothing

Example 2 - The above was example of adding a new document in case we wish to update an existing document, we can use the below code:


' Instantiate an object of word application 
Set objWord = CreateObject("Word.Application")
' Open an existing word document
objWord.Documents.open "C:\qtptesting.docx"
' Adding some text in the word document
objWord.Selection.TypeText "testing time 2"
' Save the document as provided
objWord.ActiveDocument.Save
' Close instance of word application
objWord.Quit
'Release the object for word document.
Set objWord=Nothing


Example 3 : Now we have seen the text in above example 2 writes "testing time 2" just before testingtime. Suppose we want to write the text testing 2 in a new line after text testing, we can write the code as



Set objWord = CreateObject("Word.Application")
objWord.Documents.open "C:\qtptesting.docx"

' Adding some text in the word document at the end of document:
objWord.Selection.EndKey 6,0
objWord.Selection.TypeText vbCrLf & "testing time 3"

objWord.ActiveDocument.Save
' Close instance of word application
objWord.Quit
'Release the object for word document.
Set objWord=Nothing

Example 4 : Next we want to format the text in the word document.


Set objWord = CreateObject("Word.Application")
objWord.Documents.open "C:\qtptesting.docx"
' Adding formatting to the object, there will be lot of method to format , can refer to reference vbscript in QTP or word.
objWord.Selection.Font.Size ="18"
objWord.Selection.Font.Name ="18"
objWord.Selection.Font.bold = true
objWord.Selection.EndKey 6,0
' formatting changes in the code completes
objWord.Selection.TypeText vbCrLf & "testing time 3"
objWord.ActiveDocument.Save
' Close instance of word application
objWord.Quit
' Release the object for word document.
Set objWord=Nothing

Example 5: Reading content of a word document and storing in a string


Set objWord = CreateObject("Word.Application")
objWord.Documents.open "C:\qtptesting.docx"
set objdoc = objWord.Activedocument
strWordtext = ""

' read the number of words in the document and loop through the number of characters
iWordCnt= objdoc.Words.count
For i = 1 to iWordCnt
strWordtext = strWordtext + objdoc.Words(i)
Next
msgbox strWordtext
' specific changes in the code completes

objWord.Quit
'Release the object for word document.
Set objWord=Nothing

Working with add-ins in UFT/ QTP

QTP add-ins helps to identify objects in a variety of development environments. For e.g. if we require to create tests for an application in Java environment, we need to be install add-in for Java to identify objects in Java application. To use an add-in, we need to install the add-in in UFT.


How is add-ins installed and used in QTP/UFT?


Add-ins can be installed together with UFT installation or modify the existing UFT installation. Using Add-in for a particular requires the add-in to be installed. When UFT opens, a dialog box opens which asks which add-ins is to be loaded as shown below. For better performance only the add-ins which is used in the test session should be loaded when QTP/UFT is launched.

Once an add-in is added from UFT Add-in Manager, we can define the run and record setting for the add-in. below image shows the record and run setting for Java add-in. In case we want to do testing on any open application, and not to open a fixed pre-defined application, use radio button for not recording and run on any open java application.



UFT includes built-in support for testing standard Windows application. In case the required add-in not loaded, QTP tries to identify the object as standard window object. With UFT 11.5, No separate license is required for the add-ins and can use any of the add-ins to work with.

Understanding different type of add-ins - The add-ins are broadly classified as :


Web-based Add-ins – 

  • Add-ins designed to support special objects that are generally available in Web applications.
  • Most of the capabilities of objects in this category are identical or similar.

 Web-based add-in used in UFT 11.5 are:

  •  .Net Web Forms Add-in  The .NET Add-in functions like a Web-based add-in when testing .NET Web Forms controls.
  • PeopleSoft Add-in    Identify and test PeopleSoft user-interface object
  • Siebel Add-in   Identifies Siebel objects in the application. Using Siebel Add-in provides the user to use Siebel Test Express to automatically generate a new shared object repository, or to update an existing object repository.
  • .NET Silverlight Add-in   Identifies and test silverlight application.
  • Web 2.0 Toolkit Support
  • Web Add-in
  • Web based SAP Support
  •  Oracle Add-in

 Windows based Add-ins –

  • Add-ins designed to support special objects that are generally available in Window applications.
  • Most of the capabilities of objects in this category are identical or similar.

 Window based Add-ins in UFT 11.5 are:

  • ActiveX Add-in
  • Delphi Add-in
  • .Net Windows form Add-in
  • Power builder Add-in
  • Qt Add-in
  • Windows based SAP Support
  • Stringray Add-in
  • Terminal Emulator Add-in
  • Visual Age Smalltalk Add-in
  • Visual basic Add-in. 

Java Add-in   provides Java test objects, methods, and properties that can be used when testing objects in Java applications.

Flex Add-in – Using Flex Add-in provides Flex test objects, methods, and properties, Flex objects in Flex applications are identified and automated.

In case the objects in the environment are not identified properly using the add-ins, we can use extensibility to identify the objects.


How to create a Hybrid Framework in QTP

A hybrid Framework is a framework that uses features of multiple frameworks and is more robust. For e.g, we can create data in external files as in data-driven approach, or creation of keywords as in keyword driven framework. 


Before understanding what needs to be a part of hybrid framework, it is necessary to know what are essential requirements of a framework, Below are the points that needs to be considered before creating a framework.

1. Identify where Test Artifacts needs to be maintained 


Test Artifacts can be maintained in test management tools like Quality Center or can be stored in a shared repository. Using a test management tool, For e.g HP ALM provides following features 

  •  Version controlling of test scripts, function libraries, Object repository, recovery scenario and test data
  •  Multiple Users can at the same time can access the resources in tool.
  • Option to run  set of tests and defining the execution flow can be done using test management tool.


In case of test management tool not available, Test Artifacts should be maintained in a shared folder, so that multiple user can have access to the  resources. In this case, we will require a driver script that will execute multiple test script in a batch and also can be scheduled.

2. Object Identification


Objects Identification is very important and can be identified in number of ways. Some of them are as follows

  • Using Object Repository
  • Using Descriptive Programming
  • Using HTML dom.
  • Using XPath/CSS

A hybrid approach can use a mix of various object identification techniques and can maintain object of different types in different vbs files which can be executed during test initialization.

3. Error Handling and Recovery Scenario


For expected errors, error handling should be implemented in the code and recovery scenario can be used to avoid interruption in test script execution.Code should have enough error handling defined.

4. Reporting


For each failed/passed step in test script execution, reporting in QTP results or user defined html results should be done. Reporting should not be direct using reporter.reportevent but should be wrapped in a function with arguments defined for taking screenshot or exiting the test based on flag status of the argument.

5. Test Data Management


Test data is an important factor in hybrid framework design. Preparation of test data should take into considerations factors like:

  • What are the validations required for test data preparation in fields
  • Can any random data be entered in the fields.
  • How much data is required. e.g bulk data required to execute scripts for multiple iterations.
  • What will be the source of data. Data can be stored on an excel file or can be imported from database at run time if specific existing data is required.
  • In case of large data requirement, test data generation tools like GenerateData can be used.

6. Libraries Management


For each test script execution, An initialization library should be called at the start of script execution which should load the required environment variables, object repositories and other function libraries with application specific code.
Similarly before exit from test script execution, function to close all process that were called during script execution should be executed. Also Code should have enough error handling and reporting, minimum hard code data, and should follow standing coding practices

How to capture images and create movie to be saved with QTP

QTP provides options to captures images and record movies of test script execution. User can define the setting for image Capture and screen recorder before test script execution. 

To define settings for images capture and screen recorder, Navigate to Tools>Options>Run>Screen Capture.

Click on Checkbox to save still image captures to results or click on checkbox for save movies to results.


Select whether to save images/capture movies always or only for error as shown below.



Settings for Screen capture in QTP
Settings for Screen capture in QTP

Once test is executed, recorded movie can be viewed in results as shown below



Viewing recorded movie in results
Viewing recorded movie in results

How to integrate QTP 11 with Quality Center/ALM

HP ALM 11 or Quality Center is used as a test management tool in which we can store tests, function Libraries, test data, object repository, recovery scenario and much more. Below are some of the features in QTP required to connect to Quality Center and various options in QTP regarding QTP-QC Integration

Integration between QC and QTP

QTP can connect to a project stored in ALM through VBScript Code or through QTP IDE as explained below:

Through Code

Below code creates a connection to QC. This will validate if a connection to QC already exists and in case not connected than connects to the required project in QC

Public Functionfunc_ConnectQCQTP(QCurl,Domain,ProjectName,UserName,Password)

Set qtApp = CreateObject("QuickTest.Application") 

If  qtApp.launched <> True then 

    qtApp.Launch 

End If

qtApp.Visible = "true"

If Not qtApp.TDConnection.IsConnected Then

   qtApp.TDConnection.Connect QCurl,Domain,ProjectName,UserName,Password,False

End If

Set qtApp = nothing

End Function


Through QTP Interface


Go to File>ALM/QC Connection as shown below and create the connection.

Connecting to QC from QTP



Connecting to QC from QTP

User is allowed options to reconnect to the QC Server, and authenticate on start up and login to project automatically once QTP is launched.

Version Control in QC


In case of test stored in QC,version controlling the test script is very useful,so that test can be open by one user at a time and previous versions of test are available and can revert to previous version in case of issues. In case of version control, a test will open in read only mode and changes in script can be done only post checking out the script, and once changes are incorporated, changes should be checked in.



Examples of Working with WebTable in QTP

HTML normally has many webtable and we can extract a lot of information using webtable methods in QTP. In this post we will discuss various methods by which we can used for webtable object.

Example 1: Validate if a webtable exist

boolExist = Browser(…).page(…).webtable(…).exist

Example 2 : To Find number of rows and columns in a web table

intRowCnt=Browser("Google").Page("title:=.*").WebTable(“name:= TTable").RowCount
For r=1 to intRowCnt
‘’ This will loop through each row and tell count of column in each row.
intColCnt=Browser("Google").Page("title:=.*").WebTable(“name:=TTable").ColumnCount(r) 
MsgBox intColCnt
Next

Example 3: How to get data in a particular cell in the data table

strData= Browser("Google").Page("title:=.*").WebTable(“name:=TTable").GetCellData(r,c)
Where r = row number and c = column number

Example 4: Using childObject method to find objects of a particular type within a webtable

Public Function func_findObjectinaWebTable(strObjType)
Set objDesc=Description.Create
objDesc("micclass").value=strobjType
set objChild=Browser("Google").Page("title:=.*").WebTable(“name:=TTa").ChildObjects(objDesc)
func_findObjectinaWebTable = objChild.count
End Function

Example 5: Using HTML DOM to get count of objects in a webtable

Public Function func_findObjectinaWebTable(strTagName)
set objChild= Browser("Google").Page("title:=.*").WebTable(“name:=TestTable").object.getElementsbyTagName(strTagName)
func_findObjectinaWebTable = objChild.length
End Function

Example 6: Using ChildItemCount and childItem in webtable to extract information

Public Function func_findObjectinaWebTable(strObjType,introw,intCol)
ob0jChildCnt=Browser("Google").Page("title:=.*").WebTable(“name:=TestTable,"index:=0").ChildItemCount(introw,intcol,strobjType)
If objChildCnt >0
Childitem will return object oftype defined in arguments for childitem
Set ChldItm = Browser("Google").Page("title:=.*").WebTable(“name:=TestTable,"index:=0").ChildItem(introw,intcol,strobjType,0)
If (strobjType = "Link" or strobjType ="WebEdit") Then
ChldItm.Click
ElseIf(stobjType = "WebCheckBox") Then
ChldItm.Set "ON"
End If
End Function