https://cypress.io logo
Test restart/retry if api call fails
# i-need-help
d
I have a spec file with few tests in it. One of the tests, mostly the last one will fail on a network request intermittently. The status returned is 401. It doesn't happen in all the run. I'm not sure why it happens sometimes. Is there a way I can intercept this request and if 401 is returned then restart or retry the test? What other suggestions do you have?
e
Several follow-up questions - Can you reproduce the behavior manually? - Is the endpoint critical to this test case? - If you run only the last test do you seem the same behavior? If you rearrange the order of tests is it always the last test? If you can reproduce the behavior manually I'd start by working with your development team to address the underlying issue instead of writing test behavior to workaround the broken endpoint. If the endpoint isn't critical to this test case, you can simply mock it to return a 200 with some simple set of fetched data. Alternatively you can catch the specific failure and ignore it although I don't truly recommend that approach. I'd still work with the engineering team to understand why this happens though. You could intercept this request and check it's status code is 200 and conditionally end the test but I wouldn't recommend that either. My personal recommendation would be to understand what is happening with that one api before trying to create patches around its behavior. Also if the same behavior happens with any test in different orders that also tells us something that maybe your tests lose authentication at some point during the test run.
d
Manually it works. What I noticed is that the auth token somehow gets corrupted for the test and it fails to fetch the list of bunch of programs.
e
Is it only that test? If you run other tests in another order does the same behavior occur? Are you using
cy.session
or how is authentication getting fetched for each test? Maybe the authentication expires or something after a short amount of time.