CypressError:超时重试:cy.click()失败,因为此元素与DOM分离

时间:2019-11-13 08:59:03

标签: angular cypress

接收到后端响应后,下拉菜单基本上需要很长时间才能获得负载。如果我等待大约8秒钟,那么它可以工作。但是,不想在这里硬编码等待。任何想法可能出了什么问题?我也无法识别CSS。

cy.get('input').last().type(`{selectall}${value}`);
        cy.get('mat-option > span').then(option => {
            if (option.get(0).textContent === 'Loading...') {
                cy.wait(5000);
            }
        });   

cy.containsCaseInsensitive(value, 'mat-option').first().scrollIntoView().debug().click();

错误日志-

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

<mat-option _ngcontent-gcj-c21="" class="mat-option ng-star-inserted" role="option" ng-reflect-value="[object Object]" tabindex="0" id="mat-option-104" aria-disabled="false" style="">...</mat-option>

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

The previous command that ran was:

  > cy.debug()

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

Common situations why this happens:
  - Your JS framework re-rendered asynchronously
  - Your app code reacted to an event firing and removed the element

You typically need to re-query for the element or add 'guards' which delay Cypress from running new commands.

https://on.cypress.io/element-has-detached-from-dom

4 个答案:

答案 0 :(得分:0)

您可以通过这种方式尝试

cy.get('input').last().type(`{selectall}${value}`);
        cy.get('mat-option > span').then(option => {
            //if (option.get(0).textContent === 'Loading...') {
                //cy.wait(5000);
            //}

            cy.containsCaseInsensitive(value, 'mat-option').first().scrollIntoView().debug().click();
        });

答案 1 :(得分:0)

为了等待网络响应,您可以为网络请求添加别名并等待它们。

在此处查看有关赛普拉斯的文档:https://docs.cypress.io/guides/guides/network-requests.html#Flake

cy.route('/search*', [{ item: 'Book 1' }, { item: 'Book 2' }]).as('getSearch')

// our autocomplete field is throttled
// meaning it only makes a request after
// 500ms from the last keyPress
cy.get('#autocomplete').type('Book')

// wait for the request + response
// thus insulating us from the
// throttled request
cy.wait('@getSearch')

通过上面的链接,文档中提供了更多示例。

答案 2 :(得分:0)

也许是一个较晚的答案,但对遇到同样问题的其他人可能很有用,有时使用赛普拉斯jQuery包装器是个好主意。

Cypress.$('.mySelector').trigger('click');

答案 3 :(得分:0)

我确实遇到了同样的问题 保持柏树等待一段时间直到页面加载 解决了分离元素的问题

示例

cy.wait(3000)
cy.get(growth.tpsl_input_sessionName).clear().type(sessionName);