How to find and close web browser? : Automation Tools

How to find and close web browser?

Ranorex Spy, Recorder, and Studio.

How to find and close web browser?

Postby MikeBuckley » Mon Sep 28, 2009 10: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
MikeBuckley
 
Posts: 5
Joined: Fri Sep 18, 2009 9:52 pm

Re: How to find and close web browser?

Postby Ciege » Tue Sep 29, 2009 12:54 am

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...
User avatar
Ciege
 
Posts: 965
Joined: Thu Oct 16, 2008 7:46 pm
Location: Arizona, USA

Re: How to find and close web browser?

Postby MikeBuckley » Tue Sep 29, 2009 4:11 pm

This did the trick! Thank you very much
MikeBuckley
 
Posts: 5
Joined: Fri Sep 18, 2009 9:52 pm

Re: How to find and close web browser?

Postby Ciege » Tue Sep 29, 2009 5: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...
User avatar
Ciege
 
Posts: 965
Joined: Thu Oct 16, 2008 7:46 pm
Location: Arizona, USA

Re: How to find and close web browser?

Postby Anu » Tue May 17, 2011 5:01 pm

If I need to close all open browsers, irrespective of the url or domain then what can I do?
Anu
 
Posts: 13
Joined: Tue May 10, 2011 6:10 pm

Re: How to find and close web browser?

Postby Ciege » Tue May 17, 2011 5: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...
User avatar
Ciege
 
Posts: 965
Joined: Thu Oct 16, 2008 7:46 pm
Location: Arizona, USA

Re: How to find and close web browser?

Postby Anu » Tue May 17, 2011 5: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
Anu
 
Posts: 13
Joined: Tue May 10, 2011 6:10 pm

Re: How to find and close web browser?

Postby Ciege » Tue May 17, 2011 5: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...
User avatar
Ciege
 
Posts: 965
Joined: Thu Oct 16, 2008 7:46 pm
Location: Arizona, USA

Re: How to find and close web browser?

Postby Anu » Tue May 17, 2011 6: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
Anu
 
Posts: 13
Joined: Tue May 10, 2011 6:10 pm

Re: How to find and close web browser?

Postby Ciege » Tue May 17, 2011 6: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...
User avatar
Ciege
 
Posts: 965
Joined: Thu Oct 16, 2008 7:46 pm
Location: Arizona, USA

Re: How to find and close web browser?

Postby Anu » Tue May 17, 2011 7:12 pm

Perfect, I will try this and let you know.

Thanks
Anu
(Still a newbie)
Anu
 
Posts: 13
Joined: Tue May 10, 2011 6:10 pm

Re: How to find and close web browser?

Postby Ciege » Tue May 17, 2011 7: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...
User avatar
Ciege
 
Posts: 965
Joined: Thu Oct 16, 2008 7:46 pm
Location: Arizona, USA

Re: How to find and close web browser?

Postby Anu » Wed May 18, 2011 10: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();
  }            
}
Anu
 
Posts: 13
Joined: Tue May 10, 2011 6:10 pm

Re: How to find and close web browser?

Postby Ciege » Wed May 18, 2011 10: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...
User avatar
Ciege
 
Posts: 965
Joined: Thu Oct 16, 2008 7:46 pm
Location: Arizona, USA


Return to Automation Tools

Who is online

Users browsing this forum: No registered users and 0 guests