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();
  }
});
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.

Further reading

Problem with a question

Cypress · Middle · question 33

Test yourself

Take the Cypress · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz