I am trying to automate one test scenario where i am first traversing pages one by one and on each page i am traversing list of elements until i find my desired element, but when my condition is met in the if statement i am setting a flag to stop both the loops, but the outer loop keeps on running till the last page even if my condition is already met at 2nd page can you guys please help me in this
my method
clickOnNextBtnUntilDisable() {
cy.get(".MuiPagination-ul li button").last().invoke("text").as("lastPage");
cy.get("@lastPage").then((lastPageText) => {
const index = parseInt(lastPageText, 10); // converting last page text into int.
let flag = false;
for (let i = 1; i < index; i++) {
cy.get(".MuiGrid-justify-xs-space-evenly .MuiGrid-container > p").each(
(el, index) => {
if (el.text() === "Test Attribute Do not touch") {
cy.log("Condition met");
flag = true;
return;
}
}
);
if (!flag) {
cy.contains("Next").click();
} else {
return;
}
}
});
}
my test
it("Adding question to existing attribute", function () {
dashBoardPage.clickOnSwitchView();
dashBoardPage.clickOnAttributes();
attributesPage.clickOnNextBtnUntilDisable();
});
2
Answers
Aliasing the flag
You could use an alias to test your flag condition:
This "coordinates" the static
for()
loop with the results of the dynamic queries inside the loop.Without the line
cy.get('@stop').then(stop =>
, the code is enqueuing all the commands onto the queue before the command queue has started to run.An alternative approach, set the data with a fixture so that you know exactly what page your text is on.
Then your loops can be separate, and the Next action does not depend on
flag
.