检查 Cypress 中的主机是否在线

发布于 2025-01-14 17:27:28 字数 119 浏览 2 评论 0原文

我的测试在通过网络界面控制的物联网设备上运行。我想创建一个恢复出厂设置测试,其中涉及设备的重新启动,并且我想循环检查设备是否再次在线(“可 ping”)。有没有办法在 Cypress 内部执行 ping 命令并获取其返回值。

My tests run on a IOT device which is controlled via a web interface. I want to create a factory reset test which involves a reboot of the device and I want to check in a loop if the device is online ("pingable") again. Is there a way to execute the ping command inside of Cypress and get a return value of it.

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

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

发布评论

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

评论(1

渡你暖光 2025-01-21 17:27:28

假设您指的是标准 ping 协议,这就是形式。替换您的设备地址并回复消息。

cy.exec('ping google.com', {failOnNonZeroExit: false})
  .then(reply => {
    expect(reply.code).to.eq(0)
    const expectedMsg = 'Pinging google.com [142.250.66.206] with 32 bytes of data:\r\nReply from 142.250.66.206: bytes=32' 
    expect(reply.stdout).to.satisfy(msg => msg.startsWith(expectedMsg))
  })

可能不需要循环,但如果需要,我会使用递归函数

function doPing(count = 0) {

  if (count === 10) throw 'Failed to ping';

  cy.exec('ping google.com', {failOnNonZeroExit: false})
    .then(reply => {
      if (reply.code > 0) {

        cy.wait(1000)    // whatever back-off time is required
        doPing(++count)

      } else {
        expect(reply.stdout).to.satisfy(msg => ...)
      }
    })
}

doPing()

Presuming you mean the standard ping protocol, this is the form. Substitute your device address and reply message.

cy.exec('ping google.com', {failOnNonZeroExit: false})
  .then(reply => {
    expect(reply.code).to.eq(0)
    const expectedMsg = 'Pinging google.com [142.250.66.206] with 32 bytes of data:\r\nReply from 142.250.66.206: bytes=32' 
    expect(reply.stdout).to.satisfy(msg => msg.startsWith(expectedMsg))
  })

A loop may not be needed, but if so I'd use a recursive function

function doPing(count = 0) {

  if (count === 10) throw 'Failed to ping';

  cy.exec('ping google.com', {failOnNonZeroExit: false})
    .then(reply => {
      if (reply.code > 0) {

        cy.wait(1000)    // whatever back-off time is required
        doPing(++count)

      } else {
        expect(reply.stdout).to.satisfy(msg => ...)
      }
    })
}

doPing()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文