QTP – Closing Multiple Browsers

This article outlines a few techniques in QTP to close multiple browsers.

ChildObjects & .Close Method

This is probably the most common technique used. It uses a Browser Description Object and a loop to close all Browsers in the collection.

Dim oDesc, x
 
'Create a description object
Set oDesc = Description.Create
oDesc( "micclass" ).Value = "Browser"
 
'Loop through the collection and close each browser
If Desktop.ChildObjects(oDesc).Count > 0 Then
    For x = Desktop.ChildObjects(oDesc).Count - 1 To 0 Step -1
        Browser( "creationtime:=" & x ).Close
    Next
End If

Similarly, we can modify the snippet above and close all browsers except Quality Center:

Dim oDesc, x
 
'Create a description object
Set oDesc = Description.Create
oDesc( "micclass" ).Value = "Browser"
 
'Close all browsers except Quality Center
If Desktop.ChildObjects(oDesc).Count > 0 Then
    For x = Desktop.ChildObjects(oDesc).Count - 1 To 0 Step -1
       If InStr(1, Browser("creationtime:="&x).GetROProperty("name"), "Quality Center") = 0 Then  
          Browser( "creationtime:=" & x ).Close
       End If
    Next
End If

SystemUtil Methods

SystemUtil has the following methods that can be used to close processes, including browsers:

  • CloseProcessByName
      SystemUtil.CloseProcessByName "iexplore.exe"
  • CloseProcessByHWND: Uses windows handle to close a window.
      Dim HWND: HWND = Browser( "title:=Google" ).GetROProperty( "HWND" )
      SystemUtil.CloseProcessByHWND HWND
  • CloseProcessByWndTitle: Uses window title to close it.
      SystemUtil.CloseProcessByWndTitle "Google", True
  • TSKill with SystemUtil
      SystemUtil.Run "tskill", "iexplore"
  • Process ID with SystemUtil
    PID = Browser("title:=Google").GetROProperty("process id")
    SystemUtil.CloseProcessByID PID

SystemUtil.CloseProcessByID was shared by Pragya in the following topic.

WMI

This technique shuts down the processes (as seen in the Task Manager) instead of closing the actual window using its handle. Close all IE Browsers using WMI:

strSQL = "Select * From Win32_Process Where Name = 'iexplore.exe'"
 
Set oWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set ProcColl = oWMIService.ExecQuery(strSQL)
 
For Each oElem in ProcColl
    oElem.Terminate
Next
 
Set oWMIService = Nothing

The WMI concept can be used further to close all IE as well as all Firefox browsers.

Close all IE & Firefox Browsers Using WMI

strSQL = "Select * From Win32_Process Where Name = 'iexplore.exe' OR Name = 'firefox.exe'"
 
Set oWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set ProcColl = oWMIService.ExecQuery(strSQL)
 
For Each oElem in ProcColl
    oElem.Terminate
Next
 
Set oWMIService = Nothing

More on WMI can be found on MSDN.

Subscribe for Relevant Codes newsletter to get awesome updates and articles delivered straight to your inbox.

Leave a Comment

{ 52 comments… add one }

  • Nikhil March 16, 2013, 3:20 am

    I tried the first one when all instances of browsers close, but when 3 browsers are open only two of them close and always one browser remain open.

    Reply
  • Aditya January 25, 2013, 12:24 pm

    Hi,
    I have a question and requesting your help.
    I have a website i am testing, now the scenarion is as follows
    one browser window is open, after clicking on one button on website one pop up window opens so now there are 2 windows open and the focus shifts to this new window so I want to close this pop up window and shift focus back to previous window. I tried following code on your blog but it closes pop up window but focus does not shifts to main window so my tests fail.
    Browser(“title:=PC Connection, Inc. Business”).Close
    Browser(“Index:=1″).Close
    While Browser(“CreationTime:=1″).Exist(1)
    Browser(“CreationTime:=1″).Close
    Wend

    any help is highly appreciated
    Thank you
    aditya

    Reply
  • Sirish March 1, 2012, 4:00 pm

    Hi,

    I’m trying to test a web application that on clicking an ‘illustrate” button, opens a PDF.
    While the PDf window opens , the app, stores the data on a pop-up browser window with some text.
    My problem is that I’m not able to close this open pup-up wndow at the completion of the test.

    QTP closes everything except this window. I tried everything, but this is a dynamic popup window without any identification tags to it.

    Can you please help me on this?
    I need this window to close, so qtp can run iterations of the test.

    Thanks so much!
    Sirish

    Reply
    • uma March 2, 2012, 5:26 am

      Did u try a Recovery scenario.. check if u can add this window to recovery scenario.

    • Sirish March 2, 2012, 9:51 am

      @Uma…

      Will ry out your suggestion and I’ll keep you posted if it works or not.

      Thx for the idea!!

    • Anshoo Arora March 15, 2012, 8:44 am

      Sirish, there are 3 ways I can currently think of, let me know if neither works and we’ll try to create an alternate:

      1. Close all browsers: http://relevantcodes.com/qtp-closing-multiple-browser-windows/
      2. Store the hwnd of the pop-up when it opens – demo:

      'Click Illustrate button
      'Wait for popup to open
      'Get the hwnd:
      hWnd = Browser("index:=0").Object.hWnd
      
      'When you have to close it:
      Window("hwnd:=" & hWnd).Activate
      Window("hwnd:=" & hWnd).Close
      

      3. Retrieve the open url/title of the popup – demo:

      'Click Illustrate button
      'Wait for popup to open
      'Get the openurl/opentitle
      openTitle = Browser("index:=0").GetROProperty("opentitle")
      
      'When you have to close it:
      Browser("opentitle:=" & openTitle").Close
      
    • Sirish March 15, 2012, 9:07 am

      @Anshoo…

      Thank you sir!! for your suggestions. Will keep you updated if the solutions work or not.

      Thanks for the help!!

  • Shalini January 12, 2012, 6:44 am

    In this line it is taking much time

    Set BrowserCollection = Desktop.ChildObjects(obj)

    Is there any solution for this ??

    Reply
    • Anshoo Arora January 12, 2012, 11:53 am

      Shalini: What is your version of QTP?

  • Shalini January 12, 2012, 6:42 am

    Set Obj = Description.Create
    Obj(“micclass”).Value = “Browser”
    Set BrowserCollection = Desktop.ChildObjects(obj)
    For i = BrowserCollection.Count-1 To 0 Step -1
    If Instr(1,BrowserCollection(i).GetRoProperty(“Name”),”HP Quality Center”)= 0 Then
    BrowserCollection(i).Close
    Do while (Dialog(“text:=Windows Internet Explorer”).exist(0))
    Dialog(“text:=Windows Internet Explorer”).winbutton(“text:=OK”).Click
    Loop
    End If
    Next
    Set Obj = Nothing

    Reply
  • ajay November 10, 2011, 10:17 am

    i want to close a particular browser opend in the ie,the senearo is like this
    if we are opened 10 brws then we want to close 9 except one how it is possible pls help me

    Reply
    • Anshoo Arora December 27, 2011, 6:40 am

      Ajay, there are several ways you can do this. One example shown below:

      Set descBrowser = Description.Create
      descBrowser("micclass").Value = "Browser"
      
      Set colBrowser = Desktop.ChildObjects(descBrowser)
      
      For ix = colBrowser.Count - 1 To 1 Step -1
      	colBrowser(ix).Close
      Next
      
  • uma October 12, 2011, 10:24 am

    Hi,
    i’m trying the snippet in VB Script to close all browsers except Quality Center
    I get Object required error on this particular line Set oDesc = Description.Create
    can you please help me out with this.

    Reply
  • Ny renu September 28, 2011, 7:48 am

    Finally I got this working.
    ‘—————————————
    ‘This function closes the multiple browsers if they are open
    Set brwsr=Description.Create
    brwsr(“micclass”).value=”Browser”
    Set obj=Desktop.ChildObjects(brwsr)
    browserCount= obj.count
    Set obj=nothing
    If browserCount >0 Then
    While Browser(“CreationTime:=0″).Exist
    Browser(“CreationTime:=0″).Close
    While Dialog(“nativeclass:=#32770″).WinButton(“text:=Close all &tabs”).Exist
    Dialog(“nativeclass:=#32770″).WinButton(“text:=Close all &tabs”).Click
    Wend
    Wend
    Set obj=Desktop.ChildObjects(brwsr)
    browserCount= obj.count
    Set obj=nothing
    End If
    ‘———————————–

    Reply
    • Ny renu September 28, 2011, 7:48 am

      Thanks for your help.. :)

  • Ny renu September 28, 2011, 4:56 am

    Hi

    Thanks for the reply.

    I am using the following code. but it is not working. It is not detecting the dialog box and losing focus and trying to close the IE with the dialog box opened. I am getting object disabled error. Can you please help?

    thanks

    Set brwsr=Description.Create
    brwsr(“micclass”).value=”Browser”
    Set obj=Desktop.ChildObjects(brwsr)
    browserCount= obj.count
    Set obj=nothing
    If browserCount >0 Then
    Do
    While Browser(“CreationTime:=0″).Exist
    Browser(“CreationTime:=0″).Close
    ‘Browser(“CreationTime:=0″).FullScreen
    While Dialog(“text::=Do you want to close all tabs or the current tab?”).Exist
    Dialog(“Internet Explorer”).Button(“Close all tabs”).Click
    Wend
    Wend
    Set obj=Desktop.ChildObjects(brwsr)
    browserCount= obj.count
    Set obj=nothing
    Loop Until browserCount =1
    End If

    Reply
    • Anshoo Arora September 29, 2011, 9:50 pm

      Renu: Please try this:

      Set brwsr = Description.Create
      brwsr("micclass").Value = "Browser"
      
      Do
      	If Browser("creationtime:=0").Exist(0) Then
      		hWnd = Browser("creationtime:=0").Object.hWnd
      		Window("hwnd:=" &hWnd).Close
      	Else
      		Exit Do
      	End If
      	
      	If Dialog("nativeclass:=#32770").Exist(1) Then
      		Dialog("nativeclass:=#32770").WinButton("text:=Close all &tabs").Click
      	End If
      Loop
      
  • Ny renu September 21, 2011, 3:12 am

    hi

    When trying to close the multiple tabs in IE7 and above, I am getting a dialog “Do you want to close all the tabs or nly current tab?”

    I am not able to get rid of the same.. any help would be really appreciated.

    thanks

    Reply
    • Anshoo Arora September 24, 2011, 9:34 pm

      Ny renu: You can use the following code to click the button:

      Dialog("nativeclass:=#32770").WinButton("text:=Close all &tabs").Click
      
  • Tejaswini September 9, 2011, 3:56 am

    How to open the particular browser by passing data from excel sheet.
    consider we need to test in IE as well as firefox hw to select the browser while excecuting by passing data from excel.

    Reply
    • Anshoo Arora September 12, 2011, 10:23 pm

      You can use AOM to pass the data to QTP using Environment variables.. or you can use QTP to read the value directly from Excel instead.

    • Nikhil March 16, 2013, 3:27 am

      Say if you want to open “http://Google.com”

      then read this value from excel by importing the sheet
      Datatable.Import ” Sheetname”
      Get the value of specified cell value using excel object
      Set Xl=CreateObject(“Excel.application”)
      Set Wb=Xl.workbooks.open “sheet1″
      Set Ws = Wb.worksheets.open “Sheet1″
      URL=Ws.Cells(1,1,).value

      Hope this works

  • Daniel Anudeep August 24, 2011, 1:36 am

    Hi anoosh,

    Can u pls help me how to skip the “general run error” in QTP, without clicking the “skip” button in the generated popup, but by code directly, so that we can see the errors in “Test Results” directly after the complete code execution.

    Reply
    • Anshoo Arora September 12, 2011, 10:17 pm

      Daniel: The only way I know how to do it is using an “On Error Resume Next” statement, or trying to find out the reason that causes this error in the first place..

  • Rohit Patwari July 23, 2011, 2:04 pm

    Hi Anshoo ,
    It ‘s really a delight reading your posts.
    The post regarding closing multiple browsers is also too good.
    I required help from you.
    I have a browser (IE) , on which I have pop up.
    Also there are other browsers open.
    When I use SystemUtil.CloseProcessbyhwnd or closeprocessbyid or closeprocessbytitle , all the browsers get closed.
    But I want to close only the browser with pop up on it.How do I go about it

    Reply
    • Anshoo Arora August 1, 2011, 4:07 pm

      Rohit: Try and see if this code works:

      Browser("title:=Insert_Title_Of_The_Browser_Here").Close
      
      Browser("index:=0").Close
      
      Set oDesc = Description.Create
      oDesc.Add "micclass", "Browser"
      
      Browser("creationtime:=" & Desktop.ChildObjects(oDesc).Count - 1).Close
      
  • madhu June 22, 2011, 6:21 am

    It is a really good stuff. Helps beginners like us to learn a lot. Thanks a lot.

    Reply
  • Raja January 27, 2011, 8:08 am

    Hi Anshoo,

    Can we close QTP Process using below statement.
    Systemutil.CloseProcessByName “QTPRO.exe”

    Thanks,
    Raja

    Reply
  • shahvali September 14, 2010, 5:45 am

    Hi Anshoo,

    Type Mismatch” ‘ColBrowser’ Error is giving when i executed script that you mention under “Similarly, we can modify the snippet above and close all browsers except Quality Center:”

    Please let me know how to correct it

    Reply
    • Anshoo Arora September 29, 2010, 2:49 pm

      Shshvali,

      Thanks for catching the error in this snippet! I’ve already corrected it. I’m using x in the counter but using colBrowser(i) instead of colBrowser(x). It should be this instead:

      Dim oDesc, x
       
      'Create a description object
      Set oDesc = Description.Create
      oDesc( "micclass" ).Value = "Browser"
       
      'Close all browsers except Quality Center
      If Desktop.ChildObjects(oDesc).Count > 0 Then
          For x = 0 to Desktop.ChildObjects(oDesc).Count - 1
              If InStr(1, Browser( "creationtime:=" & x ).GetRoProperty("Name"), "Mercury Quality Center") = 0 Then  
                 Browser( "creationtime:=" & x ).Close
              End If
          Next
      End If
      
    • QA February 16, 2011, 5:16 pm

      That code doesnt work and I dont understand where you have colBrowser()!! I have absolutely no idea what you fixed as the new code still doesnt work as it fails on the last open browser with object not found issue.

    • markQA February 16, 2011, 9:05 pm

      Mr. QA, your tone indicated the web admin of this forum works under you.

      Please be reasonable and be polite.

      Thanks.

    • Anshoo Arora February 19, 2011, 7:24 am

      Which part does not work? Can you include the code?

  • Mallikarjuna September 9, 2010, 5:15 am

    Hey ,
    I want to close my browsers except specific windows? how i can do that. I have 5 browsers open, i want to close 3 , Can you please suggest without using the creation time method?

    Reply
    • Anshoo Arora September 9, 2010, 1:56 pm

      Do all these browsers have exactly the same title? Different titles? Any unique attributes between these?

  • SURITH June 8, 2010, 5:22 am

    I have a parent browser and 2 child windows opened. Could you guys/gals please help me in identifying these browsers separately using QTP. The “Browser (“Creation Time:=1″) ” strategy is not working for me.

    Reply
    • Anshoo Arora June 8, 2010, 7:29 am

      Surith, its CreationTime, not Creation Time. No space is required..

      Browser("creationtime:=1")
      
  • Ajitpal Singh June 8, 2010, 2:25 am

    My point was below code provided by you to close browser would not work when we have multiple browsers opened.
    For x = 0 to Desktop.ChildObjects(oDesc).Count – 1
    Browser( “creationtime:=” & x ).Close
    Next

    So this is the correct version that should work
    For x = 0 to objChild.Count – 1
    objChild(x).close
    Next

    Also I could not understand what difference it would make if we run the loop from a greater creationtime to a smaller one or from smaller one to greater creationtime.

    Thanks!

    Reply
    • Anshoo Arora June 8, 2010, 7:28 am

      Hi Ajitpal,

      The reason why the code from zero to a higher CreationTime fails to work is because, a loop that is incrementing is looking for a values of a higher CreationTime, but as a few browsers close, the higher CreationTime browsers take the place of a browser that has a lower CreationTime. Consider this example:

      1. 5 Browsers Open:
      CreationTime: 0 1 2 3 4
      2. You close the first 2 browsers:
      CreationTime: 0 1
      3. This will have 3 browsers left to be closed with the new CreationTimes:
      CreationTime: 0 1 2
      4. Therefore, as the browsers with lower CreationTime are closed, the higher ones take their place. This is why the loop fails.

  • ajitpal singh June 5, 2010, 12:32 pm

    I have provided the correct code to close the browser. As by following code provided by Anshoo would not work when we have multiple instances of browser opened.As whenever we close the browser creation time is descreased.

    Reply
    • Anshoo Arora June 7, 2010, 8:56 am

      In that case, you have to run the loop from a greater creationtime to a smaller one like this:

      Set oDesc = Description.Create
      oDesc("micclass").Value = "Browser"
      
      Set oParent = Desktop.ChildObjects(oDesc)
      
      For ix = oParent.Count - 1 to 0 Step -1
      	oParent(ix).Close
      Next
      
  • Ajitpal Singh June 4, 2010, 7:07 am

    Dim oDesc, x
    ‘Create a description object
    Set oDesc = Description.Create
    oDesc( “micclass” ).Value = “Browser”
    ‘Loop through the collection and close each browser
    Set objChild = Desktop.ChildObjects(oDesc)
    If objChild.Count > 0 Then
    For x = 0 to objChild.Count – 1
    objChild(x).close
    Next
    End If

    Reply
    • Narinder singh July 27, 2010, 2:43 am

      Your ansnwer is perfect.

    • Nikhil March 16, 2013, 3:37 am

      Last instance of browser remains open , it doesnt close.

      what should I do?

  • Sridhar April 21, 2010, 10:20 am

    hi,

    What if the browser have some alert message, which makes the browser disabled and the close fails.
    Like, session expiry alert pop up as come, so when we try to close it it throws error.

    Reply
    • Anshoo Arora May 12, 2010, 11:05 am

      Sridhar,

      In that case, the alert must be first handled through code. I *think* with the help of WMI, you can even bypass the alert and directly close the process.

  • steven March 10, 2010, 11:38 pm
    While Browser("CreationTime:=.*").Exist(1)
        Browser("CreationTime:=.*").Close
    Wend
    

    also works

    Reply
    • Anshoo Arora March 11, 2010, 4:00 pm

      That’s quite a neat approach, Steven.

      I will update the post tonight giving full credit to you. Thanks for sharing :)

  • Khushal September 10, 2009, 5:10 am

    Hi Anshoo
    thanks alot for ur help….its working fine now.. i achieved what i wanted
    thanks for the help..
    cheers
    Khushal

    Reply
    • Anshoo Arora September 10, 2009, 4:45 pm

      That’s great, Khushal! I’m glad it worked.

  • Khushal September 9, 2009, 2:25 am

    Hi Anshoo
    Please help me with the following query:
    Currently i am having 5 Java Windows of my application.
    1. JavaWindow(“a”)
    2. JavaWindow(“a”)
    3. JavaWindow(“a.login.*”)
    4. JavaWindow(“b”)
    5. JavaWindow(“c”)

    I need to close instances 1,2 and 3 but not 4 and 5.
    The problem is that the repository gets ambiguous when i try closing 1 and 2 since both correspond to the same objects in the repository. Can you figure out a way to achieve the target?

    Regards
    Khushal

    Reply
    • Anshoo Arora September 9, 2009, 8:05 pm

      Hi Khushal,

      To close the 2 selected JavaWindows, you can do something like this:

      Dim oDesc, colObject, ix
      
      Set oDesc = Description.Create
      oDesc("micclass").Value = "JavaWindow"
      
      'Create a collection object with all JavaWindows
      Set colObject = Desktop.ChildObjects(oDesc)
      
      For ix = 0 to colObject.Count - 1
              'If title does not equal "b" or "c", then close window
      	If Not colObject(ix).GetROProperty("title") = "b" Or _
        		  Not colObject(ix).GetROProperty("title") = c Then
      		SystemUtil.CloseProcessByHWND colObject(ix).GetROProperty("hwnd")
      	End If
      Next
      
      Set colObject = Nothing
      Set oDesc = Nothing
      

      I hope this will work for you. If it doesn’t, we’ll surely try another technique.