ZombiePie
ZombiePie

Reputation: 379

How to open a new tab in cypress and get back to the previous tab

When i click on a button a new tab is opened and then i do certain actions in it I need to open the new tab and switch to the previous tab..There is no button to go to the previous tab so i need the two tabs open so that i can switch between them.

I have tried the below:

cy.window().then((win) => {
cy.stub(win, 'open', url =>
{
  url = "/#/myurl"
  win.location.href = Cypress.config().baseUrl + url;
   }).as("popup")
})
cy.get('#button').click()
cy.get("@popup").should("be.called")

The issue with this code is that it is only opening the new tab and not keeping the other tab open..IS there any way to achieve this. Thank you

Upvotes: 2

Views: 13519

Answers (1)

Waris Jamal
Waris Jamal

Reputation: 71

Cypress does not support opening a new tab, it can't handle it, so an alternate way to solve this problem is: If you are using <a> tag and it is having target="_blank" attribute then you can remove this attribute by using cy.get('yourLocator').invoke('removeAttr','target').click(); command to resolve this issue. If your new tab is redirecting to the new domain then you can use cy.origin(), and for going back you can use cy.go('back')for more details about origin & cy.go visit cypress.io & go, I hope this will solve your issue.

    cy.origin('https://www.youtube.com/',()=>{
    cy.get('yourlocator').click();
    });

Upvotes: 7

Related Questions