该脚本在我的计算机上成功 运行 但是当我将它发送给我的队友时,不会获得任何数据

The script was successfully run on my computer but when i send it to my teammates, will not getting any data

我正在尝试将 OTP 文本保存到我的 Data.json 文件中,这是一个代码。

it.only('Otp Test', function () {
    cy.visit("https://www.mailinator.com/", { timeout: 30000 })
    cy.get("#addOverlay").type("jd")
    cy.get("#go-to-public").click()
    cy.wait(2000)
    cy.xpath("(//table/tbody/tr[1])[2]").click()
    cy.get('#html_msg_body').then(function ($ele) {
      var ifele = $ele.contents().find('body > table > tbody > tr:nth-child(3) > td > h2');
      cy.wrap(ifele).then(function (ele) {
        const OTP = ele.text();
        cy.readFile("cypress/fixtures/Data.json").then((profile) => {
          profile.OTP = OTP
          cy.writeFile("cypress/fixtures/Data.json", profile);
        })
      })
    })
  })

有时会在 cy.wrap(ifele).then(function (ele) { 中出现此错误 Timed out retrying after 10000ms: Expected to find element: undefined, but never found it. 我不知道会发生什么。谁知道请帮帮我

首先,邮件收件箱发生变化,因此请通过文本搜索您的 OTP 邮件。

点击邮件行后,需要等待iframe加载。标准方法是使用 .its('0.contentDocument.body').should('not.be.empty').

it.only('Otp Test', function () {
  cy.visit("https://www.mailinator.com/", { timeout: 30000 })
  cy.get("#addOverlay").type("jd")
  cy.get("#go-to-public").click()
  cy.wait(2000)
  cy.contains('table tbody tr', 'OTP').click()  // find the right email

  cy.get('#html_msg_body')  // iframe
    .its('0.contentDocument.body').should('not.be.empty')  // wait for loading
    .wait(0)    // some javascript needs to run in the iframe 
    .find('table > tbody > tr:nth-child(3) > td > h2')    
    .then($h2 => {    
      const OTP = $h2.text()
      cy.readFile("cypress/fixtures/Data.json").then((profile) => {
        profile.OTP = OTP
        cy.writeFile("cypress/fixtures/Data.json", profile);
      })
    })
})

脚注

“有时出现此错误...”是因为 iframe 可能需要时间来加载其源代码(就像浏览器中的浏览器)。

使用 .should('not.be.empty') 重复读取,直到它有一些内容。