代码之家  ›  专栏  ›  技术社区  ›  nachocab

如何使用Cypress比较两个DOM元素?

  •  1
  • nachocab  · 技术社区  · 7 年前

    我在检查 contenteditable

    cy.get('span[contenteditable]').then($span => {
        cy.focused().then($focused => {
            expect($focused).to.eql($span)
        }
    }
    

    我该怎么做呢?

    2 回复  |  直到 6 年前
        1
  •  6
  •   Joshua Wade Abel    6 年前

    使用 .then() 会给你一个元素 jQuery

    cy.get('span[contenteditable]').should($span => {
        cy.focused().should($focused => {
            expect($focused[0]).to.eql($span[0]);
        }
    }
    

    我还替换了 。然后() 具有 .should() 类似于 ,除非它将重试任何包含的断言,直到它们成功或超时。

    以上代码改编自 this jQuery answer .

        2
  •  2
  •   Brendan    7 年前

    我想这会满足你的要求。如果你想断言它的其他任何东西,你可以添加一个 .and() 之后 .should() 连锁断言。

      cy.focused().should('have.attr', 'contenteditable', 'true');
    
        3
  •  0
  •   Derek    5 年前

    您的问题的精神似乎是,“如何使用Cypress测试元素是否集中?”

    Cypress added more explicit support for testing focus in 2019.

    现在您只需执行以下操作:

    cy.get('span[contenteditable]').should('have.focus');