【问题标题】:Convert response body blob to json or plain text in javascript将响应正文 blob 转换为 json 或 javascript 中的纯文本
【发布时间】:2019-08-27 01:59:05
【问题描述】:

在我的 cypress 测试中,我提交了一个请求,在响应中,正文返回为 blob。如何检查正文中的some text 内容。有没有办法将blob 转换为json or plain text。请参阅随附的屏幕截图。在下面添加测试代码

cy.request('https://someurlHere).then((response) => {
          expect(response.status).to.eq(200) // this is loooking good
          expect(response).to.have.property('headers')  // this is loooking good
          console.log(response.text());
          //var alertArr = [];
          //alertArr = response.json();
          //console.log(alertArr);
        })

【问题讨论】:

  • 你总是可以使用 response.json()response.text() 来返回一个承诺。一旦 promise 得到解决,你就会得到结果
  • 我得到response.json() or response.text() is not a function ... TypeError: response.text is not a function
  • 我已经在上面添加了我的 cypress 测试代码。

标签: javascript cypress


【解决方案1】:

只需检查 response.body。请参见下面的示例。

cy
  .request('POST', 'http://localhost:8888/users/admin', { name: 'Jane' })
  .then((response) => {
    // response.body is automatically serialized into JSON
    expect(response.body).to.have.property('name', 'Jane') // true
  })

【讨论】:

    猜你喜欢
    • 2022-12-16
    • 2013-11-06
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多