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
‘’Create an object of description class
Set objBrowser = Description.Create
‘’ We have created an description object with micclass as browser
ObjBrowser(“micclass”).Value = “Browser”
ObjBrowser(“micclass”).Value = “Browser”
‘’objbr stores collection of objects which matches property of
micclass as browser
Set objbr = Desktop.ChildObjects(ObjBrowser)
Set objbr = Desktop.ChildObjects(ObjBrowser)
‘’ return the count of browsers in the function itself.
getbrowsercount = objbr.count
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
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.