Forum Discussion

yury_mokropulo's avatar
yury_mokropulo
New Contributor
13 years ago

How to close ALL browsers?

Hello!

I need to close all browsers (if any) before new test begins.

I wrote simple function:



function CheckBrowser()

{

   var page = Sys.Browser("*").Page("*");

   if(page) Sys.Browser("*").Close();

}



Obviously, it closes only one first browser from Object Browser list.

I tryied to put it in cycle:



function CheckBrowser()

{

  var arr = Array("iexplore", "firefox", "chrome");

  for(var i in arr)

  {

     var page = Sys.Browser( arr i ).Page("*");

     if(page)

     {

       Log.Message("Browser '" + arr i + "' is open. Closing browser");

       Sys.Browser( arr i ).Close();

     }

   }

}



//NOTE: there must be square brackets in arr i



But it needs all browsers from array list to be open, otherwise error appears:

"Unable to find the object Browser("firefox")"



Is there any way to find and close all browsers?

Thanks.

2 Replies

  • This one works:



    function CheckBrowser()

    {

      var PropArray, ValuesArray;

     

      // closing IE

       PropNames = new Array("processname", "index");

       PropValues = new Array("iexplore", 1);



       var page = Sys.FindChild(PropNames, PropValues, 1);

       if(page.Exists)  page.Close();





    // closing Chrome

      PropNames = new Array("processname", "index");

      PropValues = new Array("chrome", 1);



      var page = Sys.FindChild(PropNames, PropValues, 1);

      if(page.Exists)  page.Close();





    // add browser...



    }

  • Hi Yuri,


     


    Here is another script:


    function closeAllBrowsers()


    {


      var browserNames = new Array("iexplore", "firefox", "chrome");


      for(var i in browserNames)


      {


        closeBrowser(browserNames);


      }


    }


     


    function closeBrowser(BrowserNamer)


    {


      if (Sys.WaitBrowser(BrowserNamer).Exists)


      {


        Log.Message("Browser '" + BrowserNamer + "' is open. Closing the browser");


        Sys.Browser(BrowserNamer).Close();


        


      }


    }


     


    Also, you can iterate though all Browser objects in the object tree and close them:


    function closeAllBrowsers_2()


    {


      var browsers = Sys.FindAll("ObjectType", "Browser", 1);


      browsers = (new VBArray(browsers)).toArray();


      for(var i in browsers)


      {


        Log.Message("Browser '" + browsers.ProcessName + "' is open. Closing the browser");


        browsers.Close();


      }


    }