Forum Discussion

dhundley's avatar
dhundley
Regular Contributor
2 years ago

closing chrome using sys.waitbrowser, close and/or terminate

Is there something wrong with the following code?

if (Sys.WaitBrowser("chrome").Exists)
{
Log.Message("chrome");
do
{
Log.Message("inside loop");
objBrowser = Sys.Browser("chrome");
objBrowser.Close();
Delay(5000);
if (Sys.WaitBrowser("chrome").Exists)
{
Log.Message("close didn't work - terminating chrome browser");
objBrowser.Terminate();
}
Delay(5000);
}
while (Sys.WaitBrowser("chrome").Exists);
}

 

I have this in my onstarttestcase event handler to ensure that there are no instances of chrome running in the system before i launch chrome for the purposes of my test case. Task Manager shows three copies are running

 

 

 

and, even though i can see the log message (from the script above) appear in the results, it does nothing to close or terminate those three copies. just to satisfy my curiosity, i copied, pasted and ran the following script from the smartbear support website https://support.smartbear.com/testcomplete/docs/tutorials/samples/desktop/process-list.html in order to list all the processes currently running

 

function TestProc()
{
var p, i, s;
for (i = 0; i < Sys.ChildCount; i++)
{
p = Sys.Child(i);
s = "Process Name: " + "\r\n" + p.ProcessName + "\r\n" +
"\r\n" + "Process Path: " + "\r\n" + p.Path;
Log.Message(p.Name, s);
}
}

 

and down in the list, sure enough, are at least two of the three instances of chrome

the only thing i can think of is that the sample piece of code is looking for processes while mine is utilizing the waitbrowser method but since we're talking about Chrome, shouldn't that work?

  • Here's a simple version, where browser is either "chrome" or "edge"

    // Close browser
    while (Sys.WaitBrowser(browser).Exists) {
       Sys.WaitBrowser(browser).Close();
    }
    

     

  • dhundley's avatar
    dhundley
    Regular Contributor

    by adding the sample code to my event handler and modifying its Log.Message line to this

     

    if (p.ProcessName === "chrome")
    {
    Log.Message(p.Name, s);
    p.Terminate();
    }

     

    it does now successfully kill all instances of Chrome. just beats me why my original script didn't do likewise. If anyone knows, please enlighten me. thanks!

     

     

     

     

     

     

     

     

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Here's a simple version, where browser is either "chrome" or "edge"

    // Close browser
    while (Sys.WaitBrowser(browser).Exists) {
       Sys.WaitBrowser(browser).Close();
    }