browser close icon

Ask general questions here.
rkarhe
Posts: 42
Joined: Thu Jul 28, 2011 5:31 am

browser close icon

Post by rkarhe » Wed Apr 25, 2012 1:18 pm

Hi,

I am trying to access close button on browser. Please see below the xPath '/form[@title~'^my\\ Editor']/titlebar/button[@accessiblename='Close']'. It works through spy but when running through script it is not closing the browser. I need this to be work on IE8, IE9, Firefox and Safari5.x.

Thanks and Regards,
Rocky

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

Re: browser close icon

Post by sdaly » Wed Apr 25, 2012 1:28 pm

An easier way which will work for each browser is to call Close() on a WebDocument object -

Code: Select all

WebDocument myDom = "rxpath to dom";
myDom.Close();

I have noticed that sometimes this fails to close and an exception is thrown, in this case you can just try again and it usually works.

I added this method to my framework and just call myDom.CloseWithRetryOnFail();

Code: Select all

	/// <summary>
		/// Calls Close() but retries (refreshing the webdocument adapter) up to 20 times if this fails.
		/// </summary>
		/// <param name="dom"></param>
		public static void CloseWithRetryOnFail(this WebDocument dom){
			//HACK:  Webdocument.close sometimes fails and throws an exception, try multiple times to close
			for (int i = 0; i < 20; i++) {
				try {
					LoggerGFI.LogInfo("Closing web document attempt " + i.ToString());
					dom.Close();
					break;
				} catch (Exception) {
					Thread.Sleep(1000); //wait 1 sec before re-finding the dom
					dom = Host.Local.FindSingle(dom.GetPath());
					//if this was the 20th attempt throw the exception
					if(i==20){
						throw;
					}
				}
			}
		}

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: browser close icon

Post by omayer » Sat Jun 09, 2012 9:51 pm

Hi Sdaly,
I am trying to use your code snippet but getting following error -

Extension methods must be defined in a non-generic static class (CS1106) -
Cannot define a new extension method because the compiler required type 'System.Runtime.CompilerServices.ExtensionAttribute' cannot be found. Are you missing a reference to System.Core.dll? (CS1110) -
Thank you,
Tipu

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

Re: browser close icon

Post by sdaly » Mon Jun 11, 2012 8:13 am

As the error states, the extension method must be defined within a static class.... Is the class static?

Instead of "public class ExtensionMethods", try "public static class ExtensionMethods"

vnet
Posts: 29
Joined: Tue Jul 24, 2012 8:13 pm

Re: browser close icon

Post by vnet » Tue Aug 21, 2012 11:46 pm

And how would anyone call this method. I added this in my Usercode.cs file. For calling this method (since it's a static method, don't I need a class name in my test when I call it with just method name Ranorex doesn't complain but doesn't do anything either. My question basically is how to class this method and what would be the argument. Appreciate the help.
thanks.

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

Re: browser close icon

Post by Ciege » Wed Aug 22, 2012 12:29 am

vnet wrote:My question basically is how to class this method and what would be the argument. Appreciate the help.
thanks.
Did you mean how to *call* this method?

If you look at the comments in his method you will see what is required. You can also see what is required in the definition of the method.
/// <param name="dom"></param>
public static void CloseWithRetryOnFail(this WebDocument dom){
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...

vnet
Posts: 29
Joined: Tue Jul 24, 2012 8:13 pm

Re: browser close icon

Post by vnet » Wed Aug 22, 2012 1:30 am

Excuse my ignorance. I found out that WebDocument is Ranorex supported API.I used the function and instead of making this an extension method I made it a local method to my recording class. And instead of passing the WebDocument in the function I define it inside the function itself and pass the xpath for the webdocument in the function. So basically looks like this-

public void CloseWithRetryOnFail(string domPath)
{
//HACK: Webdocument.close sometimes fails and throws an exception, try multiple times to close
WebDocument dom = domPath;

for (int i = 0; i < 20; i++) {
try {
Report.Log(ReportLevel.Info,"Closing web document attempt " + i.ToString());
dom.Close();
break;
} catch (Exception) {
Thread.Sleep(1000); //wait 1 sec before re-finding the dom
dom = Host.Local.FindSingle(dom.GetPath());
//if this was the 20th attempt throw the exception
if(i==20){
throw;
}
}
}
}

And in my recording I call the function and specify xpath of my webpage as an argument :)

komeershettyvinod
Posts: 2
Joined: Thu Feb 12, 2015 8:34 am

Re: browser close icon

Post by komeershettyvinod » Thu Feb 12, 2015 1:05 pm

I've got the same exception when trying to close the browser in ie-11.. can any one please suggest

Thanks,
Vinod