Wednesday, 10 September 2014

QTP - Code to close all browsers

'// This will close all IE browsers
Public Function closeAllBrowsers
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 = 0 to Desktop.ChildObjects(oDesc).Count - 1
Browser( "creationtime:=0").Close
Next
End If
End Function


the logic behind this code is, every time a new browser is opened the creation time of the browser is incremented by one.
Generally many write it as 
Browser( "creationtime:=" &x).Close
however that doesn't work. when browser with creation time 0 is closed, automatically browser with creation time 1 will be changed as browser with creation time 0 and 2 as 1 and so on...

The below line works fine,
Browser( "creationtime:=" &x).Close
if you are using for loop with x as count-1 and step -1

No comments:

Post a Comment