Cypress · Middle · Question 33 of 77
What is "conditional testing" and why is it discouraged?
cy.get("body").then(($body) => {
if ($body.find(".cookie-banner").length) {
cy.get(".cookie-banner button").click();
}
});- 1If statements aren't allowed in Cypress specs
- 2Branching on DOM state is flaky; the DOM may still change
- 3It makes the spec file much too long to read easily
Show the answer
Correct answer: Branching on DOM state is flaky; the DOM may still change
Explanation
The banner may appear a moment later. Control the state instead, for example by setting the consent cookie before visiting.