In Cypress, I am trying to create a method that uses an if/else statement and should do the following:
- It checks to see if the pagination button is displaying
- If the pagination button is visible (element), click the pagination button and then execute the assertions of the table it is on
- If the pagination button is not visible, assert the last row of the current table for the correct name and school
I created a variable to store the pagination button with a data attribute id.
When the condition is false, it executes the first block of code of the if statement when it should be executing the else portion. For more context, the pagination button isn’t appearing at the moment, yet, it still executes the first if statement and its block of code and it is failing because cy.get([data-test-id="last_pagination_button"]
)
is not found.
Can someone tell me what may be causing this?
<code>public clickPaginationButtonIfVisible(text1: string, text2: string) {
var element = cy.get(`[data-test-id="last_pagination_button"]`);
if (element) {
cy.get(`[data-test-id="last_pagination_button"]`).click();
cy.get("#Name").last().should("contain", text1);
cy.get("#School").last().should("contain", text2);
} else {
cy.get("#Name").last().should("contain", text1);
cy.get("#School").last().should("contain", text2);
}
}
</code>
<code>public clickPaginationButtonIfVisible(text1: string, text2: string) {
var element = cy.get(`[data-test-id="last_pagination_button"]`);
if (element) {
cy.get(`[data-test-id="last_pagination_button"]`).click();
cy.get("#Name").last().should("contain", text1);
cy.get("#School").last().should("contain", text2);
} else {
cy.get("#Name").last().should("contain", text1);
cy.get("#School").last().should("contain", text2);
}
}
</code>
public clickPaginationButtonIfVisible(text1: string, text2: string) {
var element = cy.get(`[data-test-id="last_pagination_button"]`);
if (element) {
cy.get(`[data-test-id="last_pagination_button"]`).click();
cy.get("#Name").last().should("contain", text1);
cy.get("#School").last().should("contain", text2);
} else {
cy.get("#Name").last().should("contain", text1);
cy.get("#School").last().should("contain", text2);
}
}