question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to handle errors during `.get`? We need to put a retry process in place.

See original GitHub issue

[QUESTION]

Is possible handler .get error? Because I need retry process case element not exists in DOM.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:29
  • Comments:22 (3 by maintainers)

github_iconTop GitHub Comments

84reactions
waitsangbvcommented, Feb 12, 2019

There are loads of scenario in which a system under test could legitimately behave differently at a given time. It could be due to data variations, system readiness, preset conditions etc. So in web testing elementA or elementB may show at a given point in the web navigation, both scenario are valid. A good test script should be able to handle this. If cypress cannot handle this kind of if (get elementA) else (get elementB) scenario, it is a major drawback.

57reactions
brian-manncommented, Jan 26, 2017

The problem here is likely your approach.

There is not and will never be a way to catch or recover from errors in Cypress. The moment error handling is introduced would create a scenario where it becomes logically impossible to consistently reproduce a test case.

It’s like trying to write a test that tests whether a process may crash. The problem is that you have no idea if or when it would crash. So to write a test you’d basically have to construct arbitrary time requirements. If the process does not crash in 10 seconds, or if the process does not crash in 10 days. Else you’d be waiting potentially until the heat death of the universe because in fact the process may never crash.

Testing in Cypress is the same way. You cannot recover from errors because you the programmer must tell us what and when you expect state to be reached in your application. If you created two flows like - do this IF this thing exists, else do something else if this thing does NOT exist - it’s impossible for a robot to understand when it should or not should give up trying.

I might be way off on my bearings for your question, so let’s approach it more pragmatically:

By default Cypress assumes whenever you cy.get an element - for that element to exist. It will wait around until it does exist or it will time out. If it times, the test fails.

If you want Cypress to wait until the element DOES NOT EXIST, you simply add that as an assertion.

cy.get("button").should("not.exist")

Now we know to retry until the element does not exist, or we time out and the test errors.

However if what you’re asking is - how do I tell Cypress to do something different IF THE ELEMENT DOES NOT EXIST - then that’s the whole problem. How does Cypress know when or when not the element should exist? Should it wait for an arbitrary amount of time? If so how much? It’s logically impossible to dictate fallback strategies because it cannot be known when something will happen, it can only be known when it has already happened.

You could achieve this yourself but if you do this, your tests will not consistently pass or fail if you are using a modern JS framework - because there is no guarantee that what you’re querying for is about to exist.

// we DO NOT RECOMMEND doing this
cy.get("body").then(($body) => {
  // synchronously query for element
  if ($body.find("element").length) {
    // do something
   } else {
    // do something else
   }
})

If what I’ve written is way off, please provide some code to further explain what you’re trying to do.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to retry the process when we get error while middle of the ...
Hello everyone I'm working on a project which I need to take input data from Excel sheet and fill the form in browser...
Read more >
Best Practices for Retry - Denali Balser
A retry of a request should only be considered when there is a chance of success- for example, in response to error codes...
Read more >
How do you implement a re-try-catch? - Stack Overflow
I have taken count and maxTries to avoid running into an infinite loop, in case the exception keeps on occurring in your try...
Read more >
Error handling and automatic retries in AWS Lambda
When you invoke a function directly, you determine the strategy for handling errors. You can retry, send the event to a queue for...
Read more >
Handle errors and exceptions in Azure Logic Apps
For the most basic exception and error handling, you can use the retry policy when supported on a trigger or action, such as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found