When I want to switch from A tab to B then it shows blank page using cypress

316 Views Asked by At

After login when I switch from Tab A to Tab B then it shows blank page, How i maintain session of Tab A. After performing activity on Tab A, I move to Tab B and start performing activity on Tab B

My Code

Screen which I want to automate

1

There are 1 best solutions below

3
Leomelzer On

If the session has not been created yet, it will run the callback fn code, otherwise, it will only restore the session (and you'll have to visit the page again).

So, I think changing before hook to a beforeEach and adding a visit after the login method would work:

beforeEach(() => {
  login('user1')
  cy.visit('http://#')
})

But this approach will visit the page twice in the first run, to avoid this personally I would use the login with it's code in the before hook and restore and visit the page in the beforeEach.

const login = ({ sessionId, username, password }) => {
  cy.session(sessionId, () => {
    cy.visit('http://#')
    cy.get('[type=text]').type(username)
    cy.get('[type=password]').type(password)
    cy.get('[type=submit]').click()
  })
}

describe('test', () => {
  const sessionId = 'Login with valid credentials'

  before(() => {
    login({ sessionId, username: 'user1', password: 'Test123' })
  })

  beforeEach(() => {
    cy.session(sessionId)
    cy.visit('http://#')
  })

  it('Tab A', () => {
    cy.get('#A').click()
  })

  it('Tab B', () => {
    cy.get('#B').click()
  })
})

Please let me know if it solves your problem.