【问题标题】:CYPRESS - XHR duration no longer captured with cy.intercept()CYPRESS - XHR 持续时间不再使用 cy.intercept() 捕获
【发布时间】:2021-04-15 21:12:55
【问题描述】:

Cypress 即将弃用 cy.route()cy.server() 以使用 cy.intercept()

这是我的旧代码,它将捕获 XHR 时间和输出

        cy.server()
        cy.route('POST', api_URL_Live).as('CONS');

        cy.wait('@CONS').then((xhr) => {
            CONSTime = Number(JSON.stringify(xhr.duration));
        });

这非常有效,并且会将持续时间保存到文件中

不再捕获持续时间的新代码

        cy.intercept('POST', api_URL_Live).as('CONS');

        cy.wait('@CONS').then((xhr) => {
            CONSTime = Number(JSON.stringify(xhr.duration));
        });

有谁知道为什么这个功能不再起作用,有什么帮助吗?

【问题讨论】:

  • 如果你记录 CONSTime 你会得到什么值。你还有什么错误吗?
  • @AlapanDas 只是“未定义”作为值,没有错误

标签: cypress


【解决方案1】:

cy.interceptcy.route 的 API 有一些重叠,但并不相同。

durationcy.route 的一个未记录的属性,但它确实有效,尽管它不受正式支持。

因为它没有被记录并且在cy.route 中很少使用,所以没有考虑在cy.intercept 中实现。

您仍然可以使用 cy.route 测量请求的持续时间 - cy.route 不会被删除,从而导致您的代码被破坏。一旦cy.intercept 对于大多数常见用例来说足够稳定,它很可能会被移动到插件中。

如果您想对cy.intercept 做同样的事情,您可以使用回调来衡量:

cy.intercept('POST', url, (req) => {
  const startTime = Date.now()
  req.reply(res => {
    // measure the time between request received and response received
    totalTime = Date.now() - startTime
  })
})

有一个开放的功能请求将计时数据添加到cy.intercepthttps://github.com/cypress-io/cypress/issues/15969

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 2021-11-10
    相关资源
    最近更新 更多