【问题标题】:How to handle(cy.get()) dynamic element if exist do A if not do B如何处理(cy.get())动态元素如果存在则做A如果不做B
【发布时间】:2019-08-18 04:51:01
【问题描述】:

我必须处理对话框元素,如果元素存在做某事而不做其他事情,但是 cy.get('...') CypressError: Timed out retrying, 导致有时元素不存在。如何处理

我试过 cy.get('...').find('...')、cy.get('...').then() 和 cy.get('...') .should('to.exist') 它不起作用。

cy.get('div.ui-dialog-content.ui-widget-content > p-messages > div > ul > li:nth-child(2) > span')
        .should('to.exist').then(() => {
            // only do if found element 
            cy.get('div.ui-dialog-titlebar.ui-widget-header.ui-helper-clearfix > a > span')
                .click()
        })

// do another 

期望:如果对话框显示关闭它并继续测试,如果对话框不显示则正常测试

【问题讨论】:

    标签: cypress


    【解决方案1】:

    赛普拉斯文档有一个关于条件测试的指南,带有example of how to run your tests based off element existence

    我已经重写了您问题中的代码,以使用文档中示例的方式。这应该有效:

    cy.get('body').then(($body) => {
      if ($body.find('div.ui-dialog-content.ui-widget-content > p-messages > div > ul > li:nth-child(2) > span').length) {
        // element found, do something here...
        cy.get('div.ui-dialog-titlebar.ui-widget-header.ui-helper-clearfix > a > span')
          .click()
      } else {
        // do something else...
      }
    })
    

    【讨论】:

      猜你喜欢
      • 2011-08-16
      • 2022-07-23
      • 2021-04-05
      • 1970-01-01
      • 2022-10-24
      • 2014-01-13
      • 2018-12-09
      • 2013-06-15
      相关资源
      最近更新 更多