Wednesday 29 January 2014

Different Methods to close all browser in QTP

There are many methods available to close all browser in QTP. Few of them are given below.
Method 1: Using ChildObjects     
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

Method 2: Closing all browser except Quality Control
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 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

No comments:

Post a Comment