Is there a way to force cypress to open in same tab instead of another tab [duplicate]
Asked Answered
W

4

5

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

Wafd answered 22/6, 2020 at 14:46 Comment(0)
R
-1

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);
})
Rustic answered 22/6, 2020 at 16:48 Comment(1)
This worked as it opens on same tab. Unfortunately, a bummer that it's not same super-domain even if it it is redirected via SSO (which is accepted in Cypress) but does not work in this case - I will deal with that separately I guess.Wafd
C
10

This worked for me:

cy.get(element).invoke('removeAttr', 'target').click()
Corell answered 3/7, 2020 at 17:51 Comment(1)
This worked for me, thanks!Scuttlebutt
H
1

Alternatively you can use

describe('describe', () => {
  let myLink: string

  it('Get response body', () => {
    cy.intercept('https://prod.cypress.io/users').as('users')
    cy.get(element).click().wait('@users')
      .then(res => {
        myLink = res.response.body.url
      })
  })

  it('Go to myLink', () => {
    cy.visit(myLink)
  })
})
Holly answered 23/12, 2021 at 4:54 Comment(0)
R
-1

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);
})
Rustic answered 22/6, 2020 at 16:48 Comment(1)
This worked as it opens on same tab. Unfortunately, a bummer that it's not same super-domain even if it it is redirected via SSO (which is accepted in Cypress) but does not work in this case - I will deal with that separately I guess.Wafd
I
-2

In case that the element does not have target attribute:

it.only('Example shows how to work with button that opens new tab without "target: _blank" and "href" attributes.', () => {
  cy.on('uncaught:exception', () => false)
  cy.visit('https://stackdiary.com/open-url-new-tab-javascript/')
  cy.window().then((win) => {
      cy.stub(win, "open").callsFake((url, target) => {
          return win.open.wrappedMethod.call(win, url, "_self");
      })
  });
  cy.get("#open-link-button").click();
  cy.url().should('include', 'stackdiary.com')
  cy.go('back')
})
Interrogative answered 11/7, 2024 at 9:18 Comment(1)
It's causing a test loading error.Trundle

© 2022 - 2025 — McMap. All rights reserved.