警报未显示

时间:2017-03-04 15:48:56

标签: selenium selenium-webdriver alert

我的测试检查用户是否可以登录该站点。 我编写了一个代码来提醒它有效但在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");
        }

1 个答案:

答案 0 :(得分:0)

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

相关问题