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
618 views
in Technique[技术] by (71.8m points)

java - Selenium test - Firefox alert disappearing immediately

I am writing a Selenium test on Firefox that deals with an alert. The alert appears for a fraction of a second when the test is run, but when done manually the alert persists. Can anyone tell why this is the case?

I tried setting this profile on Firefox, but that did not help either:

profile.setPreference("alerts.disableSlidingEffect", true);

Can anyone tell how I can make the alert stay on the page?

In the code, I am checking the presence of the alert as this, which apparently is never getting hit since the alert is disappearing too quick:

WebDriverWait wait = new WebDriverWait(webDriver, Long.parseLong(parameters[0]));
wait.until(ExpectedConditions.alertIsPresent());
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

There are 3 states for UnexpectedAlertBehaviour:

  1. ACCEPT - Accepts the alert
  2. DISMISS - Closes/Cancels the alert
  3. IGNORE - Neither accepts nor closes the alert
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
driver = new FirefoxDriver(dc);

Then you can handle the alert by performing the operation that triggers the alert and catching it as an expected exception:

try {
    click(myButton);
} catch (UnhandledAlertException f) {
    try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        System.out.println("Alert data: " + alertText);
        alert.accept();
    } catch (NoAlertPresentException e) {
        e.printStackTrace();
    }
}

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

...