Last active
June 11, 2024 09:30
-
-
Save up1/291123784047e236542416dffe56d197 to your computer and use it in GitHub Desktop.
Cypress tips :: manage tab or window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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