Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle an Alert with "UnexpectedAlertBehaviour" capability in Selenium?

Tags:

java

selenium

In selenium framework 2.25, I see that we have the UnexpectedAlertBehaviour enum type, but I don't know how to use it.

like image 884
hoang nguyen Avatar asked Oct 16 '12 11:10

hoang nguyen


People also ask

What is alert in selenium class or interface?

Alerts are basically an interface between the current web page and UI. It can also be defined as a small message box which displays an on-screen notification to give the user some kind of information or ask for permission to perform a certain kind of operation. It may be also used for warning purpose.


2 Answers

I found this portion of documentation on your issue: This may be useful for other people as well:

v2.25.0

=======

WebDriver:

  • Added API for dealing with BASIC and DIGEST authentication

    dialogs. Currently not implemented in any drivers.

  • Warn users that the IE driver will no longer use the DLL in the

    next release.

  • Deprecated browser specific WebElement subclasses.

  • Added support for "requiredCapabilities" to the remote webdrivers

    and implemented basic support for these in the firefox

    driver. Failure to fulfull a required capability will cause a

    SessionNotCreatedException to be thrown.

  • Added the ability to determine how unhandled alerts should be handled. This is handled by the "unexpectedAlertBehaviour" capability, which can be one of "accept", "dismiss" or "ignore". Java code should use the UnexpectedAlertBehaviour enum. This is only implemented in Firefox for now.

  • Allow native events to be configured in Firefox and

    (experimentally) in IE using the "nativeEvents" capability.

  • Updated supported versions of Firefox to 17.

.....

Whole list provided here

An here is the source

package org.openqa.selenium;

    public enum UnexpectedAlertBehaviour {

      ACCEPT ("accept"),
      DISMISS ("dismiss"),
      IGNORE ("ignore")
      ;

      private String text;

      private UnexpectedAlertBehaviour(String text) {
        this.text = text;
      }

      @Override
      public String toString() {
        return String.valueOf(text);
      }

      public static UnexpectedAlertBehaviour fromString(String text) {
        if (text != null) {
          for (UnexpectedAlertBehaviour b : UnexpectedAlertBehaviour.values()) {
            if (text.equalsIgnoreCase(b.text)) {
              return b;
            }
          }
        }
        return null;
      }
    }

As I see you use unexpectedAlertBehaviour to decide whether alert is unhandled and if it is so, you'll decide how to handle it.

I suppose it should be something like (my assumption):

try{
alert.accept();
}

catch(org.openqa.selenium.UnexpectedAlertBehaviour){
///...blablabla
}
like image 73
eugene.polschikov Avatar answered Sep 28 '22 02:09

eugene.polschikov


It is a CapabilityType, so you have to express it in the DesiredCapabilities that you pass when creating the driver. In Python I added this behavior to my FireFox drivers using this code:

        selenium.webdriver.DesiredCapabilities.FIREFOX["unexpectedAlertBehaviour"] = "accept"

I haven't tested this Java but in theory it should work:

DesiredCapabilities cap = new DesiredCapabailities();
cap.setPreference(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, 
   org.openqa.selenium.UnexpectedAlertBehaviour.ACCEPT);
WebDriver driver = new FirefoxDriver(cap);
like image 44
zzzzzzz Avatar answered Sep 28 '22 03:09

zzzzzzz