是否可以继续执行测试,如果e元素未找到柏树

发布于 2025-02-10 13:40:49 字数 78 浏览 2 评论 0原文

我有一个情况,需要等待元素(广告),如果可见,则需要单击它,但是如果超时后未找到元素,则需要继续执行测试。

如何用柏树处理情况?

I have a case when I need to wait for element (advertising), if it's visible then needs to click it, but if element wasn't found after timeout then needs to keep executing a test.

How to handle the situation with Cypress ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九厘米的零° 2025-02-17 13:40:49

赛普拉斯说检查条件元素的方式是

cy.get('body').then(($body) => {
  const modal = $body.find('modal')
  if (modal.length) {
    modal.click()
  }
})

您很可能将其放在测试的顶部,并且运行得太早(没有重试时间仪)。

您可以添加等待30秒的等待,但是每次测试都会延迟。

最好递归地

const clickModal = (selector, attempt = 0) => {

  if (attempt === 100) return  // whole 30 seconds is up

  cy.get('body').then(($body) => {
    const modal = $body.find('modal')
    if (!modal.length) {
      cy.wait(300)  // wait in small chunks
      clickModal(selector, ++attempt)
    }
  })
  return             // done, exit
}

cy.get('body')
  .then($body => clickModal('modal'))

拦截广告,

这是如果您可以找到网络选项卡中广告的URL,请使用cy.intercept()捕获并固定它以停止模态显示。

The way Cypress says to check for a conditional element is Element existence

cy.get('body').then(($body) => {
  const modal = $body.find('modal')
  if (modal.length) {
    modal.click()
  }
})

Most likely you put that at the top of the test, and it runs too soon (there's no retry timeoout).

You can add a wait say 30 seconds, but the test is delayed every time.

Better to call recursively

const clickModal = (selector, attempt = 0) => {

  if (attempt === 100) return  // whole 30 seconds is up

  cy.get('body').then(($body) => {
    const modal = $body.find('modal')
    if (!modal.length) {
      cy.wait(300)  // wait in small chunks
      clickModal(selector, ++attempt)
    }
  })
  return             // done, exit
}

cy.get('body')
  .then($body => clickModal('modal'))

Intercept the advert

Best is if you can find the url for the advert in network tab, use cy.intercept() to catch it and stub it out to stop the modal displaying.

愛放△進行李 2025-02-17 13:40:49

我尝试了上述解决方案,但似乎在某些情况下参数$ body无法包含必要的元素,因为当我们调用cy.get('hody')。因此,我找到了另一个解决方案,使用柏树使用jQuery,这就是:

let counter = 0;
const timeOut: number = Cypress.config('defaultCommandTimeout');
  
  const sleep = (milliseconds) => {
    const date = Date.now();
    let currentDate = null;
    do {
      currentDate = Date.now();
    } while (currentDate - date < milliseconds);
  };

  while (true) {
    if (Cypress.$(element).length > 0 && Cypress.$(element).is(':visible')) {
      Cypress.$(element).click();
      break;
    } else {
      sleep(500);
      counter = counter + 500;
        if (counter >= timeOut) {
          cy.log(elementName+ ' was not found after timeout');
          break;
        }
    }
  }

I tried the above solution, but seems that in some cases parameter $body could not contain necessary element, cause it was not loaded when we invoked cy.get('body'). So, I found another solution, using jQuery via Cypress, here is it:

let counter = 0;
const timeOut: number = Cypress.config('defaultCommandTimeout');
  
  const sleep = (milliseconds) => {
    const date = Date.now();
    let currentDate = null;
    do {
      currentDate = Date.now();
    } while (currentDate - date < milliseconds);
  };

  while (true) {
    if (Cypress.$(element).length > 0 && Cypress.$(element).is(':visible')) {
      Cypress.$(element).click();
      break;
    } else {
      sleep(500);
      counter = counter + 500;
        if (counter >= timeOut) {
          cy.log(elementName+ ' was not found after timeout');
          break;
        }
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文