[英]Selenium junit problem - message: no such alert
I am using selenium junit for my project.我正在为我的项目使用 selenium junit。 When I try to run my test, it return this error:当我尝试运行测试时,它返回此错误:
org.openqa.selenium.NoAlertPresentException: no such alert
It should return, after clicking a button, a confirmation alert and the user should press ok.单击按钮后,它应该返回确认警报,并且用户应该按确定。
Test code:测试代码:
@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));
}
I tried also with webdriverwait (10), expected condition alert is present but it doesn't work.我也尝试使用 webdriverwait (10),出现预期的条件警报,但它不起作用。
Html code: 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>
Thank you for your time!感谢您的时间!
The problem was that the web page wasn't fully loaded before the click on the delete button.问题是 web 页面在单击删除按钮之前没有完全加载。 So I put a thread.sleep of 1000 ms to resolve it.所以我放了一个 1000 毫秒的 thread.sleep 来解决它。
Correct code:正确代码:
@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));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.