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
browser close icon
Re: browser close icon
An easier way which will work for each browser is to call Close() on a WebDocument object -
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
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
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,
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
Re: browser close icon
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"
Instead of "public class ExtensionMethods", try "public static class ExtensionMethods"
Re: browser close icon
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.
thanks.
Re: browser close icon
Did you mean how to *call* this method?vnet wrote:My question basically is how to class this method and what would be the argument. Appreciate the help.
thanks.
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...
Ciege...
Re: browser close icon
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
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

-
- Posts: 2
- Joined: Thu Feb 12, 2015 8:34 am
Re: browser close icon
I've got the same exception when trying to close the browser in ie-11.. can any one please suggest
Thanks,
Vinod
Thanks,
Vinod