Cypress · Middle · Question 25 of 77

How do you test that window.open was called with the right URL?

cy.window().then((win) => {
  cy.stub(win, "open").as("open");
});
cy.contains("Share").click();
cy.get("@open").should("be.calledWith", "https://example.com/share");
Show the answer

Correct answer: Stub win.open and assert on the alias

Explanation

Stubbing replaces the real call, so no tab opens, and Sinon assertions check the arguments.

Further reading

Problem with a question

Cypress · Middle · question 25

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