Page 1 of 1

browser close icon

Posted: Wed Apr 25, 2012 1:18 pm
by rkarhe
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

Re: browser close icon

Posted: Wed Apr 25, 2012 1:28 pm
by sdaly
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;
					}
				}
			}
		}

Re: browser close icon

Posted: Sat Jun 09, 2012 9:51 pm
by omayer
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,

Re: browser close icon

Posted: Mon Jun 11, 2012 8:13 am
by sdaly
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"

Re: browser close icon

Posted: Tue Aug 21, 2012 11:46 pm
by vnet
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.

Re: browser close icon

Posted: Wed Aug 22, 2012 12:29 am
by Ciege
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){

Re: browser close icon

Posted: Wed Aug 22, 2012 1:30 am
by vnet
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 :)

Re: browser close icon

Posted: Thu Feb 12, 2015 1:05 pm
by komeershettyvinod
I've got the same exception when trying to close the browser in ie-11.. can any one please suggest

Thanks,
Vinod