https://cypress.io logo
Struggling to make cypress wait for element when a...
# i-need-help
e
I'm trying to get an element using
cy.get(".classOfElement", {timeout:10000}).should("be.visible").and("contain", "some text ")
From my understanding, the chained
should()
should abide by the timeout defined in
get()
. So even if the element is found by
get()
it won't return the element until the
should()
and the
and()
are satisfied. Am I wrong? Is that not how it works? Currently the behaviour I see is that the initial
get()
runs, the element is found, but it doesn't seem to wait any longer after that for the chained commands. Which in turn instantly fail because the contents hasn't loaded yet.
g
Watch

https://youtu.be/MWY8gYdvyho

and read https://glebbahmutov.com/cypress-examples/recipes/retry-ability.html
e
thanks, i appreciate that's how it works, and i see this exact behaviour IF the tests pass (the failing code is in afterEach hook) - however, if any test fails, so does the assertion. To remove any potential interference, i created a new test, all it does is load up the page in a beforeEach, runs a test that does
get('body')
which passes and then in afterEach i run a simple
get(".element", {10_000}).should("be.visible")
(yeah i even added a 10 secs). I run this, and it works like a charm, no issues. But as soon as I update the test to include
.should("not.exist")
the afterEach block straight up fails, instantly, regardless of the timeout added; and it fails with
$el.css is not a function
if i just have the visibility check, but if i add a
.click()
instead i get
No elements in the current DOM matched your query
which is complete rubbish 😅 - if all i change is the result of the test that has nothing to do with the page behaviour and the afterEach behaviour changes, how, why? In my search for answers I found this GH issue that was raised back in 2018: https://github.com/cypress-io/cypress/issues/2831 the suggested workaround is to use
{force: true}
but that also hasn't worked.
g
Can you make a reproducible example and open an issue or link to the existing one? I think if this is true, it might be a bug in the retry mechanism and it should be fixed now before Cypress v13 while they are looking at other retry bugs
e
I'll see if i can get something together
Hmm, i'm struggling to replicate the exact behaviour with a simple example. The afterEach still doesn't execute once the test fails, but does when it passes, but in the simple example it doesn't seem to produce a failure in the actual afterEach portion of the execution, which i see in my main one.
I'll do a little more investigation though
g
No,
afterEach
should execute, even if the test fails, you probably have something else going on there
e
perfect, in that case, i'll double check the example and send it over