Below Code shows how to close all browsers except browser based on the title of the browser in UFT / QTP.
Function CloseAllOpenbrowsersExcept(browsertitle) Set oBrowser = Description.Create oBrowser("micclass").Value = "Browser" ''Get all browsers open Set oBrowserLst= Desktop.ChildObjects(oBrowser) For i=0 to oBrowserLst.count-1 'Verify the title of the browser and close the browser if it does not the title passed 'in the function If InStr(oBrowserLst(i).GetROProperty("title"), browsertitle) = 0 Then oBrowserLst(i).close End If Next Set oBrowser = nothing Set oBrowser = nothing End Function
Similarly we can tweak the above code to close only a particular browser as shown below:
Function CloseOpenbrowserWithTitle(browsertitle) Set oBrowser = Description.Create oBrowser("micclass").Value = "Browser" ''Get all browsers open Set oBrowserLst= Desktop.ChildObjects(oBrowser) For i=0 to oBrowserLst.count-1 'Verify the title of the browser and close the browser if it does not the title passed 'in the function If InStr(oBrowserLst(i).GetROProperty("title"), browsertitle) <> 0 Then oBrowserLst(i).close End If Next Set oBrowser = nothing Set oBrowser = nothing End Function
Similarly we can close all browsers by removing the condition to check for title in the above code.
Function CloseAllOpenbrowsers() Set oBrowser = Description.Create oBrowser("micclass").Value = "Browser" ''Get all browsers open Set oBrowserLst= Desktop.ChildObjects(oBrowser) For i=0 to oBrowserLst.count-1 'Verify the title of the browser and close the browser if it does not the title passed 'in the function oBrowserLst(i).close Next Set oBrowser = nothing Set oBrowser = nothing End Function