How to close browser?

Class library usage, coding and language questions.
yinghu
Posts: 26
Joined: Thu Oct 25, 2012 5:48 pm

How to close browser?

Post by yinghu » Fri Feb 01, 2013 8:22 pm

Hi folks,

Long time no see.:)

I am doing automation test using Ranorex APIs. Now I have a problem about closing browser windows.

Here attachment is the screenshot I got when I ran my test in Visual Studio.

Thanks alot in advance!

yinghu
Attachments
Untitled.png
Untitled.png (12.16 KiB) Viewed 5013 times

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

Re: How to close browser?

Post by Ciege » Fri Feb 01, 2013 8:31 pm

How about just use

Code: Select all

wd.close
.
You should probably put it in a try/catch block to catch and handle any exceptions just in case.
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...

yinghu
Posts: 26
Joined: Thu Oct 25, 2012 5:48 pm

Re: How to close browser?

Post by yinghu » Fri Feb 01, 2013 8:41 pm

Thanks for your quick reply and mentioning try/catch thing!

I just tried "wd.Close();" and it failed same as the previous screenshot I uploaded.

Ciege wrote:How about just use

Code: Select all

wd.close
.
You should probably put it in a try/catch block to catch and handle any exceptions just in case.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: How to close browser?

Post by Support Team » Mon Feb 04, 2013 6:22 pm

Hello,

Which version of Ranorex do are you using?
Here is a little code snippet which works as expected.
Host.Local.OpenBrowser("http://www.ranorex.com", "IE");
Delay.Seconds(3);
Ranorex.WebDocument wd = Host.Local.FindSingle<Ranorex.WebDocument>("/dom[@domain='www.ranorex.com' and @caption='Test Automation Tools - Ranorex Automation Framework']");
Validate.Exists(wd.GetPath());
//Host.Local.CloseApplication(wd, new Duration(0));
wd.Close();
You can use "CloseApplication" or "Close". Both of them work.
Which domain do you use to open the browser? It could be that you use e.g. http://www.yyy.com to open the browser and yyy.com to close it. Please check the RxPath of the element.

Regards,
Bernhard
.
Image

yinghu
Posts: 26
Joined: Thu Oct 25, 2012 5:48 pm

Re: How to close browser?

Post by yinghu » Mon Feb 04, 2013 10:23 pm

I tried all the things you mentioned. There is no luck for me. I still got the same exception as before.

My Ranorex's version is 4.0.



Support Team wrote:Hello,

Which version of Ranorex do are you using?
Here is a little code snippet which works as expected.
Host.Local.OpenBrowser("http://www.ranorex.com", "IE");
Delay.Seconds(3);
Ranorex.WebDocument wd = Host.Local.FindSingle<Ranorex.WebDocument>("/dom[@domain='www.ranorex.com' and @caption='Test Automation Tools - Ranorex Automation Framework']");
Validate.Exists(wd.GetPath());
//Host.Local.CloseApplication(wd, new Duration(0));
wd.Close();
You can use "CloseApplication" or "Close". Both of them work.
Which domain do you use to open the browser? It could be that you use e.g. http://www.yyy.com to open the browser and yyy.com to close it. Please check the RxPath of the element.

Regards,
Bernhard

User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

Re: How to close browser?

Post by sdaly » Tue Feb 05, 2013 9:51 am


yinghu
Posts: 26
Joined: Thu Oct 25, 2012 5:48 pm

Re: How to close browser?

Post by yinghu » Wed Feb 06, 2013 5:47 pm

Thanks alot for your link!

Unfortunately, it still can't close the browser.
sdaly wrote:Could be similar to this - http://www.ranorex.com/forum/browser-cl ... t3306.html

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: How to close browser?

Post by Support Team » Thu Feb 07, 2013 5:47 pm

Hello,

Could you please explain exactly what you are doing in your code?
Additionally please check with Ranorex Spy if the path to the web document you are trying to close is correct.
Thank you!

Regards,
Bernhard
.
Image

yinghu
Posts: 26
Joined: Thu Oct 25, 2012 5:48 pm

Re: How to close browser?

Post by yinghu » Thu Feb 07, 2013 7:30 pm

Actually, I track the web document path by Spy, it does exist. And this "browser" is a POPUP window which opened by javascript basically like:


<script type="text/javascript">function LoadSiteManager(){
if (GetCookie('UserID') == '') {
SetCookie('RedirectTo', '../Section/Section.aspx?DomainId=1090');
window.open("http://test.auto.schoolwires.com/defaul ... 7&SiteID=1", "sitemanager2", "status=1, toolbar=0, location=0, menubar=0, width=" + (screen.width < 1025 ? (screen.width - 5) : 1200) + ", height=" + (screen.height - 50) + ", top=0, left=0, resizable=1, scrollbars=1");
} else {
window.open("../Section/Section.aspx?DomainId=1090", "sitemanager2", "status=1, toolbar=0, location=0, menubar=0, width=" + (screen.width < 1025 ? (screen.width - 5) : 1200) + ", height=" + (screen.height - 50) + ", top=0, left=0, resizable=1, scrollbars=1");
}
}
</script>

Thanks!

Support Team wrote:Hello,

Could you please explain exactly what you are doing in your code?
Additionally please check with Ranorex Spy if the path to the web document you are trying to close is correct.
Thank you!

Regards,
Bernhard

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: How to close browser?

Post by Support Team » Thu Feb 07, 2013 8:06 pm

Could you also post the complete exception message (text + stacktrace) that you get when you call Close on the window? In the screenshot you posted, you can only see the first line of the exception text.
Thank you!

Regards,
Alex
Ranorex Team
.
Image

Dinin
Posts: 5
Joined: Tue Jan 14, 2014 4:46 pm

Re: How to close browser?

Post by Dinin » Mon Jan 20, 2014 1:38 pm

Hi Alex,

I also met with this problem. If you open a browser window from another browser - try to click any link with press CTRL button - and if it found by RR, the close() method will fail and the workarounds - mentioned here - also won't work.

Thanks

Laszlo

Dinin
Posts: 5
Joined: Tue Jan 14, 2014 4:46 pm

Re: How to close browser?

Post by Dinin » Mon Jan 20, 2014 5:08 pm

Hello,

I have now a quick and dirty solution which work in my case (browser window opened from another browser with CTRL+click a link). Before this I tried to call the Close method of the WebDocument.

Please let me know your experience about this:

Code: Select all

                Form f  = null;
                if (this.windowTestObject.GetType() == typeof(Ranorex.WebDocument))
                {
                    f = ((Ranorex.WebDocument)this.windowTestObject).Browser;
                } else if (windowTestObject.GetType() == typeof(Ranorex.Form)){
                    f = (Form)this.windowTestObject;
                }
                    PopupWatcher closePopupWatcher = new PopupWatcher();
                    closePopupWatcher.Watch(f.GetPath(), CloseUpdateCheckDialog);
                    closePopupWatcher.Start();
                    f.Close();
            }
....

        //Popup Closer
        public static void CloseUpdateCheckDialog(Ranorex.Core.RxPath myPath, Ranorex.Core.Element parentElement)
        {
            int processId = ((Ranorex.NativeWindow)parentElement).ProcessId;
            Process proc = Process.GetProcessById(processId);
            proc.Kill();            
        } 


User avatar
rprehm
Posts: 24
Joined: Tue Dec 17, 2013 6:43 pm

Re: How to close browser?

Post by rprehm » Wed Jan 22, 2014 5:23 pm

Hello Dinin,

Please make sure that you have tracked the proper DOM object. The solution from “sdaly” (browser close icon) works brilliant in all of my cases.

Try to implement “CloseWithRetryOnFail()” within your project. That will resolve your issue. Many thanks again to “sdaly”.

Regards,
Robert