How to find and close web browser?

Ranorex Spy, Recorder, and Studio.
MikeBuckley
Posts: 5
Joined: Fri Sep 18, 2009 8:52 pm

How to find and close web browser?

Post by MikeBuckley » Mon Sep 28, 2009 9:26 pm

I am trying to figure this one out, and have not had any success. Please be patient with this n00b!

I want to check to see if a browser is running, and if it is, dismiss it, then restart the browser to begin testing. Now in my past life, I could do a "if Browser.Exists()... and do what I need to do from there. Now I probably am missing something very simple, but any help is appreciated.

Thanks,

Mike Buckley

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: How to find and close web browser?

Post by Ciege » Mon Sep 28, 2009 11:54 pm

Try something like this:

Code: Select all

HDWebForm = Host.Local.FindSingle<Ranorex.WebDocument>("/dom[@caption='" + DOMTitle + "']");
or

Code: Select all

HDWebForm = Host.Local.FindSingle<Ranorex.WebDocument>("/dom[@pageurl='" + DOMURL + "']");
Then you can do a:

Code: Select all

HDWebForm.Close();
The difference between the two is one looks for a web browser based on it's title the other looks for a web browser based on it's current URL.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

MikeBuckley
Posts: 5
Joined: Fri Sep 18, 2009 8:52 pm

Re: How to find and close web browser?

Post by MikeBuckley » Tue Sep 29, 2009 3:11 pm

This did the trick! Thank you very much

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: How to find and close web browser?

Post by Ciege » Tue Sep 29, 2009 4:51 pm

No problem. Now go forth and test! 8)
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

Anu
Posts: 13
Joined: Tue May 10, 2011 5:10 pm

Re: How to find and close web browser?

Post by Anu » Tue May 17, 2011 4:01 pm

If I need to close all open browsers, irrespective of the url or domain then what can I do?

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: How to find and close web browser?

Post by Ciege » Tue May 17, 2011 4:04 pm

Like in your other post about finding links in a frame... Instead, get an IList of all browser objects, then use a foreach to loop through each browser object and close.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

Anu
Posts: 13
Joined: Tue May 10, 2011 5:10 pm

Re: How to find and close web browser?

Post by Anu » Tue May 17, 2011 4:41 pm

Thank you Ciege, but to search based on a <Ilist> I need to provide a webDocument with domain information. When there is a browser open, then it works fine. I have it implemented it already.

The problem is when there is no browser open then Ranorex throws an error. "NO element found for path 'dom....' within 10s.

Appreciate your patience with this newbie. I come from QTP world, so figuring this out and C# is all new to me.

So this is what I tried.

Code: Select all

HDWebForm = Host.Local.FindSingle<Ranorex.WebDocument>("/dom[@caption='" + DOMTitle + "']");
HDWebForm.Close();
and

Code: Select all

WebDocument webdocument = "/dom/[domain ='" + url + "']";
Webdocument.Close();
Both throw an error that there is no such object open.

Thanks
Anu

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: How to find and close web browser?

Post by Ciege » Tue May 17, 2011 4:57 pm

I don't get it... You say:
I need to close all open browsers
Then you say:
The problem is when there is no browser open then Ranorex throws an error

Well... If there is no open browser... then why do you need to close all open browsers? In other words, they are already closed.... Or am I missing something?
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

Anu
Posts: 13
Joined: Tue May 10, 2011 5:10 pm

Re: How to find and close web browser?

Post by Anu » Tue May 17, 2011 5:17 pm

Yes, So when i execute my script I want my script to be smart enough to close all open browsers(irrespective of name) first and then open a new browser and work on it.

If there is any existing browser Ranorex opens a new tab and then gets confused with multiple browsers open.

Thanks for your reply.
Anu

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: How to find and close web browser?

Post by Ciege » Tue May 17, 2011 5:41 pm

OK, first, you will want to enclose your Find in a try/catch block. That way you can catch the exception when no more browser objects are found.

Here is code that will make an IList of all WebDocuments (Browsers) that are children of the desktop:

Code: Select all

IList<Ranorex.WebDocument> AllDOMs = Host.Local.FindChildren<Ranorex.WebDocument>();
Now you can loop that list and close any that exist.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

Anu
Posts: 13
Joined: Tue May 10, 2011 5:10 pm

Re: How to find and close web browser?

Post by Anu » Tue May 17, 2011 6:12 pm

Perfect, I will try this and let you know.

Thanks
Anu
(Still a newbie)

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: How to find and close web browser?

Post by Ciege » Tue May 17, 2011 6:14 pm

Anu wrote:(Still a newbie)
Don't worry, we were all there once... :D
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

Anu
Posts: 13
Joined: Tue May 10, 2011 5:10 pm

Re: How to find and close web browser?

Post by Anu » Wed May 18, 2011 9:41 pm

Thanks Ciege - It works like magic although both the code below do the trick. not sure which one is better than the other.

Code: Select all

IList<Ranorex.WebDocument> AllDoms = Host.Local.FindDescendants<Ranorex.WebDocument>();
if (AllDoms.Count >=1)
{
  foreach (WebDocument myDom in AllDoms)
  {
     myDom.Close();
  }				
}
or

Code: Select all

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

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: How to find and close web browser?

Post by Ciege » Wed May 18, 2011 9:57 pm

#2 is better for *most* instances.
FindChildren will look only for browser objects that are children of the desktop (most likely scenario). It will only search the desktops children then stop.

FindDescendants will look for any browser object that are descendants of the desktop (i.e. children of children of children... etc...). In other words it will search any and every open application that it can find, then search each possible path of each application. This can be very slow and time consuming if you happen to have any other applications open at the time if your search. I would only use this method if I knew there was a possibility that a DOM could be open within an application that I needed to close, but I cannot think of any time that would be the case.

If you want both you could write an overload for your CloseBrowser method that defaults to using FindChildren and can be switched to use FindDescendants if needed.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...