This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Close all Browsers before running a test


Looking for a way to close all instances of Edge and Chrome before a test starts. Just in case a previous run failed etc and left the browser open as the next test run might want to use chrome but edge is already open and set to use existing browser it will use edge


  • I find it a good idea / good practice to have a test primer function that allows you to start from a clean state.   our current FW/Library has a function called FreshStart that iterates through known applications we test, and does a force close on them.

    I use SystemUtil.CloseProcessByName and pass in values like chrome.exe, firefox.exe, <your application executable.exe>

    We really never know the state a prior test leaves a lab machine in, so I find this sets our tests up for success.

    WARNING - If you leverage the record and run / browser launch - this could close that browser :(     

  • I used SystemUtil.CloseProcessByName and it works great however I find sometimes after it closes the browsers, the next step to launch the browser fails to run (perhaps a timing issue)

    I found another script yesterday which I was using to close system processes and it also had the same issue but I put a Wait 10 after it ran and that seemed to solve the issue. I could put a wait after the SystemUtil.CloseProcessByName, however that will always wait 10 seconds even if no browsers were found to be open

    my script only does a wait if it finds there was a browser open so only sleeps 10 seconds after they were closed but if no browsers were found to be open, it doesnt sleep

    Is there anyway with SystemUtil.CloseProcessByName to check if any browsers were open and then only sleep if so like my script does? Or stop the browser from failing to load after this process runs 

    Here are my scripts to close edge and chrome and only wait 10 if any browser was found to be open:

    Public Function closeBrowserAtStartup(ByVal browserToClose)
    strSQL = "Select * From Win32_Process Where Name = '" & browserToClose & ".exe'"
    Set oWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set ProcColl = oWMIService.ExecQuery(strSQL)
    For Each oElem in ProcColl
    Set oWMIService = Nothing
    closeBrowserAtStartup = ProcColl.Count
    End Function

    Public Sub closeBrowsersAtStartup()
    browsersWereOpen = False
    numberOfBrowserInstancesEdge = closeBrowserAtStartup("msedge")
    If numberOfBrowserInstancesEdge > 0 Then
    browsersWereOpen = True
    End If
    numberOfBrowserInstancesChrome = closeBrowserAtStartup("chrome")
    If numberOfBrowserInstancesChrome > 0 Then
    browsersWereOpen = True
    End If
    If browsersWereOpen = True Then
    Wait 10
    End If
    End Sub

    so basically it queries the running processes for instances of edge.exe or chrome.exe. First calls to close Edge and if instances were greater than 0 set a true flag, repeat for chrome.

    If it was ever found true a browser was open, wait 10 seconds

    This seems to work well because I have to call my browser via systemUtil because I need to pass in which browser to run, and to run in incognito mode and ignore SSL

    systemutil.Run "msedge.exe""-InPrivate --start-maximized --allow-insecure-localhost" or

    systemutil.Run "chrome.exe""-incognito --start-maximized --allow-insecure-localhost"

    so these lines are not being triggered most of the time if I dont do wait. 

    If there is a way to use SystemUtil.CloseProcessByName and ensure the browsers dont fail to open in the next step that would be great



  • I think the real answer is browser open vs. browser process still running.    We had this issue with Firefox as for reasons unknown, after 'closing' the process lingered a while.     I think the unfortunate 'hack' we did was to start our test by saying 'close any open browsers/apps', then do our pre-req data fetch/setup (which takes a little bit of time), then start our test with launching the browser.    This added a pause of sorts between close/open of browser - which it seems like you are seeing.

    I wonder if you were to poll the process list with the nice function you have, almost like you would poll for an object being present or not present on a webpage.

    Hope it works / good luck!