Page 1 of 1

Close all browsers function

Posted: Sat May 03, 2014 3:50 am
by Mayra
I am using the following code in Ranorex 5, to close all opened browsers. Unfortunately, this does not do the job from time to time. It is one of the many inconsistent behaviors I am experiencing with Ranorex. Is there a better way to do this? Thanks.

public static void closeAllOpenBrowsers(){
IList <Ranorex.WebDocument> AllDoms = Host.Local.FindChildren<Ranorex.WebDocument>();
if (AllDoms.Count >=1)
{
foreach (WebDocument myDom in AllDoms)
{
myDom.Close();
}
}


Delay.Seconds(20);

}

Re: Close all browsers function

Posted: Mon May 05, 2014 2:47 pm
by krstcs
I don't see anything wrong with it, per se.

What browsers (and versions) are you using? Which ones are having this issue? Are you using the browsers in "private" mode ("incognito" for Chrome)? (I've had issues with Chrome not closing web pages, especially in "incognito" mode.)

Which version of Ranorex (specifically) are you using?



On a coding note, you don't need the "if (AllDoms.Count >=1) {...}" statement. The foreach() loop already has that logic built in. If there are no webdocument objects in the list, the foreach() will just drop out and go on without executing any of the commands. If you were using a for() loop, you might want the if, but not with the foreach(). Good coding practices suggest that you shouldn't use redundant logic unless there is some reason for it, just one more thing the CPU has to do, that is doesn't need to. :D

This shouldn't impact the issue you are raising though.

Re: Close all browsers function

Posted: Tue May 06, 2014 2:04 am
by Mayra
Hi,
Im not using Chrome in incognito mode, version of Chrome version 34.
It also happens in Firefox, more so than in Chrome. Firefox, I am using version 26.
For Ranorex version 5.
I guess I can go ahead and kill the process. However the problem is beyond closing the browsers, because if I want to use the browser instance that is already up, Ranorex does not detect it and then I end up with two tabs opened for my application.
Thanks.

Re: Close all browsers function

Posted: Tue May 06, 2014 10:52 am
by mzperix
Hi Mayra,

You should try out sdaly's solution: http://www.ranorex.com/forum/browser-cl ... tml#p13606

Basically it tries to close the browser again until there are no open instances of the browsers. (It tries 20 times before throwing exception.)

Best Regards,
Zoltan