Skip to content

Synchronized function invoke  #561

@JoeyLi-1

Description

@JoeyLi-1

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()
})

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions