javierbrea / cypress-localstorage-commands

Extends Cypress' cy commands with localStorage methods. Allows preserving localStorage between tests and spec files. Allows disabling localStorage.
MIT License
178 stars 9 forks source link

Synchronized function invoke #561

Open JoeyLi-1 opened 10 months ago

JoeyLi-1 commented 10 months ago

Is your feature request related to a problem? Please describe. The restore and save storage is async function like other cypress functions.

In our case, we need to check a certain token in before() function of each spec. If it is valid then we can skip some certain steps. Such as login etc.

So, we need to restore the storage and check the token in the storage and then invoke certain functions based on the result of last step.

It said that cypress already make all function return a promise and cypress will determine the exec order.

So, I wonder if it is possible to make a synchronized function. Or maybe it is impossible to achieve this goal at all. If that is impossible, I shall drop this idea. The tests work well currently, I just want to improve the performance by skip some steps. Thank you!

Additional context E.g. I have two specs. A.spec: before('1. login', () => { cy.restoreLocalStorage() const token = cy.getLocalStorage('testToken') // decode token if (token.exp <= Date.current() / 1000) { cy.login('userA') } }) after(() => { cy.saveLocalStorage() }) B.spec: before('1. login', () => { cy.restoreLocalStorage() const token = cy.getLocalStorage('testToken') // decode token if (token.exp <= Date.current() / 1000) { cy.login('userA') } }) after(() => { cy.saveLocalStorage() })

JoeyLi-1 commented 10 months ago

@javierbrea PS: this is a great package. It works well if I restore the whole storage.

javierbrea commented 10 months ago

Hi @JoeyLi-1 , first of all, thank you, I'm glad you like the package 😃

As you said, it is not possible to make a synchronized function... it is a Cypress behavior, it is not a matter of this plugin. Anyway, I have tested by myself something similar to your example, and it worked for me by using then:

describe("localStorage item", () => {
  beforeEach(() => {
    cy.restoreLocalStorage();
    cy.visit("/");
    cy.getLocalStorage("item").then((itemValue) => {
      if (itemValue !== "true") {
        cy.task("log", "item is not true, setting it to true");
        cy.setLocalStorage("item", "true");
      } else {
        cy.task("log", "item is already true. Skipping");
      }
    });
  });

  afterEach(() => {
    cy.saveLocalStorage();
  });

  it("should be true", () => {
    cy.getLocalStorage("item").should("equal", "true");
  });

  it("should be true after reloading", () => {
    cy.getLocalStorage("item").then((itemValue) => {
      expect(itemValue).to.equal("true");
    });
  });

  it("should be true again", () => {
    cy.getLocalStorage("item").should("equal", "true");
  });

  it("should be true after reloading again", () => {
    cy.getLocalStorage("item").then((itemValue) => {
      expect(itemValue).to.equal("true");
    });
  });
});

As you can see in the code, it should set the "item" in the localStorage to "true" only the first time it is executed, and the rest of times it should skip that step. I added a "log" task to ensure that this really was what was happening, and here is the output:

Captura de pantalla 2024-01-08 a las 12 56 45