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

如何使DOM元素在网页中可见(Cypress web Automation)

  •  0
  • Mlz  · 技术社区  · 2 年前

    我从一个API收到一系列产品名称,我反复输入到“主搜索栏”中。

    Main Search Bar

    在模拟第一次访问该页面的人的行为时,我遇到了一个弹出覆盖,它使所有元素都没有响应,除了它本身。

    Elements

    The pop-up

    0 回复  |  直到 2 年前
        1
  •  1
  •   V.Krieps Mlz    2 年前

    这解决了我的问题:

    cy.get('body').then(($body) => {
                          if ($body.find('.overlay').is(":visible")) {
                              cy.get('.overlay').click()
                          }
                      })
    
        2
  •  0
  •   agoff    2 年前

    You can also modify the element to not appear, 假设 data-dr-hide 属性决定是否显示覆盖。

    cy.get('.popup').invoke('attr', 'data-dr-hide', 'true')
      .should('have.attr', 'data-dr-hide', 'true');
    // the `true` may or may not need to be in quotes
    

    如果您能够识别 cookie local storage or session storage 值来确定您是否是第一次访问者,也可以将该值设置为表示您不是第一次访问者的值来加载页面。

    // pseudo-code
    describe('My Tests', () => {
      beforeEach(() => {
        cy.visit('/url')
          .then(() => {
            window.localStorage.setItem('firstTimeVisitor', false);
            window.sessionStorage.setItem('firstTimeVisitor', false);
        }).setCookie('firstTimeVisitor', false);
      });
    
      it('validates X', () => {
        // code here
      });
    });
    
    推荐文章