Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vergjor opened this issue Jun 16, 2022 · 5 comments · Fixed by #23503
Closed

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

vergjor opened this issue Jun 16, 2022 · 5 comments · Fixed by #23503
Assignees
Labels
topic: session Issues when using session command type: enhancement Requested enhancement of existing feature

Comments

@vergjor
Copy link

vergjor commented Jun 16, 2022

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

@emilyrohrbough emilyrohrbough added the topic: session Issues when using session command label Jun 16, 2022
@devhid
Copy link

devhid commented 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.

@emilyrohrbough
Copy link
Member

@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.

@liambutler
Copy link

I personally like having isolation between it() blocks, so I'm glad to see that #22230 is working towards having an option to be configured, rather than one or the other

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Aug 26, 2022

The code for this is done in cypress-io/cypress#23503, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Aug 30, 2022

Released in 10.7.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v10.7.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Aug 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
topic: session Issues when using session command type: enhancement Requested enhancement of existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants