简体   繁体   English

testcafe requestLogger 只记录夹具中的第一个测试

[英]testcafe requestLogger only logs the first test in the fixture

So I'm getting better with testcafe and one feature I'd like to learn is its RequestLogger.所以我在 testcafe 方面做得越来越好,我想学习的一项功能是它的 RequestLogger。

So I've created an instance of it所以我创建了它的一个实例

    import { RequestLogger } from 'testcafe';

const logger = RequestLogger(/some reg exp/, {
    logRequestHeaders: true,
    logRequestBody: true
});

export default logger;

and then tried to use it on a sample test fixture:然后尝试在示例测试夹具上使用它:

fixture `REQUEST LOGGER TEST`
.requestHooks(logger);

test('should contain 204 as a status code 1', async t => {
    await t.useRole(role);
    await model.doSmth(t);
    await model.doSmthElse(t);

    console.log(logger.requests);

    await t
        .expect(logger.requests[0].response.statusCode)
        .eql(204);

    await t
        .expect(logger.requests[1].response.statusCode)
        .eql(200);
});

While the first test works just fine, the second one, even if it's the same, will output an empty array once I'll try to console.log(logger.requests)虽然第一个测试工作得很好,但第二个,即使它是相同的,一旦我尝试 console.log(logger.requests) 将输出一个空数组

Any idea how to go about it?知道如何去做吗?

I had the same problem because you have to wait for the Smart Assertion Query Mechanism of Testcafe before doing assertions on Logger.request array.我遇到了同样的问题,因为在对 Logger.request 数组进行断言之前,您必须等待 Testcafe 的智能断言查询机制。

The documentation tells us that using count or contains predicates make Testcafe use the Smart Assertion Query mechanism 文档告诉我们,使用 count 或 contains 谓词使 Testcafe 使用智能断言查询机制

Suggestion from Marion may do the job if your request returns a 200 statusCode :如果您的请求返回 200 statusCode ,Marion 的建议可能会完成这项工作:

await t.expect(logger.contains(record => record.response.statusCode === 200)).ok();

I found simpler to do this instead, which does not depend on the returned http status我发现这样做更简单,它不依赖于返回的 http 状态

await t.expect(logger.count(() => true)).eql(1);

replace eql(1) with the number of requests you expect to be logged before doing your assertions在执行断言之前,将 eql(1) 替换为您希望记录的请求数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM