我正在使用 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à</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