cypress: Is it possible to handle errors during `.get`? We need to put a retry process in place.
[QUESTION]
Is possible handler .get error? Because I need retry process case element not exists in DOM.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 32
- Comments: 22 (3 by maintainers)
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.
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.getan 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.
If what I’ve written is way off, please provide some code to further explain what you’re trying to do.
Hi! First of all, thank you all for maintaining Cypress. It’s been quite useful! But I must weigh in on this issue. It’s necessary for
.getto have perhaps a flag/option to not return an assertion. The reason is simple:get(x)assumes thatxexists, but asserting existence of elements is a standard testing procedure. This comes fromget(x)not being the same as"if exists(x), get(x)", but rather"just get(x) and let's see what happens"Simplified version of @jacobprouse’s solution
Use with
cy.ifExists('.myclass')@big-gulp Cypress retries for you automatically. You can use the timeout option on
cy.getto do what you’re describing today:https://docs.cypress.io/api/commands/get.html#Syntax
Completely agree!
@brian-mann your opinion on this problem is very idealistic. Every complex application has if this do that on certain elements and Cypress should support that.
I’ve had the same issue, in our tests we don’t know what is on the page at load. So if we are testing a page of content for images, we want to skip the image tests if there are no images. We conditionally run our tests by running a command that checks the DOM using the selector with some vanilla js, and either returns it or skips the test.
using a custom function that checks the DOM.
Hope this helps someone! And thank you Cypress team, you’ve made testing a lot easier!
So… is it possible to handle errors during get? Is there a hacky-way to do this?
Is there any update on this feature? To handle
if element does not exist?This is a relly bad answer to the case that he is presenting
I’m also interested in any solution to this. I have a webpage that is only updated through refreshing, but there’s some data/state I want to test that’s updated by a background process.
If I setup my tests through the API and then navigate to the page, I’m finding that my background process hasn’t completed in time for me to test the scenario I want. This would be in addition to the tests I have before the background process finishes.
my use case is this
beforeEach:Just prevents having to login each time, i just assume you’re usually logged in, but sometimes a test will fail if not logged in. (Note, I’m using firebase and found it difficult implementing a cypress function for login)