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
--
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.
alertText = Driver.SwitchTo().Alert().Text;
2.12
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.
> --
> You received this message because you are subscribed to the Google Groups
> "webdriver" group.
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.
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
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
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 -
--
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.