0

I have a sequence of a few tests on an application that I want to run. I want these tests to be able to run in a sequence or individually (which requires logging in).

Currently, if I was to run these tests in a sequence, it tries to log in before every test, but throws error because it cannot find the email/password field (since /login already has a user logged in, it redirects to /dashboard) which don't contain those fields. The first test case in the sequence logs into the application and then starts the next.

This is the login script:

class LoginPage {
  visit() {
    cy.visit('/login', {
      failOnStatusCode: false
    });
  }
  fillPlayerCredentials() {
    cy.fixture('playerFixture').then((player) => {
      cy.get('[data-testid=loginEmailField]').should('be.visible').type(player.email);
      cy.get(':nth-child(2) > .MuiFormControl-root > .MuiInputBase-root > .MuiInputBase-input')
        .should('be.visible')
        .type(player.password);
    });
  }
  login() {
    cy.get('[data-testid=loginButton]').click();
  }
}

When I run my second test, I want to check if localStorage is already set or if sc-token already has a value, and if so, it can just move on into the actual test case and skip logging in**

qatester123
  • 143
  • 1
  • 3
  • 9

1 Answers1

1

You can use localStorage as you usually do:

if (!localStorage.getItem('sc-token')) {
    // do login
}

You can also make assertions on it to be sure that the token has been properly saved after login. See https://example.cypress.io/commands/local-storage

  • I have added that statement into my test case, however it is still trying to log in after each test case. I'm using this method (https://stackoverflow.com/questions/50471047/preserve-cookies-localstorage-session-across-tests-in-cypress) to save localStorage from commands.js then consuming them inside my test in beforeEach and afterEach. Maybe it has something I need to modify? – qatester123 Mar 29 '21 at 16:52