Closing and killing browsers

Class library usage, coding and language questions.
JSH_QA
Posts: 56
Joined: Thu Apr 05, 2012 9:03 am

Closing and killing browsers

Post by JSH_QA » Wed Jul 09, 2014 12:57 pm

The online API documentation for the "OpenBrowser Method (url, browserName)" says of the browserName argument 'The name of the browser to open. Can either be "firefox", "ie", "chrome" or "safari".'

The online API documentation for "KillBrowser Method (browserName)" says of the browserName argument 'The name of the browser to close. Can either be "firefox" or "ie".'

Could you confirm please that:
(1) the KillBrowser method can safely be used to kill any browser opened using OpenBrowser.
(2) The "CloseApplication Method (containedElement, gracePeriod)" can safely be used to close or kill any browser opened using OpenBrowser.

As the process id returned from OpenBrowser cannot safely be used when more than one browser of the same type is open, I am having to find a reliable way of closing/killing open browsers that does not rely on the process id.

Many thanks,

John H.

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

Re: Closing and killing browsers

Post by mzperix » Wed Jul 09, 2014 2:27 pm

Hi, you could try to close with the dom object. Ranorex abstracts from the browsers itself by creating a dom. In theory this dom should be the same if opened in different browsers.

So if you open the same page in FF, Chrome and IE, you will see 3 forms in spy - 1 for each browser type, and the browser specific informations should be there. And also 3 dom objects, which SHOULD look the same.

So I would create a list of the open doms with the URL, then close them one-by-one.

Here is a code snippet for closing every dom with the given domain:

Code: Select all

        	
IList<Ranorex.WebDocument> webList = Host.Local.Find<Ranorex.WebDocument>("/dom[@dom='yoursite']");
			
foreach (Ranorex.WebDocument webdoc in webList)
{  
    webdoc.Close();
}
if you want to look for a specific URL, just change the @dom attribute to @URL attribute, or anything what you can describe with ranorexpath :)

Best Regards,
Zoltan

JSH_QA
Posts: 56
Joined: Thu Apr 05, 2012 9:03 am

Re: Closing and killing browsers

Post by JSH_QA » Wed Jul 09, 2014 4:00 pm

Thanks Zoltan,

I do have something similar in place currently (see below), but it seems a little strange having to do this, rather than using an API (CloseApplication or CloseApplications) that appears to be intended for this purpose. I was hoping to remove this code if the named APIs work reliably for all browser types.

IList<Form> forms = Host.Local.Find<Form>(String.Format("/form[@processname='{0}']", BrowserManager.GetProcessName(browserType)));
foreach (Form f in forms)
{
f.Close();
}

I do wrap this in exception handling and follow up with Host.Local.KillBrowser in the expectation that this will kill off any unresponsive browser instances, although it is not entirely clear from the documentation what "KillBrowser" actually does. I am assuming that it uses an operating system call to kill the process (like End Task in Task Manager), but don't know that for sure. Wanting to use KillBrowser to tidy up when other things fail is the reason for asking about which browser types it works with.

Thanks again,

John H.

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

Re: Closing and killing browsers

Post by krstcs » Thu Jul 10, 2014 4:04 pm

Typically you should use the DOM object to close a browser window so the system and application (browser) can do any cleanup that is needed.

The kill command actually forcefully terminates the process, and therefore does not allow it to clean up. With browsers this can cause issues where the next time the browser is opened it tries to re-open the last session's tabs and links.

The kill command should be used sparingly and only when no other method will work.
Shortcuts usually aren't...

JSH_QA
Posts: 56
Joined: Thu Apr 05, 2012 9:03 am

Re: Closing and killing browsers

Post by JSH_QA » Fri Jul 11, 2014 9:13 am

Ok,so KillBrowser forcefully terminated the browser, equivalent to End Task in Task Manager.

(1) Do CloseApplication and CloseApplications attempt a friendly shutdown of an application, equivalent to calling Close on a DOM object? If so, it should presumably be possible to use one or other of these to close browsers nicely?

(2) Going back to my questions in the message at the very start of this thread (as the documentation for OpenBrowser and KillBrowser lists different browsers), can you confirm please that the "CloseApplication Method (containedElement, gracePeriod)" can safely be used to close any browser opened using OpenBrowser? If this is the case, this would mean that I can simplify my code to just use CloseApplication.

Thanks again,

John H.

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

Re: Closing and killing browsers

Post by krstcs » Fri Jul 11, 2014 1:29 pm

Any Browser method that Ranorex has in the API should work with all four (4) browsers that they support (IE, FF, Chrome, Safari on Windows).

If you notice, the method you listed takes a "containedElement" in the call. That element can be ANY element inside the parent, so, to me at least, that means that is should work on any application that Ranorex works on. But... I haven't tried it everywhere. :D
Shortcuts usually aren't...