Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...