【问题标题】:How to get the json response of a RequestLogger如何获取 RequestLogger 的 json 响应
【发布时间】:2018-08-22 18:15:08
【问题描述】:

RequestLogger

A 在主测试控制器之外进行此测试,使用 page modelthis 配方。

/**
  Used to get the periodic analytic id.
  Whenever we are viewing an asset, the server must respond with an id.
  This id is later used by the client, to send periodic analytics.

  @param {object} t          Testcafe's test controller
  @param {object} logger     A testcafe's RequestLogger.
  @returns {string}          Returns the periodic analytic id.
*/
async getPeriodicAnalyticId(t, logger) {
  const logPrefix = 'Get periodic analytic id > ';
  let responseBody;

  await t
    .expect(logger.requests.length).gt(0, logPrefix + 'No requests logged.');

  responseBody = logger.requests[0].response.body;

  await t
    .expect(responseBody).notTypeOf('undefined', logPrefix + 'Logged request does not have a response a body.')
    .expect(Buffer.isBuffer(responseBody)).eql(true, logPrefix + 'Invalid response body (not buffer)');

  // Periodic analytic id can be found on the server response of the 1st analytic sent.
  return JSON.parse(logger.requests[0].response.body.toString()).id;
}

错误

在针对本地 Http 服务器运行此测试时,工作正常。但是当测试针对 https 远程服务器时它会失败。我收到此错误:

SyntaxError: Unexpected token  in JSON at position 0

在这条线上

return JSON.parse(logger.requests[0].response.body.toString()).id;

调试信息

来自本地 Http 服务器的响应正文有效: localBuffer 它转化为: localBufferToString

来自远程 https 服务器的响应正文失败: remoteBuffer 它转化为: remoteBufferToString

问题

我想知道我用来将响应正文转换为 json 的方法是否可以。目前我使用:

JSON.parse(logger.requests[0].response.body.toString())

我的环境

operating system: Windows 10
testcafe version: 0.21.1
node.js version: 9.3.0

【问题讨论】:

  • 我建议您调试测试并检查 logger.requests[0]、logger.requests[0].response 和 logger.requests[0].response.body 变量的值。他们中的一些人可能什么都不返回。以下文章描述了如何调试您的测试:devexpress.github.io/testcafe/documentation/recipes/…devexpress.github.io/testcafe/documentation/recipes/…
  • 谢谢你,Marion,我会这样做并报告。我忘了说本地服务器在 http 上,远程在 https 上,如果这很重要的话。 (我更新了问题)
  • 我用调试器的截图更新了调试信息@Marion。
  • 问题可能与 TestCafe 超时有关:t.expect 等待 3 秒,这个时间可能不足以加载资源。尝试在“expect”之前调用“wait”: await t.wait(4000).expect(logger.contains(record => record.response.statusCode === 200)).ok();如果这没有帮助,请更新此示例,以便我可以在本地查看问题:github.com/MarinaRukavitsyna/TestCafe-RequestLogger-example/…
  • @Marion 并非如此。在主测试控制器上,收集的所有请求的 statusCode 都有一个延迟和一个断言,通过了。我将尝试在您的代码上的一个更简单的示例中重现这一点。

标签: node.js testing automated-tests e2e-testing testcafe


【解决方案1】:

我遇到了同样的问题,因为在我的情况下服务器响应是 gzipped 响应,并且 testcafe logger API 不会自动解压缩响应。

所以我们按照以下步骤操作:

使用 logResponseBody 参数配置记录器(也如评论中提到的,不要将 'stringifyResponseBody' 参数设置为 true)

const logger = RequestLogger(
  /yourApiRegex/,
  { logResponseBody: true },
);

写一个辅助方法来解压请求体:

import zlib from 'zlib';

export const getBody = async body => new Promise((resolve, reject) => {
  zlib.gunzip(body, async (error, buff) => {
    if (error !== null) {
      return reject(error);
    }
    return resolve(JSON.parse(buff.toString()));
  });
});

测试中的示例用法

await t.expect(logger.contains(
  async (record) => {
    const body = await (getBody(record.response.body));
    return body.someProps === 'someValue';
  },
)).ok();

【讨论】:

  • 谢谢!我将尝试实现这一点。对我的示例进行快速检查,我只获得了第一个记录的请求的正文,但没有奏效。我得到:Error: incorrect header check。我明天会玩并报告或接受您的回答。
  • 希望你能成功。如果有帮助,请考虑接受我的回答。
  • 感谢您等待@Ricovitch。暂时还没有时间玩。所以我还不能接受答案。
  • 谢谢@Ricovitch,这就是我胡言乱语的原因。响应需要解压缩。也许我们应该在 RequestLogger sn-p 上添加 'stringifyResponseBody: false。这就是为什么我在实施您的示例时遇到错误的原因。我也更新了我的示例here
  • 对于任何感兴趣的人,我创建了基于 Ricovitch 答案的第二个解决方案。从记录器解压缩所有压缩响应的实用程序。有了这个,你可以保持你的断言干净。您可以在same repo 上找到解决方案。我也在 Git 提交上放置了 3 个标签。
猜你喜欢
  • 1970-01-01
  • 2022-06-28
  • 2019-12-29
  • 2020-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多