简体   繁体   English

赛普拉斯:你如何循环等待某些东西变得可见(最终)?

[英]Cypress: how do you loop waiting for something to become visible (eventually)?

I am waiting for a class to be become available in Cypress it might take 2sec, 5min or 20mins to become available, the time is never the same;我正在等待 class 在赛普拉斯可用,可能需要 2 秒、5 分钟或 20 分钟才能可用,时间永远不会相同;

But I can't seem to get it to work, it always logs it as {} in the logs, this is despite in chrome dev tools class.very is available.但我似乎无法让它工作,它总是在日志中将其记录为 {},尽管在 chrome 开发工具中 class.very 可用。

for (let i = 1; i < 10; i++) {
      const $vg = cy.$('.very')
      cy.log($vg)
      if ($vg == ".very") {
        cy.get("#dosomething").click();
      } else {
        cy.log($vg)
        cy.wait(2000);
      }
    }

Something like this can be used to address the timeout issue.像这样的东西可以用来解决超时问题。

cy.get('.very', { timeout: 10000 })
  .should('be.visible')
  .and('contain', 'Home')
  .click()

Recommend checking out this cypress.io documentation: https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Return-Values建议查看此 cypress.io 文档: https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Return-Values

The top portion of the document gets into the asynchronous nature of cypress which is really important to wrap your head around and it explains why your example code won't work.文档的顶部介绍了 cypress 的异步特性,这对于让您全神贯注非常重要,它解释了为什么您的示例代码不起作用。 Here's a key blurb from that document: " You cannot assign or work with the return values of any Cypress command. Commands are enqueued and run asynchronously. "这是该文档中的关键内容:“您不能分配或使用任何赛普拉斯命令的返回值。命令被排队并异步运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM