question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Keep the page state after user logs in when using cy.session()

See original GitHub issue

What would you like?

I think we could benefit from a small modification of the cy.session() command where after the user logs into the application or if the validate() method is run we could continue from that state in the tests, instead of the page being cleared.

Why is this needed?

When we use cy.session() and we add a validation for the session like in my case by trying to open a specific page that is only accessible if the user is logged in, then I would have to use:

    validate() {
      cy.visit('/');
      cy.location('pathname', { timeout: 30000 }).should('equals', '/home');
    },

and this is the only way I can validate the login for our application. Then after the session we need to again use cy.visit() to navigate to the home page which is again extra time to re-load everything instead of just continuing from the validation or login state… removing the blank page part would be amazing

Other

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
devhidcommented, Jun 22, 2022

+1 for this. We only have one E2E flow in our product (but with certain variations) and our E2E test just wouldn’t make sense if the previous page state isn’t carried over. I’m having to resort putting all the test steps inside an entire it block making it less readable and harder to debug incase there’s an error

However, I understand that tests, in theory, should have isolated state.

So I’d like to propose maybe we have a step() function that would help organize the flow of an E2E test while still being within a single test (it block) and also display those steps in the Test Runner:

describe('Instant Flow', () => {
    it('should fill in preliminary information , () => {
        step('Enter First Name', () => {
            cy.get('[data-testid=pre-account-firstName').type('John');
            cy.get('[data-testid=pre-account-firstName').should('have.value', 'John');
        })

        step('Enter Last Name', () => {
            cy.get('[data-testid=pre-account-lastName').type('Doe');
            cy.get('[data-testid=pre-account-lastName').should('have.value', 'Doe');
        });

        step('Enter State', () => {
            cy.get('#question-state').click();
            cy.get('.hds-listbox--with-selection-icon ul li').contains('Arizona').click();
            cy.get('#question-state span[class=hl-dropdown__button-text')
                .invoke('text')
                .should(($text) => {
                    expect($text.trim()).to.equal('Arizona');
                });
        });
    });
});

However, I know OP’s idea was more for performance sake of reusing the state from previous test and this is more for readability, but I feel this might still work for both use cases.

1reaction
emilyrohrboughcommented, Jun 22, 2022

@devhid This request is to remove the last “Clear Page” step that executed in the cy.session command to remove the need to re-visit once a session has been established.

What you are describing is disabling test isolation for a set of tests to have better readability and more granular tests for a given point in your application. This actually requested in #22279 and the full extend of this work can be tracked on #22230.

Read more comments on GitHub >

github_iconTop Results From Across the Web

session - Cypress Documentation
Because cy.session() clears the page and all session data before running setup , you can use it to easily switch between sessions without...
Read more >
Cypress Tests: Preserve Cookies, Keep Active Login - DZone
This article explains step-by-step how to preserve and share the cookie/session between tests in an easy way.
Read more >
Use cy.session() instead of login page object in Cypress
Short guide to using cy.session() command an save minutes from your test run. ... Use cy.session() instead of login page object in Cypress....
Read more >
How to save and restore state in Cypress tests - Reflect.run
The new cy.session() command solves this problem by caching and restoring cookies, localStorage and sessionStorage after a successful login. The ...
Read more >
Is there a way to keep session Storage between tests in Cypress
The cy.session() command is a cache - anything inside does not get called multiple times. You need to call it beforeEach() test not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found