Skip to content
Advertisement

Is there a way to force cypress to open in same tab instead of another tab

I know one of Cypress’ trade-offs is testing on multiple tabs. However, our website default to opening another tab. Can I force Cypress to open on same tab to continue my tests? I have this code below but still opens a new tab:

cy.get(element).invoke('attr', 'target', ' _self').click()

I remember finding it somewhere that it can be done but my 1am brain is unable to find it via google search. I also found this on the Cypress documentation but it may not be relevant to my case as I would need to do multiple assertions on that new page which is logged on via SSO: https://docs.cypress.io/api/commands/invoke.html#Function-with-Arguments

Advertisement

Answer

I would try to invoke the href attr from yielded subject and cy.visit()(which means the cypress opens it in the same app tab), like:

cy.get(element).invoke('attr', 'href').then(myLink => {
    cy.visit(myLink);
})
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement