You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!
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:
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()
})
The text was updated successfully, but these errors were encountered: