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

All about working with browser object in QTP

This article discusses some useful code snippets working with browser object in QTP and useful tips while working with browser in QTP.

1.  How to launch a webpage in any of the browser.

Well, there are multiple ways to launch a webpage in browser of choice

a.  Using Systemutil command - SystemUtil.Run is an inbuilt QTP command used to launch an application. We can launch a browser using Systemutil as:

                  SystemUtil.Run "Chrome.exe", “http://www.thoughtscollectionme.blogspot.in/

Using systemutil.run we can launch other application, e.g.: we can open a word document

                  SystemUtil.run “winword.exe”, “C:\meraword.docx”

Other methods for Systemutil command are:

CloseProcessByName: Closes the process based on the name of the process. E.g.: If we want to close chrome in the machine, we can use:
                 SystemUtil.CloseProcessByName "chrome.exe"

CloseProcessByHwnd: Closes the process based on the window handle of the process.
                  SystemUtil.CloseProcessByHWND handleid

b.  Another method is to launch the browser using Wscript.shell object. The code to launch the application using wscript.shell object is as follows:

                 Set objShell = CreateObject("Wscript.shell")
                 objShell.run “chrome.exe  http://www.thoughtscollectionme.blogspot.in/
                 set objShell = Nothing

    2. How to know how many browsers instances are open at a particular instance:

We can get the number of open instance of browser using below function
  Function getbrowsercount()
               ‘’Create an object of description class
              Set objBrowser = Description.Create
             ‘’ We have created an description object with micclass as browser
             ObjBrowser(“micclass”).Value = “Browser”
            ‘’objbr stores collection of objects which matches property of micclass as browser
             Set objbr = Desktop.ChildObjects(ObjBrowser)
           ‘’ return the count of browsers in the function itself.
            getbrowsercount = objbr.count
            End Function

3. How to know the title of each of the  open browser

     Set objBrowser = Description.Create
    ObjBrowser(“micclass”).Value = “Browser”
    Set objbr = Desktop.ChildObjects(ObjectBrowser)
    getbrowsercount = objbr.count
    For i= 1 to objbr.count
   ’’Loop through each of the browser and print the title of browser in message box
            Msgbox objbr(i).getRoproperty(“title”)
    Next

    4. Now we need to close all the open browsers?

  ‘’We use ordinal identifier to identify an test object by assigning a numerical    value which indicates object’s  location or order relative to its group. Different types of Ordinal identifier are:
·         Index
·         Location
·         CreationTime.
While Browser(“Creationtime:=0”).exist(0)
Browser("creationtime:=0").Close
Wend

 5. How to close all browser except Quality Center?

When we run a test in QTP, we do not require QC to close but other browsers to close. The code can easily be derived using the codes explained above.
The code will be something like:
Function CloseAllbrowsersExceptIE(browsertitle)
Set objBrowser = Description.Create
objBrowser("micclass").Value = "Browser"
'Get all browsers
Set objBrowserLst= Desktop.ChildObjects(objBrowser)
For i=0 to objBrowserLst.count-1  
 'Verify the title of the browser contains "Quality Center"  
If InStr(objBrowserLst(i).GetROProperty("title"), "Quality Center”) = 0 Then
                                objBrowserLst(i).close  
            End If  
            Next 
Set objBrowser = nothing
Set objBrowser = nothing
End Function

6. Next question is how to close only a particular browser only and keeping the other browsers open.


The code is very much similar to above problem where we close all the browser except QC.
Just need to change the if statement in the code above:

If InStr(objBrowserLst(i).GetROProperty("title"), "Quality Center”) = 0
to
If InStr(objBrowserLst(i).GetROProperty("title"), "Quality Center”) > 0

will close only the particular browser and keep the other browser open.


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.


Example to explain working with excel application in QTP using VBScript

 In this post, we will know how to work with excel application with an example . We will explain how to copy a source file and create an destination file with same name or a dynamic name generated. We will also explain how to copy data from an excel worksheet to another worksheet, delete an excel sheet, and rename an excel worksheet using excel application object.

 The problem we will resolve using this code is as follows. 


In the below code, we will explain how to implement the above using vb script.

destfile = CopyFile("D:\testt\TestSupport_1.xls", "D:\testt\", 1)
copydata "D:\testt\generatedRpt.xls", destfile, "general_report", "temp"
masterfile = Copyfile(destfile, "D:\testt\TestingSupport_Daily_Data.xls",0)


'' Description - This function will copy the file from source location to destination folder. In this example we have used below three parameter as per our need.

''Source file - Exact name and location of the source file. This is an external file generated at a fixed location which was first part of problem and will be handled using QTP
''DestinationFolder - this is either the folder location with or without file name. 
''filename - this shows filename is included in the the destinationfolder. In case of dynamic naming of the file, provide value as 1 else provide value as 0.

function CopyFile(SourceFile, DestinationFolder, filename)
if (filename =1) then
'' generate a dynamic name for the file in case filename argument is provided as 1
    DestinationFile = DestinationFolder + "TestSupport" + cstr(DatePart("m",Now()))+ cstr(DatePart("d",Now()))+ cstr(DatePart("yyyy",Now())) + ".xls"
else
   DestinationFile = Destinationfolder
end If

    Set fso = CreateObject("Scripting.FileSystemObject")
    'Check to see if the file already exists in the destination folder
    If fso.FileExists(DestinationFile) Then
    ''In case of destinationfile already exists, delete the existing file
fso.DeleteFile DestinationFile, True
    End If

'' Copy the file from source location to destination location
    fso.CopyFile SourceFile, DestinationFile, True
''close the instance of filesystem object
    Set fso = Nothing
''return the value of file name as the rsturn value of the function.
copyfile = Destinationfile
End function
'

'' Description - this function will copy data from sheet1 of workbook1 to sheet2 of workbook2.


sub copydata(workbook1, workbook2, sheet1,sheet2)
'' Create an instance of excel object
Set objExcel = CreateObject("Excel.Application") 
'' define if the excel needs to be displayed and alerts to be displayed. here the excel will be visible but no alerts will be displayed in case of alert prompt in the excel application.

objExcel.Visible = True
objExcel.DisplayAlerts = False
'' create an instance of excel workbooks for source as well as destination workbook
Set objsrcFile= objExcel.Workbooks.Open(workbook1)   
Set objdestfile = objExcel.Workbooks.Open(workbook2) 
objsrcfile.Activate
'' create instance of worksheet in the workbook
Set objsrcSheet = objsrcfile.Worksheets(sheet1)
Set objdestSheet = objdestfile.Worksheets(sheet2)
'' gather the data from the source file to destination file
'' In the below example , we are copying the data from 4th row in source file to destination file. we can start from first row also if required.
icolcount = objsrcSheet.usedrange.columns.count
irowcount = objsrcSheet.usedrange.rows.count
for i = 4 to irowcount-1
for j = 1 to icolcount-1
''copies data from the destination file to the source file
objdestSheet.cells(i-3,j)= objsrcsheet.cells(i,j)
next
next
objsrcfile.save
objdestfile.save
''This is again a customised code to delete a sheet from excel file. 
objdestfile.sheets("Previous Day").Delete
'' This step renames the sheet in the destinstion file.
Set objWrksheet = objdestfile.Worksheets("Today")
objWrksheet.Name = "Previous Day"
set objWrkSheet = nothing
''Rename another sheet in the workbook
Set objWrksheet = objdestfile.Worksheets("temp")
objWrkSheet.Name = "Today"
set objWrkSheet = nothing
''Add a temp table at the end of sheets and name it as temp
Set objWrkSheet = objdestfile.Sheets.Add(, objdestfile.Sheets(objdestfile.Sheets.Count))
objWrkSheet.Name = "temp"

''Save the excel file and close the instance of excel aplication
objdestfile.save
objsrcfile.close
objdestfile.close
objexcel.quit
set objexcel = nothing
msgbox "done"
end sub