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

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

XPath Object Identification in UFT descriptive programming

UFT also supports XPATH and css together with descriptive programming, object repository and DOM for object Identification.

Xpath stands for Xml Path. Below are some XPath examples to be used with UFT. Xpath and css are most widely used object identification way in selenium also.


Xpath examples:

Id: 

Browser(...).Page(...).weblink("xpath:=//a[@id='linkuft']").click

Attribute Value:

Browser(...).Page(...).weblink("xpath:=//a[@Atttribute='goodclass']").click

text:

Browser(...).Page(...).weblink("xpath:=//a[text()='UFT']").click 

partial attribute value

Browser(...).Page(...).weblink("xpath:=//a[starts-with(@attribute,'goodclass']").click

Browser(...).Page(...).weblink("xpath:=//a[contains(@attribute,'goodclass']").click

Browser(...).Page(...).weblink("xpath:=//a[ends-with(@attribute,'goodclass']").click


To know details on what is CSS and XPath and how to capture CSS or xpath of an element. refer to

XPath and CSS for selenium/UFT

We can use tools like firebug/firepath or IE developer tool to identify the xpath/css of an element.

xpath is really useful when element is not identified diretly based on Object repository properties and used to
identify an element based on relative element existence based on other elements in the tree.

Ordinal Identifier in QTP

While learning the objects, QTP tries to identify objects based on the mandatory and assistive properties of the object. In case the above properties are insufficient to uniquely identify the object. QTP uses the ordinal identifier properties to uniquely identify the object together with the madatory and assistive properties.


There are three type of Ordinal Identifier in QTP which are assigned as numeric integer value for the object based on the order occurrence of object in the browser.


The three types of ordinal identifier are described below:



Ordinal Identifier

Description

 Index Based

Based on the object in the window, An index value is assigned to object based on existence in the source code. Index value starts from 0.

Location Based 

The Location property is based on location of object, works vertically from top to bottom and then from left to right. value starts from 0.

Creation Time

The Creation Time property is used for browser objects mainly and used to identify multiple browsers based on the time the browser/page was open. Value starts from 0

VBA Code- Extracting Database data into excel sheet using macro

Code to read the data from database and writing data in excel file using VB Macro and VBA.



'''' Naming the sub as auto_open will trigger the sub automatically on opening the file.
'''' We can create a sub with different name and assign macro to a control in the excel
''''file 
Sub auto_open()
'''' define the connection string to connect to the database, it can be a dsn created,
'''' or connection string to create to the database. Please refer connectionstrings.com
'''' to connect to different database using connection string.
connStr = "dsn=testthedb''
'''' Query to fetch data from the database
strGetEmployeeDetails = "select * from employee order by UserName asc;"
''''Provide reference of sheet to add data to in the current workbook. We can add a
''''sheet in excel, or refer to an external excel file/Sheet using excel.application.
''''In this example, Sheet Employee_Details exist in the current excel file
Set worksht = ThisWorkbook.Sheets("Employee_Details")
''''--------------------------
'''' Create database connection
   Set Conn = CreateObject("ADODB.Connection")
   Set rset = CreateObject("ADODB.Recordset")
   Conn.Open connStr
   rset.Open strGetEmployeeDetails, Conn, adOpenStatic

''''The recordset is stored in rset.
''''The first row of data is the column details of the query results
''''and is added in the results header

For i = 0 To rset.Fields.Count - 1
worksht.Cells(2, i + 2) = rset.Fields(i).Name
Next i

''''Copy the recorset data into excel file starting at row 3 and column 2
worksht.Cells(3, 2).CopyFromRecordset rset
rset.Close
Set rset = Nothing
ThisWorkbook.Save
Conn.Close
Set Conn = Nothing
End Sub

What is the difference between ByRef and ByVal? –QTP Interview question

ByRef and ByVal are two ways to pass arguments to a function or subroutine in VBScript. Understanding the difference between two is simple.

Let us start with ByVal. Suppose a function or subroutine has an argument which accepts a variable with byval,suppose a variable x is passed as argument to the function, The value of variable does not change once the function is executed successfully.

Now let us move to ByRef. ByRef means passing value as a reference. Suppose a function or subroutine has an argument which accepts a variable with byref, suppose variable x is passed as argument to the function, The value of variable changes once the function is executed successfully.

Writing the below function in a notepad and running as a vbscript will help to differentiate ByVal and ByRef


By Ref Code:


Dim x
x= 5
call squareofNumber(x)
msgbox x
Function squareofNumber(ByRef varx)
        varx = varx*varx
        msgbox varx
End function


ByVal Code:


Dim x
x= 5
call squareofNumber(x)
msgbox x
Function squareofNumber(Byval varx)
        varx = varx*varx
        msgbox varx
End function
 By default, if we do not provide ByVal or Byref, it takes the argument as ByRef.


How to create a database checkpoint in QTP

A database checkpoint compares the values returned by SQL query during checkpoint execution. Below are the steps to create a database checkpoint in QTP


  • Navigate to Insert>Checkpoint>Database Checkpoint.


  • Define whether to define SQL statement manually or using Microsoft Query


  • Select the database or query if previously defined as shown below.We select QT_Flight_32 database.Click on OK.

  • Select the required columns/tables to be selected in SQL and click on next button.


  • Define conditions for selected columns in the above step and define sorting criteria. Click Next>Next


  • Click on finish



  • Change the properties of database checkpoint and select whether to insert the checkpoint before or after current step and clcik on OK. A database checkpoint is created and inserted in the test.



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


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