Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Cypress saying my element is detached after just running a get command?

Objective: I want to click on a particular element on the page using an accessibility selector with cypress

Code

cy.findAllByRole('rowheader').eq(2).click();

Error

Timed out retrying: cy.click() failed because this element is detached from the DOM.

<th scope="row" data-automation-id="taskItem" aria-invalid="false" tabindex="-1" class="css-5xw9jq">...</th>

Cypress requires elements be attached in the DOM to interact with them.

The previous command that ran was:

  > cy.eq()

This DOM element likely became detached somewhere between the previous and current command.

Question: I can see in the DOM that this element is still present - there is no logic that will detach this element from the DOM, and the eq method certainly wouldn't do that. Additionally, the findAllByRow method is clearly working as it has found the correct th element I want to click on. How come its saying the element is detached? Is there a workaround for this situation?

like image 230
Gwynn Avatar asked May 04 '21 20:05

Gwynn


People also ask

Why is element detached from DOM?

The "element is detached from DOM" is a sign of a problem, not the problem itself! There is a variety of ways the test can observe the application and wait for it to be ready. You can observe the DOM itself, the network traffic, listen for events, even reach into the code to spy on method calls.

How do you make elements visible in Cypress?

For handling the hidden elements, Cypress takes the help of the jQuery method show. It has to be invoked with the help of the Cypress command (invoke['show']).

How do you know if an element is displayed or not in Cypress?

Tip: if a Cypress test fails with "element is not visible" error, but you are sure the element should be visible, you can debug the visibility check yourself by stepping through the Cypress. dom. isVisible code, see Debug the Element Visibility Problems in Cypress.

What is assertion error in Cypress?

CypressTest AutomationSoftware Testing. Cypress has a list of common assertions that can be applied to any element on the browser. Assertions are the checkpoints that confirm if a test step of the automated test case passed or failed. Thus it checks the expected state of the application under test.


1 Answers

This could be bad advice, but can you try the following?

cy.findAllByRole('rowheader').eq(2).click({force: true})
like image 177
Ε Г И І И О Avatar answered Oct 09 '22 20:10

Ε Г И І И О