Forum Discussion

marinb's avatar
marinb
Contributor
7 years ago

Browser exists, yet it does not. How is that possible?

To properly kill any process before starting my tests, I've added this bit of code (part of a bigger loop):

 

browser = Sys.WaitProcess(eachBrowser, 1000);
if (browser.exists) {
    logMessage.debug('    - Browser exists. Trying to terminate...');
    try {
        Sys.Process(eachBrowser).Terminate();   
    } catch (err) {
        logMessage.debug('Error on terminating [' + eachBrowser + ']: ' + err.message);
    }
}

This results in this log:

 

How can this happen? It does not exist, so it should not move into the if clause. Furthermore, it generates an error, but it's in a try/catch. I thought try/catch mechanisms are specifically made to prevent errors from being written to the log because you intercept them in the catch?

 

I find it hard to believe that the process stopped to exist right in the same millisecond the code went into the exist clause.

 

Are there easier/more trustworthy methods to kill your browser? The Browser.Close is just too shaky since it does not properly close pages if a browser process runs in the background for some reason (internet explorer has a way of running itself in the background without the browser actually being opened).

 

 

  • tristaanogre's avatar
    tristaanogre
    7 years ago

    We do all our testing in IE so our code is IE centric... however, this is what we're doing:

    function closeIEInstance() {
        var counter = 0;
        var browser;
        browser = Sys.WaitBrowser('iexplore', 1000);
        while((browser.Exists) && (counter < 60)){
            counter++;
            browser.Terminate();
            browser = Sys.WaitBrowser('iexplore', 1000);
        }  

    Note that, instead of process, we're using "WaitBrowser".  See if this works better for you.

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    If you're using JScript or JavaScript, it is case sensitive.  Make sure you're checking "Exists" not "exists". If that doesn't fix it, let us know.

     

    As for try/catch... they don't catch object exists errors because they are not script coding errors.  What I would do, though, is use browser.Terminate() in your try block to make sure, if it DOES exist, that you actually are closing the instance of chrome found initially in your WaitProcess.

    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      We do all our testing in IE so our code is IE centric... however, this is what we're doing:

      function closeIEInstance() {
          var counter = 0;
          var browser;
          browser = Sys.WaitBrowser('iexplore', 1000);
          while((browser.Exists) && (counter < 60)){
              counter++;
              browser.Terminate();
              browser = Sys.WaitBrowser('iexplore', 1000);
          }  

      Note that, instead of process, we're using "WaitBrowser".  See if this works better for you.

      • marinb's avatar
        marinb
        Contributor

        That code looks a bit neater than ours. I will try it that way as well.

        I stopped using waitBrowser because waitBrowser does not recognize iexplore processes running without an actual page interface. Too many times, this code:

        browser = waitBrowser(browserName, 1000);
        if (!browser.Exists) { 
            Log.Message ('Browser does not exist, I am going to start it...');
            Browsers.Item(browserName).Run(url)
        }

        lead to log:

        Browser exists, I am going to start it

        Browser already running!

         

         

        said 'there is no browser running, I am going to start one...'

        and then 'Browser already running' when doing

    • marinb's avatar
      marinb
      Contributor

      Well, if there was a browser, it also went into the browser.exists clause, so exists or Exists apparently doesn't make a difference.

       

      I could change the process.Terminate to browser.Terminate, but that as well shouldn't make a difference I guess? If there are 3 browsers open and he finds one but kills another, he simply loops till they are all gone. (around the code snippet, there is another loop)