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

Utility Function for Working with Loading OR/Libraries, Capturing Image/Text and QTP Reporter in QTP

Below are some of the useful utility functions in QTP

Loading OR/Libraries


1.    Function to load Library Dynamically:

This function will load function library at runtime with the test.
 LoadFunctionlibrary(Library Name)

2.    Function to Load OR dynamically:

This function will add an object repository to the test dynamically at run time.
 RepositoriesCollection.AddRepositoryName

Capturing Image/Text


3.   Capturing Image at run time:

    This will capture the screenshot at runtime
    Browser(“…”).Page(“…”).capturebitmap(path to store image”)


4.   Capture Text:

This will capture the text at runtime to the clipboard. 

setobjClip=createobject("Mercury.Clipboard")
objClip.SetText(someText)
printobjClip.GetText

Reporting event


5.    Reporter.reportEvent: 

   This will add user defined result to the QTP Results
   Reporter.reportEventstatus,Event,Details

6.    Reporter.Filter:

This will filter the events to be written in QTP
Reporter.Filter = rfEnableAll

7.    Systemutil.run:


 This will execute statement similar to what we write in the run in start menu
Systemutil.run(Process)



Error Handling using Recovery scenario

When we run a test or a batch of test in QTP, tests may start failing in case application crashes or a pop up window appear during test execution. QTP needs to recover from such error and continue with execution of test script. QTP provides creating recovery scenario through recovery scenario manager so that the tests are executed uninterrupted. In this article we will discuss on recovery scenarios.

QTP enables user to create recovery scenarios and associate them with specific tests. Recovery scenarios activate specific recovery operations when trigger events occur. Recovery scenarios are intended for use only with events that cannot be predicted in advance, or for events that cannot otherwise be synchronized with a specific step in the test. 

The Recovery Scenario Manager provides a wizard that guides through the process of defining a recovery scenario, which includes a definition of an unexpected event and the operations necessary to recover the run session.

A recovery scenario consists of the following:


•       Trigger Event. The event that interrupts run session. This can be application error or a pop up window appearing

•       Recovery Operations. The operations to perform to enable QTP to continue running the test after the trigger event interrupts the run session.

•       Post-Recovery Test Run Option. The instructions on how QTP should proceed after the recovery operations have been performed

Following are the steps in creating recovery scenario using recovery scenario manager:


  • Create a new scenario
  • Define the trigger events
  • Define recovery operation
  • Define post recovery operation
  • Naming the scenario and associating with test.

 Step 1: Creating a new Scenario


Select Resources > Recovery Scenario Manager.


Step 2: Define Trigger Event

Select the required trigger Event and click on next button
A Trigger event can be one of the following:
•       Pop-up Window
•       Object State
•       Test run error
•       Application Crash

Step 3: Recovery Operation:

This page enables  to manage the collection of recovery operations in the recovery scenario. A recovery Operation can be one of the following:


•       Keyboard or mouse operation
•       Close application process
•       Function call
•       Restart Microsoft Windows




Step 4: Post-Recovery Test Run Options Page

This page enables  to define post-recovery test run options, which specify how to continue the run session after QTP has identified the event and performed all of the specified recovery operations.

Following are the options available post recovery:

•       Repeat current step and continue
•       Proceed to next step
•       Proceed to next action or component iteration
•       Proceed to next test iteration
•       Restart current test run
•       Stop the test run


Step 5:  Completing the Recovery Scenario Wizard Page

This page enables  to review a summary of the scenario settings  defined and provide option to add scenario to current test. 




Useful Info:  We have an existing recovery scenario that covers common errors and issue for Recovery . The file is available in "C:\Program Files\HP\QuickTest Professional\recovery\WebRecovery.qrs". Do have a look, this will be very useful to use existing recovey scenario for most of common error

Error Handling using vbscript in QTP

Introduction to Error Handling using  vbscript in QTP:


Unexpected events during a test run disrupt a test or may give invalid test results. For example, during a test run, an application error may occur. This error does not permit the automated test to navigate to the feature, screen, or module that needs to be tested. These unexpected errors and events are called exceptions. It becomes important to handle these exceptions so that we are able to continue with automated testing even in unattended mode. Handling of exception in a manner so that test execution is uninterrupted is known as error handling.

Following are the ways in which error handling can be implemented in QTP.


       VB Script Error Handling

o   Using Test Settings
o   Using On Error Statement
o   Using Err Object Properties
o   Using Exit Statement

       Recovery Scenarios


Test Settings : Error Handling


Error Handling can be defined  in Test Settings through File>Settings>Run as shown below:
It defines the possible actions in case  an error is encountered during run session


Test Settings for error handling

Using On Error Statement


       On Error Statement

On Error statement, which informs the VBScript engine of intention to handle errors by self, rather than to allow the VBScript engine to display a typically uninformative error message and halt the program. This is done by inserting a statement like the following at the start of a procedure:    
  

       On Error Resume Next:

On Error Resume Next tells the VBScript engine that, should an error occur, we want it to continue executing the program starting with the line of code that directly follows the line in which the error occurred.

       On Error Goto 0: 

This turns off the error handling


Err Object Properties and Methods


The Err object is part of the VBScript language and contains information about the last error to occur. Some of the most useful properties and method of Err object are as follows:

Err.Number Property: The Number property returns or sets a numeric value specifying an error. Number is the Err object's default property.If the value of Err.Number is 0, no error has occurred.

Err.Description Property: The Description property returns or sets a descriptive string associated with an error.    
            
Err.Clear MethodThe Clear method clears all property settings of the Err object.


Following is a code snippet how error handling can be implemented programmatically.

Public  Function  FunctER()
On error resume next
Lines of code
If (err.number<>0) then
       Reporter.ReportEvent micFail, “Func1", err.description
Else
      Reporter.ReportEvent micFail, “Func1", “Function executed”
End If
Err.clear
End Function


Using Exit Statement:


Exit Statement can be used in conjunction with err object to exit from a test/action/iteration in case of an error encountered. Following are the exit statements that can be used in QTP to exit from a particular state:

ExitTest: Exits the entire QTP test or Quality Center business process test, regardless of the run-time iteration settings.

ExitAction: Exits the current action.

ExitActionIteration: Exits the current iteration of the action.   

ExitTestIteration:  Exits the current iteration of the QTP test or Quality Center business process test and proceeds to the next iteration.


QTP Descriptive programming Basics

Descriptive programming is creating of description of objects so that they can be identified once executed from QTP. It is approach for object identification together with object repository.

Why Descriptive Programming:


 Descriptive programming is useful due to following reasons:
  • It reduces the effort of maintaining and associating object repository with the test.
  • Maintenance is simpler.
  • In scenarios, when application is not developed but we know the properties of objects that will be added in the application. In such scenarios , we cannot maintain object repository since application is not ready and Objects cannot be learned from the  application. So Using Descriptive programming, we can prepare the scripts which can be executed once application is up and running.
  • It is useful when performing similar actions on multiple objects with similar properties
  • It is useful when object can be uniquely identified using fewer set of properties existing across multiple set of objects.
 

     OR Vs Descriptive programming


      When QTP learns an object during recording session or through object spy, object is identified and saved in object repository. QTP saves properties like innertext, name for the object in the object repository to uniquely identify an object. Similar to this, Descriptive programming creates a unique description of the object which is identified.

      With using Object Repository, we can use features like relative identifier, and other QTP specific features. Also time to create script based on descriptive programming is more compared to object repository. So for projects and for limited use of automation pack and applications with mostly static pages, Object repository can be better approach.

    Types of Descriptive programming


       Descriptive Programming can be implemented in following ways.
  •           Creating description string
  •           Using Description object.
Let us discuss the ways in some details:

  •       Creating description string:


             Browser("title:=Google").Page("title:=Google").WebEdit("name:=q").Set “DP”

 Above we created a description string similar to tree structure we see in expert view.

Below are few points to remember in creating description strings.

1. The parent object in the string can be used from Object repository, in case child object is defined in string.
2. Child objects cannot be used from OR  if string description is described for parent in the tree.

  Browser("Google").Page("Google").WebEdit("name:=q").Set “DP” is possible but
  Browser("title:=Google").Page("title:=Google").WebEdit("q").Set “DP” will not identify the object

  In case multiple objects are to defined , they needs to be separated by , .
  Browser("title:=Google").Page("title:=Google").WebEdit("name:=q”,html tag = In").Set “DP”


     To identify the properties of the object to be used in descriptive programming, Go to Object Spy and click on object.
  •       Go to Identification button and properties tab
  •       Select properties and property values and create string such that the identification is unique.

     In case of multiple objects with same properties, we can use index property to identify object based on indexing.

  •      Using Description.Create:  

Description object creates a collection of object based on object matching the defined properties.

Steps in description Object


         1. Define description object:

          set odesc = description.create
          we have defined an description object odesc.

         2. Add property to the object defined in step above.

               odesc(“name”).value =”q”
               odesc(“html tag”).value =”Input”
               we have defined the object with properties.

      3. Replace string with description object.

            Browser("title:=Google").Page("title:=Google").WebEdit(odesc).Set “DP”
            we have replaced string with description object.

     This description object can also be used to find child objects in the tree structure. For e.g : we can find all the links in the pages, check box, perform action on all the objects in the collection.

Below code explains the same:

odesc = Description.create
odesc(‘”html tag”).value =”Input”
odesc(“type”).value = “checkbox”
Set ochkbox = Browser("title:=Google").Page("title:=Google").childobjects(odesc)
For each obj in ochkbox
obj.set “ON”
Next

We can use ochkbox.count to get objects matching the description


HTML DOM and QTP


HTML DOM is language independent model for representing and interacting with objects in html. It defines the structure for HTML and help in traversing through objects in HTML. QTP supports HTML DOM and following methods are used to identify an object in HTML. This approach can be useful in case QTP is not able to identify the object.


Following are the methods by which we can identify an object using HTML DOM using QTP. We will explain methods used to extract information from HTML DOM and will  use the below html as reference.

<!DOCTYPE html>

<html>

<body>

<p id="para">Hello World!</p>

<p>Paragraph2</p>

<a id=”Link1” name =”LinkName” class=”LinkCls”>LinkTest</a>

<a id=”Link2” name =”LinkName” class=”LinkCls2”>LinkTest2</a>

<a id=”Link3” name =”LinkName” class=”LinkCls3”>LinkTest3</a>

</body>

</html>

GetElementsbyTagName Method



 This methods gives a list of all objects with the specified TagName. For e.g.:  Set objColl =Browser(“…”).Page(“….”).object.getElementsbyTagName(“a”)

This will give the list of all elements in the Page with tag name as “a”. In above sample html code, there are three elements with tag name as a. So objColl will be a collection of three elements.

In this example, we can access the link with class name as LinkTest2, we can use the below function

           Public Function funcGetParticularElem(strObjName)
    boolelemstat = False
   set  objLinkColl=  Browser("…..").Page("title:=.*").Object.getElementsByTagName("a")
  For each objLink in objLinkColl
  If (objLink.classname =strObjName) Then
              objLink.click
              boolelem = True
              Exit For
End If
Next
If (boolelem= True) Then
         Msgbox “Element Found”
Else
       Msgbox “element not found”
End If


GetElementsByName Method


This gives the list of all objects matching the specified name.

For e.g.: Set objColl = Browser(“…”).Page(“….”).object.getElementsbyName(“LinkName”)

This will give the list of all elements in the Page with name as “LinkName”. Similar to above explained example on how to use HTML DOM for GetelementsbyTagName method, we can use the same for getelementsbyname method and extract useful information for the element.


GetElementByID Method


This gives the list of objects with a particular Id . Normally Id’s are unique for each object in the Page, else it picks the first element of the object satisfying the Id.

For e.g: Browser(“…”).Page(“….”).object.getElementbyId(“Link1”).click

This will select the object with Id as “Link1” in the Page and perform the requested operation on the page. Difference between GetElementById and above methods is whereas the above methods give collection of elements. This method gives the element with the Id provided. Also Id defined in the page is unique for

RunScript Method


 This help to execute java script statement in QTP.

For e.g: 
Browser(“…”).Page(“….”).runscript(alert(”Hello World”);)

Once we have the collection of the objects, we can iterate through the collection and compare properties of the object like innerHTML,innerText with the expected value, and perform action on a particular object.

How to identify Object properties in the HTML Page

Using developer toolbar for object identification


We can use HTML DOM to extract information from the page and is very useful in case object is not identified by QTP directly.

Suppose there are two buttons with similar properties in a web table. We can use HTML DOM to identify object based on the unique object in the next cell of web Table. To traverse the DOM structure and find properties of every element in the page, there are various inbuilt and external tools based on browsers.In IE and chrome, we can press F12 to view DOM structure and inspect elements in the html Page.


Understanding Checkpoints in QTP

A checkpoint verifies that expected information is displayed in the application while the test is running. Checkpoint helps to identify whether the application is functioning correctly. Checkpoint is a verification step that compares the current value for specified properties or current state of an object with the expected value or characteristics. If the results do not match, the checkpoint fails. Results of the checkpoint are displayed in the Run Results Viewer.

Types of checkpoints

Following are the different types of checkpoints that can be created in QTP.
Standard Checkpoint: Following checkpoints are created in QTP as Standard Checkpoint.
  •  Page Checkpoint - checkpoint inserted on page object
  •  Image Checkpoint - checkpoint inserted on image object in the page
  • Table Checkpoint - checkpoint inserted on table object in the page
  • Standard Checkpoint - checkpoint on object other than page, image and table object.

Bitmap Checkpoint - verifies the actual bitmap of the area with expected bitmap 

XML Checkpoint - compares expected xml with actual xml

Database Checkpoint - compares expected value in database with actual value

Accessibility Checkpoint - compare accessibility of the page.


checkpoint in QTP


Inserting a Checkpoint

 Standard checkpoints are used to perform checks on images, tables, Web page properties, and other objects within application.
A checkpoint can be inserted in the test in following ways:

•    During Recording SessionTo insert a checkpoint while recording,start a recording session before proceeding to the next step.
•    Using Active Screen If using the Active Screen option, ensure that the Active Screen contains  sufficient data for the object that needs to be checked.
•  During Editing Sessions:  To insert or modify an existing checkpoint step while editing, open the application and display the relevant object before proceeding to the next step.

Following are the steps to insert a standard Checkpoint.
•Select Insert > Checkpoint menu for inserting a checkpoint during recording mode or right click on object in active screen.
•Select option Insert Standard Checkpoint which opens the Checkpoint Properties dialog box. Select the node in the tree where checkpoint needs to be inserted and click OK.

Checkpoint Properties:

Once an object is selected for checkpoint, different checkpoint dialog box opens based on the object type selected as shown below:
Following are the area displayed for checkpoints properties:

  • Object Details area: name that QTP assigns to the checkpoint object and the class of Object.
  • Properties grid area: For each object class, QTP recommends default property checks. To check a property, select the corresponding check box in the properties grid area.
  • Configure Value area: This area enables  to configure object property values or the values of the operation arguments defined for the step.
  • Checkpoint Timeout and Statement Location area: Checkpoint Timeout Specifies the time interval (in seconds) during which QTP attempts to perform the checkpoint successfully.Insert statement specifies whether to insert the checkpoint step before or after the currently selected step

Checkpoint  in Expert View

Checkpoint is displayed in Expert View as:

Browser("Mercury Tours").Page("Flight Confirmation").Check Checkpoint("New York")
Return value from checkpoint can be stored  in a variable.
Eg: Dim return
return = Browser("Mercury Tours").Page("Flight Confirmation").Check Checkpoint("New York")

This will give value as true or false based on checkpoint passed or Failed.