Selenium junit 问题 - 消息:没有这样的警报

Selenium junit problem - message: no such alert

我正在为我的项目使用 selenium junit。当我尝试 运行 我的测试时,它 return 这个错误:

org.openqa.selenium.NoAlertPresentException: no such alert

它应该 return,在点击一个按钮后,一个确认提示,用户应该按确定。

测试代码:

  @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>

感谢您的宝贵时间!

问题是在点击删除按钮之前网页没有完全加载。 所以我放了一个1000毫秒的thread.sleep来解决它。

正确代码:

@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));
    }