Java Spring - Selenium asserting that an element exists or not

Viewed 68

I have a template with a table where I display all the notes I have. I am trying to test a deletion of a single note. I have setup a test where I want to see if the element still exists in the table. In this case I am trying to see if there are any rows in the table, since I am only creating a single note prior to deletion. This is the template with rows that I am trying to check:

<tr th:each="note: ${notes}" class="note-elements">
    <td>
        <button id="editNoteButton" type="button" class="btn btn-success"
                th:onclick="showNoteModal([[${note.noteid}]], [[${note.notetitle}]], [[${note.notedescription}]])">Edit</button>
        <form class="delete-form" th:action="@{notes/delete}" th:object="${noteDelete}" method="post">
           <input type="hidden" name="noteid" th:value="${note.noteid}" />
           <input id="deleteNoteButton" type="submit" value="Delete" class="btn btn-danger" />
        </form>
    </td>
    <th id="table-noteTitle" scope="row" th:text="${note.notetitle}" />
    <td th:text="${note.notedescription}" />
</tr>

This is the NotePage class with only relevant fields and methods:

public class NotePage {


    @FindBy(css = ".note-elements")
    private List<WebElement> noteElements;

    private final JavascriptExecutor js;

    private final WebDriverWait wait;

    public NotePage(WebDriver webDriver) {
        PageFactory.initElements(webDriver, this);
        wait = new WebDriverWait(webDriver, 1000);
        js = (JavascriptExecutor) webDriver;
    }

    public Boolean noteElementsExist() {
        return noteElements.size() > 0;
    }
}

And in my test I am asserting it like this:

@Test
public void deleteNoteTest() {
    setUp();
    NotePage notePage = new NotePage(driver);
    ResultPage resultPage = new ResultPage(driver);
    createNote(notePage, "Note title", "Note description");
    Assertions.assertEquals("Result", driver.getTitle());
    Assertions.assertEquals("New note added!", resultPage.getSuccessMessage());
    driver.get("http://localhost:" + this.port + "/home");
    notePage.openNoteTabJS();
    notePage.deleteNoteJS();
    Assertions.assertEquals("Result", driver.getTitle());
    Assertions.assertEquals("Note deleted!", resultPage.getSuccessMessage());
    driver.get("http://localhost:" + this.port + "/home");
    notePage.openNoteTabJS();
    Assertions.assertEquals(false, notePage.noteElementsExist());
}

The test fails here:

Assertions.assertEquals(false, notePage.noteElementsExist());

In debug mode I can see that the size of the noteElements is 1 after deletion. What am I doing wrong here and how should I fix this test? The whole repo can be found here.

0 Answers
Related