Issue
I am using selenium junit for my project. 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.
Html code:
<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!
Solution
The problem was that the web page wasn't fully loaded before the click on the delete button. So I put a thread.sleep of 1000 ms to resolve it.
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));
}
Answered By - Radix
Answer Checked By - David Goodson (JavaFixing Volunteer)