Selenium test - Firefox alert disappearing immediately

后端 未结 3 777
余生分开走
余生分开走 2021-01-21 05:31

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. C

相关标签:
3条回答
  • 2021-01-21 06:21

    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();
        }
    }
    
    0 讨论(0)
  • 2021-01-21 06:23

    The chosen answer worked for me, but it seems that FirefoxDriver(org.openqa.selenium.Capabilities) is deprecated. The working alternative is:

    FirefoxOptions firefoxOptions = new FirefoxOptions();
    firefoxOptions.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
    driver = new FirefoxDriver(firefoxOptions);
    
    0 讨论(0)
  • 2021-01-21 06:34

    I don't think the 'alerts.disableSlidingEffect' Firefox profile option should be causing the alert to disappear - Instead, try creating your WebDriver instance using the unexpectedalertbehaviour capability set to 'ignore'.

    0 讨论(0)
提交回复
热议问题