ebanster
ebanster

Reputation: 1086

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

Upvotes: 5

Views: 10996

Answers (3)

Eugenia
Eugenia

Reputation: 21

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)
  })
})

Upvotes: 1

Praveen Pandey
Praveen Pandey

Reputation: 698

This worked for me:

cy.get(element).invoke('removeAttr', 'target').click()

Upvotes: 10

Alex Izbas
Alex Izbas

Reputation: 625

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);
})

Upvotes: -1

Related Questions