Jacek Walczak
Jacek Walczak

Reputation: 79

Implement requestHooks in cucumber/testCafe

When I want to add requestHooks (for example) to my test and fixture I basically don't know where to do it.

I am using this repo https://github.com/rquellh/testcafe-cucumber

Upvotes: 2

Views: 1061

Answers (3)

Tomas Randus
Tomas Randus

Reputation: 2285

My solution (gherkin-testcafe: ^2.2.0):

The definition:

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   |

And the implementation:

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();
});

Upvotes: 0

Jacek Walczak
Jacek Walczak

Reputation: 79

I find a solution. however, it is not stable: sometimes it throws an error: '[object DOMException]:\n No stack trace available'. Maybe someone knows why? The code ( after creat mock and logger object as in testCafe doc):

When('I log in as free user', async () => {
    await testController.addRequestHooks(mock)
    await testController.addRequestHooks(logger)
    await testController.wait(2000)
    await testController
        .click(selector)
        .typeText(selector,string, {replace : true})
        .typeText(selector,string, {replace: true})
        .click(selector);
});

UPDATE: now it works with wait() function, but maybe there is some more elegant answer for that?

Upvotes: 4

mlosev
mlosev

Reputation: 5227

testController is available in Given, When, Then steps. So, you can use the test controller standard methods: addRequestHooks and removeRequestHooks.

I've modified the example from the https://github.com/rquellh/testcafe-cucumber repository to demonstrate the RequestLogger usage.

const {Given, When, Then} = require('cucumber');
const Role = require('testcafe').Role;
const RequestLogger = require('testcafe').RequestLogger;
const githubPage = require('../support/pages/github-page');

const logger = new RequestLogger('https://github.com');

Given(/^I open the GitHub page$/, async function() {
    await testController.addRequestHooks(logger);
    await testController.navigateTo(githubPage.github.url());
});

...
Then(/^Logger should contain captured request information$/, async function() {
    await testController.expect(logger.contains(record => record.response.statusCode === 200)).ok();
});

...

Upvotes: 2

Related Questions