I need to find the number of elements present on a web page with a given locator(cy.get() or cy.xpath()). If the element is not present with the given locator, then it should not Fail the test.
I have tried cy.get(), cy.find(), cy.xpath(): all of them fails the test in case the element is not found on web page. I have tried to use cy.get('body').find('loc').length; But it also fails the test.
The below code works, but i am not able to use the value of x out side the loop. And scenario is as such that i cant put all of my code inside then().
let x = 0;
cy.get("body").then(($body) => {
x = $body.find("element").length;
cy.log(`inside then: `,x);
})
cy.log(`outside then: `,x);
Expected: inside then: ,1 outside then: ,1
Actual: inside then: ,1 outside then: ,0