bahmutov / cypress-data-session

Cypress command for flexible test data setup

Home Page:https://glebbahmutov.com/blog/cypresss-data-session/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Validate based on cy.visit()

IanVS opened this issue · comments

The cy.session() validate function treats rejected promises or failed cypress chainables as false, which allows a validation function like

// Attempt to visit a page that only a logged-in user can see
function validate() {
  cy.visit('/private')
}

as shown in https://docs.cypress.io/api/commands/session#Validating-the-session. I think that's a really nice way to validate that login credentials are valid. So I have two questions:

  • Could this plugin also support that format?
  • If not, or in the meantime, how can I accomplish the same effect?

Thanks!

So far what I've come up with is something like this:

validate: (saved) => {
      if (saved.cookie?.value) {
        cy.setCookie('cookieName', saved.cookie.value);
      }
      cy.visit('/protected-route');
      return cy.url().then((url) => {
        return url.endsWith('/protected-route');
      });
    },

Does that seem like a reasonable approach, or can you think of something better?

:) that approach is slow. I thought I had an example of how I would validate a cookie for example in https://github.com/bahmutov/chat.io but I cannot find it right now. Stay tuned - I need to explain this, so follow my blog / twitter for when I drop a video or a blog post how to better do this.

Thanks. This seems not to work in fact, because I'm getting recomputing setupUser because a parent session has been recomputed, which then fails because the setup tries to run again even though I already have the user set up. I haven't figured out what it means that a parent session has been recomputed...