Automated tests 在cucumber/testCafe中实现requesthook

Automated tests 在cucumber/testCafe中实现requesthook,automated-tests,e2e-testing,cucumberjs,web-testing,testcafe,Automated Tests,E2e Testing,Cucumberjs,Web Testing,Testcafe,当我想将requestHooks(例如)添加到我的测试和夹具中时,我基本上不知道在哪里做 我正在使用此repo测试控制器在给定中可用,当时,然后步骤。 因此,您可以使用测试控制器标准方法:和 我修改了存储库中的示例,以演示RequestLogger的用法 const{gived,When,Then}=require('cumber'); const Role=require('testcafe')。角色; const RequestLogger=require('testcafe')。Reque

当我想将requestHooks(例如)添加到我的测试和夹具中时,我基本上不知道在哪里做


我正在使用此repo

测试控制器
给定
中可用,
时,
然后
步骤。 因此,您可以使用测试控制器标准方法:和

我修改了存储库中的示例,以演示
RequestLogger
的用法

const{gived,When,Then}=require('cumber');
const Role=require('testcafe')。角色;
const RequestLogger=require('testcafe')。RequestLogger;
const githubPage=require('../support/pages/github-page');
常量记录器=新请求记录器('https://github.com');
给定(/^I打开GitHub页面$/,异步函数(){
等待testController.addRequestHooks(记录器);
等待testController.navigateTo(githubPage.github.url());
});
...
然后(/^Logger应包含捕获的请求信息$/,异步函数(){
等待testController.expect(logger.contains(record=>record.response.statusCode==200)).ok();
});
...

我找到了一个解决方案。但是,它不稳定:有时会抛出错误:“[object DomeException]:\n没有可用的堆栈跟踪”。也许有人知道为什么? 代码(在testCafe文档中创建mock和logger对象之后):

更新:现在它与wait()函数一起工作,但也许有更优雅的答案?

我的解决方案(小黄瓜测试咖啡馆:^2.2.0):

定义:

Feature: Check server names

Scenario Outline: Fetch pages
    Given there is the <url>
    When I check the response status
    Then the http <prameter> equals the <value>

    Examples:
        | url                    | prameter | value |
        | https://www.seznam.cz  | server   | nginx |
        | https://www.google.com | server   | gws   |

解决方案基于@mlosev答案:
Feature: Check server names

Scenario Outline: Fetch pages
    Given there is the <url>
    When I check the response status
    Then the http <prameter> equals the <value>

    Examples:
        | url                    | prameter | value |
        | https://www.seznam.cz  | server   | nginx |
        | https://www.google.com | server   | gws   |
const {Given, When, Then} = require('cucumber');
const {RequestLogger} = require('testcafe');

let logger;
Given(/there is the (.+)/, async (t, [url]) => {
    logger = RequestLogger(url, {
        logResponseHeaders: true,
    });
    await t.addRequestHooks(logger);
    await t.navigateTo(url);
});

When(/I check the response status/, async t => {
    await t.expect(logger.contains(record => record.response.statusCode === 200)).ok();
});

Then(/the http (.+) equals the (.+)/, async (t, [name, value]) => {
    await t.expect(logger.contains(record => record.response.headers.server === value)).ok();
});