2

It seems that in the ASP.NET WebDriver API, executing the following when there is no javascript alert present results in an exception:

browser.SwitchTo().Alert();

IE and FF both throw a WebDriverException, but Chrome throws an InvalidOperationException.

So far, this is the only code that seems to work:

try
{
    var alert = browser.SwitchTo().Alert();
    if (alert != null)
        alert.Dismiss();
}
catch (WebDriverException)
{
    // alert was not present in IE or FF
}
catch (InvalidOperationException)
{
    // alert was not present in Chrome
}

Is there a way to check that an alert dialog is present, without having to catch an exception?

danludwig
  • 46,965
  • 25
  • 159
  • 237

2 Answers2

2

The actual answer here is no, you must always catch an exception. The logic behind the design of the API is that you should always be aware of what browser state you expect. If you expect an alert to appear, you should be able to use switchTo() to switch to it and handle it. If you expect an alert to appear, and use switchTo() and it is not present, that is an exceptional condition, and an exception is thrown. The normal (non-exceptional) case is to not expect an alert, and thus there is no corresponding method to look for an alert not being displayed. Incidentally, this is the same logic employed by findElement(). You can argue that the wrong logic is being employed by the API designers, but that is the way the current API is implemented.

JimEvans
  • 27,201
  • 7
  • 83
  • 108
  • Answered, thank you. The argument that the presence of an alert is an exceptional condition when you are not expecting it sounds reasonable in theory. However when using WebDriver in an automated series of tests, as I assume is the reality for many who use it, that design decision causes difficulties. I have had to completely disable the try/catch for Firefox, as it was just too slow to run after a browser action step was executed (SpecFlow AfterStepAttribute). If any WebDriver project leaders are reading this, something like `bool IWebDriver.CanSwitchToAlert()` would be freakin awesome. – danludwig Feb 24 '12 at 21:47
  • In point of fact, I **am** one of the project leaders, the one responsible for the .NET bindings. However, consensus would have to be reached for an API addition like that, since we like to keep the API capabilities synchronized across language bindings as much as possible. It might be worth an email to the Selenium Developers mailing list, or to hop on the #selenium IRC channel on Freenode to make your case there. – JimEvans Mar 01 '12 at 16:20
  • Sorry, I totally spaced on the name. I recognize now (previously had to download the source w/crazyfunbuild when a bug was briefly introduced in a nuget package several months ago). Thanks Jim. Before I go and push this into the decision-making community, what do you think the idea's chances are? Do you have an opinion on the matter, one way or another? – danludwig Mar 01 '12 at 17:33
  • Honestly, not that great. Simon Stewart has pretty strong opinions on what the API should look like, and this is the kind of thing I'm guessing he won't care too much for. Bring it up anyway though, and make your case. The worst thing that could happen is we end up saying no, and most of us are unfailingly polite chaps. – JimEvans Mar 01 '12 at 21:46
0

Keeping in mind your excerpt :

Is there a way to check that an alert dialog is present, without having to catch an exception?

This exception is likely being caused by the statement below, upon trying to perform a switch when there is no alert window present :

var alert = browser.SwitchTo().Alert();

I would suggest that you try:

  • Start by using the getWindowHandles api method and investigate the presence/absence of the Alert i.e another window. You can either loop through or check using a counter.

  • If the Alert window is present you can use its window handle and dismiss the same.

  • Else (i.e If the Alert window is not present ) skip the execution of the statement below
    and thus avoid the exception.

    var alert = browser.SwitchTo().Alert();

More info abt moving between windows and window handle provided here

self-babush
  • 585
  • 3
  • 13
  • I tried looking in the window handles. There was only 1 window handle in the collection, for the main window. I will try it again. – danludwig Jan 24 '12 at 01:13
  • Well,sigh :(. There seem to be an existing question (although for a different language, with a similar attempted solution .http://stackoverflow.com/a/8245417/948641). Raised a meta question. http://meta.stackexchange.com/q/120144/177478 , if that helps. – self-babush Jan 24 '12 at 11:21
  • I have confirmed that the alert does not affect the window handles. Even when there is an alert present on the browser, the window handles collection contains 1 entry, which is a reference to the window where the alert originated. The Alert() method is what throws the exception, but it seems to be the only way to find out if there is an alert present. – danludwig Jan 24 '12 at 12:36