quickly detecting that an Alert window is not present

2,838 views
Skip to first unread message

Steve Murphy

unread,
Dec 15, 2011, 11:38:12 AM12/15/11
to webd...@googlegroups.com

Hi,

 

I have the following method to retrieve the text in an Alert window.

 

My problem is that when the Alert window does not exist it takes ages for the Driver (which is an IWebDriver instance) to throw an exception.

 

How could we speed up the throwing of the exception?

 

private string AlertText()

{

   string alertText = string.Empty;

 

   try

   {

      alertText = Driver.SwitchTo().Alert().Text;              

   }

   catch (Exception ex)

   {

      // swallow deliberately

   }

 

   return alertText;

}

 

Usage:

 

// handle ajax alerts as a fail

string alertText = AlertText();

if (alertText.Contains("AJAX")) return SuccessResult.Fail(alertText);

 

Thanks,

 

Steve

 

Moises Siles

unread,
Dec 15, 2011, 5:10:58 PM12/15/11
to webd...@googlegroups.com
What version of webdriver are you using?

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

Tarun Bhadauria

unread,
Dec 16, 2011, 12:19:39 AM12/16/11
to webd...@googlegroups.com
Are you using implicitly wait?
then this might be a reason why webdriver waits for some time before giving up on finding the alert.

if so the you could induce a smaller implicitly wait before your alert text and bring it back to normal post it -


<smaller implicitly wait>

      alertText = Driver.SwitchTo().Alert().Text;              

<implicitly wait back to normal>

though I am not sure if this is really good approach to work with implicitly wait.

Steve Murphy

unread,
Dec 16, 2011, 11:15:00 AM12/16/11
to webd...@googlegroups.com

2.12

Steve Murphy

unread,
Dec 16, 2011, 11:17:42 AM12/16/11
to webd...@googlegroups.com

I tried that – but it doesn’t make the slightest bit of time difference with SwitchTo.

 

Steve

--

You received this message because you are subscribed to the Google Groups "webdriver" group.

To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/LETKogf5sEkJ.

Levent

unread,
Dec 17, 2011, 2:54:59 PM12/17/11
to webdriver
Unfortunately this is one of those methods in webdriver for which you
need to create your own wrapper method  with a timer, which ideally
should have been built-in.
On Dec 16, 8:17 am, Steve Murphy <Steve.Mur...@mdsl.com> wrote:
> I tried that - but it doesn't make the slightest bit of time difference with SwitchTo.

>
> Steve
>
> From: webd...@googlegroups.com [mailto:webd...@googlegroups.com] On Behalf Of Tarun Bhadauria
> Sent: 16 December 2011 05:20
> To: webd...@googlegroups.com
> Subject: [webdriver] Re: quickly detecting that an Alert window is not present
>
> Are you using implicitly wait?
> then this might be a reason why webdriver waits for some time before giving up on finding the alert.
>
> if so the you could induce a smaller implicitly wait before your alert text and bring it back to normal post it -
>
> <smaller implicitly wait>
>       alertText = Driver.SwitchTo().Alert().Text;
> <implicitly wait back to normal>
>
> though I am not sure if this is really good approach to work with implicitly wait.
> --
> You received this message because you are subscribed to the Google Groups "webdriver" group.
> To view this discussion on the web visithttps://groups.google.com/d/msg/webdriver/-/LETKogf5sEkJ.

Daniel Wagner-Hall

unread,
Dec 18, 2011, 1:48:42 PM12/18/11
to webd...@googlegroups.com
Unfortunately, it may take up to two seconds for an alert to propagate
through Firefox's internals in a way that we can see, so there is a
two-second delay on every check for an alert, where an alert is not
present. We are working on this problem, and have tried out a couple
of different solutions, watch this space :)

> --
> You received this message because you are subscribed to the Google Groups
> "webdriver" group.

Andy

unread,
Dec 16, 2011, 7:02:07 PM12/16/11
to webdriver
Steve,

Don't try to get the text right away. That might be what's slowing you
down. Remember good tests fail fast. So don't do anymore than you need
to. I handle alerts in two steps.
1. Check to see if the alert exists
2. If the alert exists to something, else don't bother trying.

Here is my hasAlert() method.

public boolean hasAlert()
{
// Get a handle to the open alert, prompt or confirmation
try{
myDriver.switchTo().alert();
return true;
} catch (Exception e) {
// no alert
return false;
}
}

I don't use implicit waits and myDriver.switchTo().alert(); returns
immediately (less than 1 second) if there is no alert.

Give it a try and let me know.

-Andy

On Dec 16, 8:17 am, Steve Murphy <Steve.Mur...@mdsl.com> wrote:

> I tried that - but it doesn't make the slightest bit of time difference with SwitchTo.


>
> Steve
>
> From: webd...@googlegroups.com [mailto:webd...@googlegroups.com] On Behalf Of Tarun Bhadauria
> Sent: 16 December 2011 05:20
> To: webd...@googlegroups.com
> Subject: [webdriver] Re: quickly detecting that an Alert window is not present
>
> Are you using implicitly wait?
> then this might be a reason why webdriver waits for some time before giving up on finding the alert.
>
> if so the you could induce a smaller implicitly wait before your alert text and bring it back to normal post it -
>
> <smaller implicitly wait>
>       alertText = Driver.SwitchTo().Alert().Text;
> <implicitly wait back to normal>
>
> though I am not sure if this is really good approach to work with implicitly wait.
> --
> You received this message because you are subscribed to the Google Groups "webdriver" group.

> To view this discussion on the web visithttps://groups.google.com/d/msg/webdriver/-/LETKogf5sEkJ.

Steve Murphy

unread,
Dec 19, 2011, 5:58:42 AM12/19/11
to webd...@googlegroups.com
Hi Andy,

It takes 2.1422142 seconds to fail, where HasAlert is the method that you kindly provided me with.

Here's the code:

DateTime start = DateTime.Now;
if (HasAlert())
{


string alertText = AlertText();
if (alertText.Contains("AJAX")) return SuccessResult.Fail(alertText);
}

else
{
TimeSpan ts = DateTime.Now.Subtract(start);
}

We execute hundreds of actions per test run, so 2.14 seconds per action can quickly add up.

I'm using C# with WebDriver 2.12 - what are you using to get a fail in less than 1 second?

Thanks,

Steve

Mark Collin

unread,
Dec 19, 2011, 7:11:24 AM12/19/11
to webd...@googlegroups.com
Based on this I've had to implement the following:

public void waitForAlertToBeAccepted(final int timeout) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
Boolean switched = false;
try {
driver.switchTo().alert().accept();
switched = true;
} catch (Exception Ex) {
// Couldn't switch!
}
return switched;
}
});
}

I have some test fields that pop up an alert when trying to close the window
because changes have not been saved, this will ensure that I wait for the
alert to appear and accept it rather than just doing a driver.close() and
leaving the firefox window behind. You could probably tweak it for your
purposes in the interim.

-----Original Message-----
From: webd...@googlegroups.com [mailto:webd...@googlegroups.com] On

Behalf Of Daniel Wagner-Hall
Sent: 18 December 2011 18:49
To: webd...@googlegroups.com

Subject: Re: [webdriver] quickly detecting that an Alert window is not
present


--
This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

If you have received this email in error please notify postm...@ardescosolutions.com

Andy

unread,
Dec 19, 2011, 1:45:15 PM12/19/11
to webdriver
Steve,
I'm using Selenium 2.15, Java. I logged the time in the console before
the hasAlert method is called, and before the method returns inside
the catch. Here are the stats.

IE8 (29ms)
{"Calling hasAlert":"18:25:50.026"}
{"No Alert":"18:25:50.055"}


FF8 (11secs)
{"Calling hasAlert":"18:26:34.220"}
{"No Alert":"18:26:43.844"}

second run (8secs)
{"Calling hasAlert":"18:36:47.608"}
{"No Alert":"18:36:55.879"}


Chrome (9ms)
{"Calling hasAlert":"18:27:25.290"}
{"No Alert":"18:27:25.299"}

According the Daniel's comment above it appears that Firefox has a
performance issue with alerts. I'm guessing that the 2.14 second delay
you are seeing is in Firefox?


-Andy

> For more options, visit this group athttp://groups.google.com/group/webdriver?hl=en.- Hide quoted text -
>
> - Show quoted text -

Steve Murphy

unread,
Dec 20, 2011, 8:12:31 AM12/20/11
to webd...@googlegroups.com
Yes that's right - the 2.14 sec delay I'm experiencing is in firefox 8 with c# and webdriver 2.12

dimisec

unread,
Feb 22, 2012, 8:07:02 AM2/22/12
to webd...@googlegroups.com
Andy, is there any way to detect the alert presence using the JSON wire protocol?  I use the 2.19 version of the remote server, and when trying to get a URL of the page generating an alert, I'm getting the following exception (from the server log):

14:05:10.109 WARN - Exception thrown
org.openqa.selenium.UnhandledAlertException: Modal dialog present
Build info: version: '2.19.0', revision: '15849', time: '2012-02-08 16:10:57'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.18-164.2.1.el5', java.version: '1.6.0_13'
Driver info: driver.version: EventFiringWebDriver
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:170)
        at org.openqa.selenium.remote.ErrorHandler.createUnhandledAlertException(ErrorHandler.java:150)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:119)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:439)
        at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:200)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.openqa.selenium.support.events.EventFiringWebDriver$2.invoke(EventFiringWebDriver.java:100)
        at $Proxy1.get(Unknown Source)
        at org.openqa.selenium.support.events.EventFiringWebDriver.get(EventFiringWebDriver.java:154)
        at org.openqa.selenium.remote.server.handler.ChangeUrl.call(ChangeUrl.java:39)
        at org.openqa.selenium.remote.server.handler.ChangeUrl.call(ChangeUrl.java:1)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:619)
14:05:10.110 WARN - Exception: Modal dialog present
Build info: version: '2.19.0', revision: '15849', time: '2012-02-08 16:10:57'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.18-164.2.1.el5', java.version: '1.6.0_13'
Driver info: driver.version: EventFiringWebDriver

Krishnan Mahadevan

unread,
Feb 22, 2012, 8:19:22 AM2/22/12
to webd...@googlegroups.com
Perhaps you could include the get URL part within a try catch block, look for UnhandledAlertException (which comes up when there is an unhandled Alert present between two webdriver operations), process the alert and retry the get URL part again.
Would that do ?


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/ZalJJWyrmlkJ.
Reply all
Reply to author
Forward
0 new messages