I am trying to avoid the wait()
function on my tests.
I'm aware, based on the official docs, that Cypress works async and we dont need to use the wait()
function, especially the visit()
command handles that because it loads the page and then it moves on.
In my test case I want to handle two things that cause an issue:
open a dropdown menu which is on the Left nav menu. There are 5 menus and I want the 2nd
click on an option to go to another page
it("clicks on the 'Front End' and navigates to the correct page", () => {
visit(path, {
timeout: 120000,
pageLoadTimeout: 120000,
});
cy.get(selectors.CATEGORIES)
.eq(2)
// I use 'within', because I want to search **inside the
// selectors.CATEGORIES.eq(2) and not on the whole DOM**
.within(() => {
cy.get(dataCySelector("gridRow")).then(($optionsWrapper) => {
const parentEl = $optionsWrapper.parent();
const isMenuOpen = parentEl.css("display");
// if i dont add the wait(), it selects the 1st 'menu options'
// instead of the **3rd**
cy.wait(3000);
if (isMenuOpen === "none") {
console.log("*MENU IS CLOSE I OPEN*");
cy.contains("category").click(); // OPEN THE MENU
cy.contains("Front End").should("be.visible")
.click(); // click the 'front end'
} else {
console.log("*MENU IS OPEN I DONT CLICK ON IT*");
cy.contains("Front End").should("be.visible")
.click(); // JUST click on the 'front end'
}
cy.url().then(() => {
cy.urlIncludes("/path/to/menu/option");
cy.wait(3000);
cy.contains(dataCySelector("AN_ELEMENT"));
});
});
});
});
So the flow that I have is this:
- visit the page that i want
- get the
CATEGORIES
selector (there are 5 menus on the left nav bar) - get the 3rd
- Use
within
so to drill down in into its children (i replacedthen
because it searched on the whole DOM instead)!! - I get the parent of the 'gridRow' and look if
display=none
- ! if I don't add the
wait(3000)
, theparentEl
is the first menu wrapper!! - After the comparison, click on the 'option link'
- the user is redirected to the new page, but again i need the
wait()
so to check if the element selector exists.
Something must be wrong in here, can I get rid of the wait
s ?
thanks.