Alert is not displayed

狂风中的少年 提交于 2021-02-05 11:34:47

问题


My test checks whether the user can log in to the site. I wrote a code to alert that works but showing parts in catch. I tried to write alert without try and catch but displays an error.

How to write alert without try and catch and alert to be displayed(test to pass).

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

This is my code:

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:


Since you have confirmed that the dialog is not truly a JS alert dialog, you won't be able to use Alert. The good news is that it's HTML just like any other part of the page so you can just scrape the element that contains the error text.



来源:https://stackoverflow.com/questions/42597943/alert-is-not-displayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!