【问题标题】:How to check if element is fully uncovered如何检查元素是否被完全覆盖
【发布时间】:2023-02-13 10:07:42
【问题描述】:

这不是常见的赛普拉斯问题,因为固定元素被另一个元素覆盖而导致测试失败。

我的页面顶部有一个可扩展列表。当它展开时,我希望它位于页面的所有其他方面的顶部。所以我正在编写一个柏树测试来验证没有其他东西覆盖它。

不幸的是,测试并没有在明显的失败案例中失败。

此测试对上述列表成功

cy.get('#list')
  .should('be.visible')
  .find('p').each(($listItem) => {
    cy.wrap($listItem)
      .should('be.visible')
      .click(); // another layer of cover check
  });

我想这是成功的,因为元素不是因此是“可见的”,并且点击是成功的,因为每个元素的中心都被暴露了。我如何测试列表主体是否完全暴露/显示在顶部?

【问题讨论】:

    标签: cypress visibility


    【解决方案1】:

    在图像中,列表位于下拉列表的右侧,因此这是检查 x 维度重叠的一种方法:

    cy.get('#dropdown').then($el => {
        const rhs = Math.round($el[0].getBoundingClientRect().right)
    
        cy.get('#list').then($el => {
          const lhs = Math.round($el[0].getBoundingClientRect().left)
          expect(lhs).to.be.gt(rhs)
        })
      })
    

    看起来页面上还有一个您想要重复检查的表格。

    概括一下:

    Cypress.Commands.add('hasNoOverlapWith', {prevSubject: true}, (subject, others) => {
      let covered = false;
      const targetRect = subject[0].getBoundingClientRect()
      others.forEach(other => {
        cy.get(other).then($el => {
          const otherRect = $el[0].getBoundingClientRect()
    
          // other covers from the left
          const coveredLeft = otherRect.right >= targetRect.left && 
            otherRect.right <= targetRect.right
    
          // other covers from the right
          const coveredRight = otherRect.left <= targetRect.right && 
            otherRect.left >= targetRect.left
    
          if (!covered) {
            covered = coveredLeft || coveredRight
          }
        })
      })
    
      cy.then(() => {
        expect(covered).to.eq(false)
      })
    })
    
    cy.get('#list').hasNoOverlapWith(['#dropdown', '#table'])
    

    【讨论】:

    • 奇怪的是,柏树似乎没有类似于 .should('not.be.covered') 或内置的东西。这不是要检查的常见事情吗?
    • 的确。还应该在等式中考虑z-index。元素可能重叠,但如果#list 具有最高的z-index,则不会被覆盖。
    猜你喜欢
    • 2012-10-24
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多