I found the problem. First of all everything, there were two different issues to solve about my Stackoverflow issue.
1- my API request came as unauthorized.
I was getting authorization tokens like this--> anyVariable=localStorage.getItem('autToken')
This was work without a problem while I using npx Cypress open
because Cypress works extremely quickly on the interface and anyVariable
fully filled by token and works without any problem.
But while using headless mode then cypress gets slower so that's why Cypress sent API req before anyVariable
fully filled by token and responses as unauthorized.
I read some issues and articles about Cypress and Getting item by LocalStorage. People said that this usage "anyVariable=localStorage.getItem('autToken')" to get item local storage works as async So that's why I used cy.
command to solve my problem and my attempt completely solved my problem.
I solved the problem by using the chainable cypress method.
cy.window()
.then((window) => {
return window.localStorage.getItem('anyVariable')
})
.then((token) => {
setAnyVariable(token)
})
2- The second problem was caused by one of our frontend component which refreshes the newly created item list to be newly created items visible. It was strange because that component was works stable when I run my tests with npx cypress open
.
Why I explained that if you face with similar issue then you can try to check your frontend components.
I working on a solution to that problem right now.