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

Showing posts with label QTP Basics Tutorials. Show all posts
Showing posts with label QTP Basics Tutorials. Show all posts

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


How to use and load Environment variables in QTP

Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer. Environment variables in qtp are very useful to define the environment condition in which QTP is working.

Types of Environment variables

There are two types of Environment variables in QTP

Built-in variable

Built-in variables are QTP defined variables that contains information of Test Properties, computer’s information, Information related to operating system and Action’s property.

User defined variable

Other than environment variable defined in-built in QTP, We can define user defined environment variables. The main purpose of defining user defined variables is to define global variables that can be used throughout test execution irrespective of working with different Actions. So scope of user defined variable is throughout the test execution. This can be of type internal or external if loaded from a file.

Loading of User defined Environment variable


Environment variable can be loaded in following ways:


1.  Through QTP UI

 User defined Environment variables can be created through UI as shown below

Adding new environment Parameter

 Option to export user-defined environment variables and loading of environment variables from external file is also available.


2.  Using ExternalFileName Property and LoadfromFile Method

 Loading of variables from external file as shown above can be done through code:

'Check if an External Environment file is loaded and if not, load it.
fileName = Environment.ExternalFileName
if (fileName = "") Then
    Environment.LoadFromFile("C:\Env.xml")
End If

3.  Using Environment.Value

 Value of environment variable can be extracted at runtime as 
StrEnv=environment.value(envName)

Similarly value of environment variable can be set at runtime as
Environment.value(“envname”)=”some value”

Where to Access Environment Variable 


To access the environment variable, Go to File>Settings>Environment and select built-in or user defined from the drop down.

Accessing environment variables

Use of Environment variable 


  • We can define parameters in QTP using environment variables.
  • Environment variables can act as global variables which can be used across different  tests/actions.
  • We can dynamically load environment variables during start up of QTP execution and can store url, environment, login details in a xml file,which will be loaded each time test is executed.
  • Built- in Environment variables gives us useful information about the test environment and extracting the information can be useful.



How to use Regular Expressions in QTP


Regular expressions are used to identify objects and text strings with varying values. Regular Expression are strings that defines search phrase based on special character provided in the expression.This is useful when expected value of any object property is regularly changing but in a fix pattern.

Some useful points to remember about Regular Expressions are:


  • Regular Expression is useful for following scenarios in QTP:
    • Defining the property values of an object in dialog boxes or in programmatic descriptions
    • Defining expected values for checkpoints
    • Defining pop-up window conditions in a recovery scenario.
  • Can create regular expression for strings only.
  • Period (.), hyphen (-), asterisk (*), caret (^), brackets ([ ]),parentheses (()), dollar sign ($), vertical line (|), plus sign (+), question mark (?), and backslash (\) are special characters used to create regular expression.
  • When one of above mentioned special characters is preceded by a backslash (\), QTP treats it as a literal character.
  • By default, the value of all Property objects added to a Properties collection are treated as regular expressions.

Below are the various regular expressions used in QTP.

  • Matching Any Single Character (.) eg abc. Will match abc followed by any character.

  • Matching Any Single Character in a List ( [xy] ) e.g [ab] will match either a or b

  • Matching Any Single Character Not in a List ( [^xy] ) e.g 1[^23] will match all values between 11 to 19 except 12 and 13.

  • Matching Any Single Character within a Range ( [x-y] ) e.g : 1[1-3] will match 11,12, and 13.

  • Matching Any AlphaNumeric Character Including the Underscore ( \w )

  • Matching Any Non-AlphaNumeric Character (\W) will match any special character other than underscore. Please note case of W in this case.

  • Matching Zero or More Specific Characters ( * ) This matches zero or more occurrences of the preceding character. e.g ca* will match caa,caaaa,c and so on. Similarly c.* will match c, cs,caaa, and so on, since preceding character here is “.”.

  • Matching One or More Specific Characters ( + ) Only difference from * is it will match for minimum one character. e.g ta+r will match taar,tar but not tr as in above case.

  • Matching Zero or One Specific Character ( ? ) A question mark (?) instructs QTP to match zero or one occurrences of the preceding character. For example: te?r matches ter and tr, but nothing else

  • Matching One of Several Regular Expressions ( | )  e.g new|day will match either of new or day. If we write ne(w|d)ay, it will match neway or neday.

  • Matching the Beginning of a Line ( ^ ) This will match only if match is found at beginning of line.

  • Matching the End of a Line ( $ )  This will match only if match is found at end of line.

  • Matching a word at boundary(\b) e.g new\b will match testnew but not in knewit.

  • Matches a digit character(\d)  Matches a digit value.

  • Matching a non-digit character(\D) Matches a non digit value

For understanding of regexp object for regular expression, Click here


Different Ways to do recording in QTP


Recording in QTP can be done in following modes:


Normal Recording


This is the default mode of recording in QTP. It records the objects in your application and the operations performed on them.During normal recording objects are identified as standard objects are saved in repository.

To Access Normal Recording, Click the Record button or select Automation > Record or press F3 Key.


Analog Recording Mode


This mode records the mouse movement and keyboard operation in the application.This mode is useful in case object is not recognized as standard object.

To Access Analog recording, start a recording session as we do in normal recording. Click the Analog Recording button or select Automation > Analog Recording to start analog recording.Once recording is complete, click the analog recording button to close analog recording.
The mouse and keyboard operation are saved in a track file which can be reused.The track file called by the RunAnalog method contains all your analog data and is stored with the current action.

Window("Microsoft Internet Explorer").RunAnalog "Track1"

Some useful points to remember about Analog recording are as follows:


1. Shortcut Key for Analog Recording is SHIFT+ALT+F3.

2. Useful for applications in which the actual movement of the mouse needs to be recorded.

3. Analog Recording can record relative to the screen or relative to a specific window. The settings are defined in Analog Recording Settings.

4. The steps are recorded in a separate file saved within the action.

5. Steps once recorded cannot be edited in Analog recording.

6. Test may fail in case screen resolution changes.


Low Level Recording


This enables user to record the exact coordinates of any object, whether or not QuickTest recognizes the specific object or the specific operation.

To Access Low Level recording, start a recording session as we do in normal recording. Click the Low level Recording button or select Automation > Low Level Recording to start analog recording.

Some useful points to remember about Analog recording are as follows:


1. Shortcut key for Low Level recording is CTRL+SHIFT+F3

2. Useful when object is not identified by QTP in normal recording mode.

3. During Low Level recording, objects and parent objects are added as window object.


Window("…").WinObject("…").Click 12,32


Object Identification in QTP - Learning Objects


During run time QTP matches the object properties it store during test preparation and perform the required action on the object identified.In this article, we will discuss how objects are identified in QTP.

Learning an Object


1. When we work on an object, QTP stores it as a test object and assign class to the object to which it belongs, For e.g: webEdit, WebElement.

2. QTP considers the mandatory identification property for the object based on the class to which the object belongs.

3. If it is not able to identify unique object in the page using mandatory properties, it adds assistive properties one by one until a unique identification is created.

4. It then adds ordinal identifier if identification properties (defined in 2 and 3) are not unique to identify object. Ordinal Identifier indexes the object based on Location, Creation time or index of the object in the page.

5. We can also define Visual relation identifier. Visual relation identifier defines an object based on relative position of the object relative to other objects in the page. For details on this, please refer QTP user guide.

6. We can also enable Smart Identification for the object in the object repository.


As explained in step 1-6 above, identification for object are created and saved in the object Repository.

An object can be learned in object Repository in following ways:

1. During recording session.

2. Using Object spy.

3. Learning from object repository Tool.


All the child objects can be learned based on the parent object using Object Spy.

Identification of object during run session:


Once we have learned the entire objects, an object repository containing all required objects is created.
During Run-session, QTP looks to identify unique object matching the object definition from object repository. Once it finds the required object, it performs action on particular object.

The object is identified during run session in following way:


1. It looks for identification properties (mandatory and assistive properties) in the run object.

2. In case more than one object matching the identification properties is found and visual relation identifier is defined, it search to identify the object using virtual relation identifier.

3. In case unique object is not found in step 1 and 2, QTP uses smart identification to identify the object.

4. In case using above steps, if object is not identified and virtual relation identifier is not defined, it looks for ordinal identifier properties.


Using above steps, QTP determines if the object is identified in QTP

Brief about Terms used:


Visual relation identifier

Visual relation identifier defines an object based on relative position of the object relative to other objects in the page.

Smart Identification

In case, QTP is not able to identify unique object based on identification properties and smart Identification checkbox is selected, it forgets learned properties and tries to identify the unique object based on Base Filter Properties and optional filter properties defined in smart identification. It searches for all objects matching the base filter properties and filter out objects based on optional filter property one by one, until a unique object is found.

Ordinal Identifier

The ordinal identifier assigns the object a numerical value that indicates its order relative to other objects with an otherwise identical description. Index, Location, and Creation time are different types of ordinal Identifier. Creation Time is browser specific only.


Actions in QTP - Types of Action and Calling Actions

Actions help divide your test into logical units, A test comprises calls to actions.

Once a test is created, an action is created for the test. The Action is reusable by default. A reusable action can be called from various actions within the test or from external tests.
Creating multiple actions in test help to enhance reusability and modularize the tests. 

Types of Action:

Following are the types of Actions in QTP:

1. Reusable Actions

Can be called from multiple actions within or outside the test. An Action is reusable by default.

2. Non-reusable Actions

Cannot be called from other actions. Uncheck reusable checkbox while creating an action.

3. External Actions

This is a reusable action stored in another test.

4. Nested Action

An action called from other actions is nested action for the action from which it is called.

Calling An Action:

Action can be called in following ways: 

1. Call to Existing Action

We can view the steps of the action in the action view, but cannot modify them. The called action’s local object repository is also read-only

2. Call to Copy of Action

We can view the steps of the action in the action view and also modify the associated resources with the action. This action is non-reusable in the test.

3. Call to External Action

External actions can be called from the action by call to external action.
External Action can be called in expert view using LoadandRunAction as shown below:

LoadAndRunAction "Complete path of the test", "Name of Action"


Points to Remember about Actions

Some Important points to remember for Actions are as follows:

1. There can be maximum of 120 actions in a test.

2. For each action in a test, a new sheet in data table exists.

3. Data defined in global datasheet can be used by all the actions in the test.

4.Data defined in per action datasheet can be used by specified function only.

5. If we want to edit resources associated with a called action, use call to copy of action.

6. We can use input and output Parameter for an action, to get parameter value from an action.

7. Extension for actions is .mts

8. We can have multiple outputs from an action.

How and What to parameterized using Parameterization in QTP

Test can be enhanced by parameterizing the values that it uses. A parameter is a variable that is assigned a value from an external data source or generator.Following values can be parameterized:


  •        Checkpoints
  •        Object properties for a selected step
  •        Operation arguments defined for a selected step.
  •        One or more properties of an object stored in the local object repository. 

Values can be parameterized using:

  •       Test/action parameters
  •       DataTable parameters
  •       Environment Variable parameters
  •       Using Random number parameters

Parameter Types: 

Following are the parameter types used for parameterization


·        Test/action parameters:

 Test parameters enable to use values passed from the test. Action parameters enable to pass values from other actions in the test. 

·         Data Table parameters: 

Enables to create a data-driven test (or action) that runs several times using the data supplied. In each repetition, or iteration, QTP uses a different value from the data table

·        Environment variable parameters:


 Enables to use variable values from other sources during the run session. These may be values supplied, or values that QTP generates based on conditions and options

·        Random number parameters. 

Enables to insert random numbers as values in the test


Synchronization in QTP


      A synchronization point instructs QTP to wait until certain response from the application during playback.Synchronization point is required due to reason that when a test is run, the application may not always respond with the same speed. For example, it might take some extra time for below activities


  • for a progress bar to reach 100%
  • for a status message to appear
  • for a button to become enabled
  • for a window or pop-up message to open

      Synchronization can be used to ensure that QTP waits until the application is ready  before performing a certain step in following ways:


  • Timeout Value Modification
  • Using WaitProperty
  • Using Wait  Statement
  • Using Exist Statement

Timeout Value modification


Object Synchronization timeout  Sets the maximum time (in seconds) that QTP waits for an  
object to load before running a step in the test.

To define the Object  Synchronization timeout, Access  File > Settings > Run.



Using WaitProperty


Adding a synchronization point instructs QTP to pause the test until an object property  achieves the value specified.

The below dialog box enables to insert a WaitProperty statement to synchronize test.


How to Access:


  • Start a recording session.
  • Display the screen or page in application that contains the object for which a synchronization point needs to be inserted.
  • In the QTP window, select Insert >Synchronization Point.

WaitProperty is displayed in Expert View as shown below
Browser(".").Page("…").WebElement("…").WaitProperty "visible", true, 10000


Using Wait and Exist Statement


Exist and/or Wait statements can be used to instruct QTP to wait for a window to open or an object to appear

Exist Statement:


Exist statements return a Boolean value indicating whether or not an object currently exists

Eg:  blnExist = Browser(“…”).Page(“…”).Object.exist

Wait Statement:


Wait statements instruct Quick Test to wait a specified amount of time before proceeding to the next step.

Eg:  Wait(30)

Below code shows an example of Using Wait and Exist Statement for synchronization.

blnDone=Window("Flight Reservation").Dialog("Flights Table").Exist
counter=1
While Not blnDone
Wait (2)
blnDone=Window("Flight Reservation").Dialog("Flights Table").Exist
counter=counter+1
If counter=10 then
blnDone=True
End if
Wend