Selenium JUnit问题 - 信息: 没有这样的警告框。

3

我正在使用 Selenium JUnit 进行项目开发。当我尝试运行测试时,返回以下错误:

org.openqa.selenium.NoAlertPresentException: no such alert

点击按钮后,应返回确认弹窗,并要求用户按下“确定”按钮。
测试代码:
  @Test
  public void adminDeleteDoc() {
    driver.get("http://localhost:8080/login");
    driver.manage().window().setSize(new Dimension(550, 713));
    click(By.linkText("Accesso amministratori"));
    sendKeys(By.id("username"), "8245");
    sendKeys(By.id("password"), "prova1");
    click(By.id("submit"));
    click(By.id("btn_deletedoc"));
    assertThat(driver.switchTo().alert().getText(), is("Sei sicuro di voler cancellare questo dottore?"));
    driver.switchTo().alert().accept();
  }

    public void click(By locator) {
        new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeClickable(locator)).click();
    }

    public void sendKeys(By locator, String text) {
        findElement(locator).sendKeys(text);
    }

    public WebElement findElement(By locator) {
        return new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(locator));
    }

我也尝试使用webdriverwait (10),期望条件是弹出警告框,但它没有起作用。

HTML代码:

<body>
    <div class="container text-center">
        <div align="center">
            <h2>Gestione dottori</h2>
            <table class="table table-striped table-responsive-md">
                <tr>
                    <th>ID</th>
                    <th>Nome</th>
                    <th>Specialit&agrave</th>
                    <th>Azione</th>
                </tr>
                <tr th:each="doc: ${listDoc}">
                    <td th:text="${doc.getId()}"></td>
                    <td>Dr. <span th:text="${doc.getFirstName()}"></span> <span
                        th:text="${doc.getLastName()}"></span></td>
                    <td th:text="${doc.getDoc_type()}"></td>
                    <td>
                        <div class="col col-md-auto align-self-center">
                            <div class="p-1">
                                <a th:href="@{/admin_deletedoc/{id}(id=${doc.getId})}" onclick="return confirm('Sei sicuro di voler cancellare questo dottore?');">
                                    <button type="button" class="btn btn-danger"
                                        id="btn_delete_doc" name="btn_delete_doc">
                                        <span id="btn_deletedoc" class="fas fa-trash-alt"></span>
                                    </button>
                                </a>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
            <a th:href="@{/admin_createdoc}"><span id="btn_createDoc"
                class="plus bg-dark">+</span></a>
            <hr>
            <div class="col col-lg-2 align-self-center">
                <div class="p-1">
                    <form th:action="@{/admin_logout}" method=post>
                        <button name="btn_logout_profile" id="btn_logout_profile"
                            type="submit" class="btn btn-primary">Log Out</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

感谢您的时间!

第二次执行 driver.switchTo().alert() 时,错误出现在哪里? - Lino
第一次我使用assertThat(driver.switchTo... - Radix
由于您在这里的回答,弹出窗口似乎没有出现。至少 Selenium 没有看到它。我理解您的意思了吗?“我也尝试使用 webdriverwait(10),期望条件警报出现,但它不起作用。”你是说你能在视觉上看到它,但是 Selenium 无法识别它? - Prophet
是的,没错。如果我运行我的项目,它会出现(在Selenium IDE中也是如此),但在Selenium Junit中却没有。我猜测页面没有完全加载,所以我为警报设置了一个webdriverwait,但它失败了(它给了我超时异常)。 - Radix
1个回答

0
问题在于在点击删除按钮之前,网页没有完全加载。因此,我加了一个1000毫秒的线程休眠来解决它。
正确的代码:
@Test
  public void adminDeleteDoc() {
    driver.get("http://localhost:8080/login");
    driver.manage().window().setSize(new Dimension(550, 713));
    click(By.linkText("Accesso amministratori"));
    sendKeys(By.id("username"), "8245");
    sendKeys(By.id("password"), "prova1");
    click(By.id("submit"));

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

    click(By.id("btn_deletedoc"));
    assertThat(driver.switchTo().alert().getText(), is("Sei sicuro di voler cancellare questo dottore?"));
    driver.switchTo().alert().accept();
  }

    public void click(By locator) {
        new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeClickable(locator)).click();
    }

    public void sendKeys(By locator, String text) {
        findElement(locator).sendKeys(text);
    }

    public WebElement findElement(By locator) {
        return new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(locator));
    }

你应该使用条件等待,即检查页面标题是否可用或其他内容,而不仅仅是固定的1秒。 - Lino

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接