Skip to content

Instantly share code, notes, and snippets.

@up1
Last active June 11, 2024 09:30
Show Gist options
  • Save up1/291123784047e236542416dffe56d197 to your computer and use it in GitHub Desktop.
Save up1/291123784047e236542416dffe56d197 to your computer and use it in GitHub Desktop.
Cypress tips :: manage tab or window
// HTML
<div class="demo">
<a href="/windows/new" ,="" target="_blank">Click Here</a>
</div>
// Test with cypress
describe("Demo spec", () => {
it("check data after click link", () => {
// Arrange
cy.visit("your target");
// Act
cy.get(".demo > a").click();
// Assert
cy.url().should("include", "/windows/new");
cy.get("h3").should("have.text", "New Window");
});
});
describe("Demo spec", () => {
it("check data after click link", () => {
// Arrange
cy.visit("your target");
// Act
cy.get(".demo > a").invoke("removeAttr", "target").click();
// Assert
cy.url().should("include", "/windows/new");
cy.get("h3").should("have.text", "New Window");
});
});
it.only("check data after click link with real", () => {
// Arrange
cy.visit("your target").then((win) => {
cy.stub(win, "open").as("openWindow");
});
// Act
cy.get("#tabButton").click();
// Assert
cy.get("@openWindow")
.should("have.been.calledOnce")
.and("have.been.calledWith", "/new-page");
cy.window().then((win) => {
win.location.href = "/new-page";
});
cy.get("h1").should("contain", "This is a sample page");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment