Friday, 9 August 2013

How to Close All Open Browsers Using QTP

Method 1 -
'Check the existence of a browser and close it
'until no more browsers exist

While Browser("creationtime:=0").Exist(0)

    'Close the browser
    Browser("creationtime:=0").Close

Wend
Limitation – The code does not allow to ignore any specific browser (like Quality Center)
Though there another way by which can enumerate all open browser and close them in QTP and below demonstrates the same

Method 2 -
'Create a description for browser

Set oBrowser = Description.Create
oBrowser("micclass").Value = "Browser"

Set oPage = Description.Create
oPage("micclass").Value = "Page"

'Get all browsers
Set allBrowser = Desktop.ChildObjects(oBrowser)

Dim i, iCount

iCount = allBrowser.Count - 1

For i = 0 To iCount
    'Get the page object from the browser
    Set oPg = allBrowser(i).ChildObjects(oPage)(0)

    'Get the URL of the
    If InStr(oPg.GetROProperty("title"), "Quality Center", vbTextCompare) = 0 Then
        'Close the browser
        allBrowser(i).Close
    End If
Next
Method 3 -
Closing processes using WMI
Another way to close a process is to use Window management instrumentation (WMI)
'Name/IP of the computer
sComp = "."

'Get the WMI object
Set WMI = GetObject("winmgmts:\\" & sComp & "\root\cimv2")

'Get collection of processes for with name iexplore.exe
Set allIE = WMI.ExecQuery("Select * from Win32_Process Where Name = 'iexplore.exe'")

'Loop through each process and terminate it
For Each IE in allIE
    IE.Terminate()
Next

No comments:

Post a Comment