【发布时间】:2018-08-22 18:15:08
【问题描述】:
A 在主测试控制器之外进行此测试,使用 page model 和 this 配方。
/**
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