不显示警报

Alert is not displayed

我的测试检查用户是否可以登录站点。 我写了一个代码来提醒有效,但在 catch 中显示部分。 我尝试在没有 trycatch 的情况下编写警报,但显示错误。

如何在没有trycatch的情况下编写警报并显示警报(测试通过)。

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for alert to be present (tried for 5 second(s) with 500 MILLISECONDS interval)

这是我的代码:

public void getMessage() {
        //Verify that is message preview
        try {
        WebDriverWait wait = new WebDriverWait(driver, 5);
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        System.out.println(alert.getText());
        alert.accept();
        Assert.assertTrue(alert.getText().contains("ERROR: Invalid login credentials."));
        } catch (Exception e) {
        System.out.println("Alert is not displayed");
        }

由于您已经确认该对话框不是真正的 JS 警告对话框,因此您将无法使用 Alert。好消息是它 HTML 就像页面的任何其他部分一样,因此您可以只抓取包含错误文本的元素。