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
Asked
Active
Viewed 183 times
1 Answers
2
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.

Leomelzer
- 135
- 7
-
1It is also nice to use `login` method as a custom cypress command, please check the docs: https://docs.cypress.io/api/cypress-api/custom-commands – Leomelzer Jan 19 '23 at 13:38
-
No, it's not working. I want to start 2nd test case where 1st test case stops. – Muhammad Bilal Jan 23 '23 at 04:54
-
So try to define `testIsolation: 'off'` (not sure what version you're using) in cypress.config and use the same code you put at the question, this way Cypress will not clear the page by visiting about:blank before each `it` block. Should work, but please keep in mind that's not a good practice. – Leomelzer Jan 25 '23 at 16:36