Skip to content
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

Synchronized function invoke #561

Open
JoeyLi-1 opened this issue Dec 29, 2023 · 2 comments
Open

Synchronized function invoke #561

JoeyLi-1 opened this issue Dec 29, 2023 · 2 comments
Assignees
Labels

Comments

@JoeyLi-1
Copy link

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.

  • I had tried to use async / await to make the function work as sync function. But Cypress did not allow this kind of operations. Failed with exceptions.
  • I had chained all the operations with .then() in before() hook within each spec. But it failed with exceptions as well.

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
Copy link
Author

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

@javierbrea
Copy link
Owner

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

@javierbrea javierbrea self-assigned this Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants