This article outlines a few techniques to close IE (and Firefox) 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. Pragya, thanks for sharing this concept with us :)
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.
Thanks for visiting Relevant Codes.
If you have any questions, please ask them in the comments section. If your query is confidential, please use the Contact Form to send me an e-mail instead.
{ 43 comments… read them below or add one }
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
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 = NothingI hope this will work for you. If it doesn’t, we’ll surely try another technique.
Hi Anshoo
thanks alot for ur help….its working fine now.. i achieved what i wanted
thanks for the help..
cheers
Khushal
That’s great, Khushal! I’m glad it worked.
While Browser("CreationTime:=.*").Exist(1) Browser("CreationTime:=.*").Close Wendalso works
That’s quite a neat approach, Steven.
I will update the post tonight giving full credit to you. Thanks for sharing :)
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.
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.
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
Your ansnwer is perfect.
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.
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 NextMy 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!
Hi Ajitpal,
The reason why the code from
zeroto 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.
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.
Surith, its
CreationTime, notCreation Time. No space is required..Browser("creationtime:=1")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?
Do all these browsers have exactly the same title? Different titles? Any unique attributes between these?
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
Shshvali,
Thanks for catching the error in this snippet! I’ve already corrected it. I’m using
xin the counter but usingcolBrowser(i)instead ofcolBrowser(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 IfHi Anshoo,
Can we close QTP Process using below statement.
Systemutil.CloseProcessByName “QTPRO.exe”
Thanks,
Raja
It is a really good stuff. Helps beginners like us to learn a lot. Thanks a lot.
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
Rohit: Try and see if this code works:
Browser("title:=Insert_Title_Of_The_Browser_Here").CloseBrowser("index:=0").CloseSet oDesc = Description.Create oDesc.Add "micclass", "Browser" Browser("creationtime:=" & Desktop.ChildObjects(oDesc).Count - 1).CloseHi 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.
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..
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.
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.
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
Ny renu: You can use the following code to click the button:
Dialog("nativeclass:=#32770").WinButton("text:=Close all &tabs").ClickHi
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
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 LoopFinally 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
‘———————————–
Thanks for your help.. :)
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.
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
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 NextSet 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
In this line it is taking much time
Set BrowserCollection = Desktop.ChildObjects(obj)
Is there any solution for this ??
Shalini: What is your version of QTP?
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.
Mr. QA, your tone indicated the web admin of this forum works under you.
Please be reasonable and be polite.
Thanks.
Which part does not work? Can you include the code?