Efficiently Open and Manage Browser Tabs with Cypress
Table of contents
As Cypress runs inside the browser, there will never be support for multiple browser tabs in Cypress (As per cypress's official documentation).
However, during testing, there may be instances when the application opens a link in a new tab, and it becomes necessary to test the contents of the new tab especially when users click on <a> tag that opens a new tab. Users then want to switch to that tab to verify that the content is loaded.
In this blog post, we will explore different ways to handle new tabs in Cypress to tackle this use case.
In case our application is using an anchor link with a target attribute like this: <a href="…" target="_blank">
, we can modify the target attribute and open the link in the same tab instead.
The HTML target
attribute specifies a name or a keyword that indicates where to display the response that is received.
Target:
Value | Description |
_blank | The link is opened in a new window or tab |
-self | The link is displayed in the same frame (This is default) |
There are 2 ways to manipulate the target attribute and open the link in the same tab.
First Approach
The first approach involves manipulating the target attribute to open the link in the same tab in a below way:
If the target value is “_blank”, we can remove the attribute and the link will open in the same tab. This is because, as mentioned in the table above, when “target=_blank”, the link is set to open in a new tab or window.
Sample Code:
cy.get(‘.example > a’)
.invoke(‘removeAttr’,’target’).click();
Implementation:
it.only('opens the link in same tab by removing the target attribute', () => {
cy.visit('https://the-internet.herokuapp.com/windows')
cy.get('.example > a')
.invoke('removeAttr','target').click();
});
Second Approach:
The second approach involves directly manipulating the target attribute of the link to “_self” which will also result in the link opening in the same tab. Therefore, if the “target” attribute is set to “_self”, the link will open in the same tab.
Sample Code:
cy.get(‘.example > a’)
.invoke(‘attr’,’target’,’_self’).click()
Implementation:
it.only(‘opens the link in same tab by removing the target attribute‘, () => {
cy.visit('https://the-internet.herokuapp.com/windows')
cy.get(‘.example > a’)
.invoke("attr", "target", "_self")
.click();
});
Tip:
If you want to go back to the previous tab, you can do it using cy.go(‘back’) or cy.go(-1) but this will only work if your domain URL is the same.
Conclusion
By using either of these two approaches, we can effectively handle multi-tab support in Cypress while ensuring that the link is opened in the same tab. This will enable you to perform actions on the same page and verify your results.
Thanks for reading. Happy Learning !!
Connect and Follow Me On Socials :
Like 👍 | Share📲 | Comment💭
Subscribe to my newsletter
Read articles from Nikunj Vaishnav directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Nikunj Vaishnav
Nikunj Vaishnav
👋 Hi there! I'm Nikunj Vaishnav, a passionate QA engineer Cloud, and DevOps. I thrive on exploring new technologies and sharing my journey through code. From designing cloud infrastructures to ensuring software quality, I'm deeply involved in CI/CD pipelines, automated testing, and containerization with Docker. I'm always eager to grow in the ever-evolving fields of Software Testing, Cloud and DevOps. My goal is to simplify complex concepts, offer practical tips on automation and testing, and inspire others in the tech community. Let's connect, learn, and build high-quality software together! 📝 Check out my blog for tutorials and insights on cloud infrastructure, QA best practices, and DevOps. Feel free to reach out – I’m always open to discussions, collaborations, and feedback!