Close all browsers function

Ask general questions here.
Mayra
Posts: 68
Joined: Mon Dec 16, 2013 5:27 am

Close all browsers function

Post by Mayra » Sat May 03, 2014 3:50 am

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);

}

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Close all browsers function

Post by krstcs » Mon May 05, 2014 2:47 pm

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.
Shortcuts usually aren't...

Mayra
Posts: 68
Joined: Mon Dec 16, 2013 5:27 am

Re: Close all browsers function

Post by Mayra » Tue May 06, 2014 2:04 am

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.

mzperix
Posts: 137
Joined: Fri Apr 06, 2012 12:19 pm

Re: Close all browsers function

Post by mzperix » Tue May 06, 2014 10:52 am

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