Reputation: 33
Assuming I am working with 2 test files. In one of the step definition files, I implemented some Hooks in them for example:
Test1:
import {
Given,
When,
Then
} from "@badeball/cypress-cucumber-preprocessor";
Given ("I visit Wikipedia", () => {
cy.visit('https://www.wikipedia.org');
});
When ("I search something", () => {
cy.get("input[type='search']").type('Music');
cy.get("button[type='submit']").click();
});
Then ("I check if it is there", () => {
cy.get('.mw-page-title-main').contains('Music')
});
Test2:
import {
Given,
When,
Then,
Before,
After
} from "@badeball/cypress-cucumber-preprocessor";
Before(() => {
cy.log('Execeuted BEFORE Scenario in Test 2');
});
After(() => {
cy.log('Execeuted AFTER Scenario in Test 2');
});
Given ("I visit my uncle", () => {
cy.log("Going to see uncle")
});
When ("I tell him stories", () => {
cy.log("He laughs at my stories")
});
Then ("I eat his food", () => {
cy.log('He is happy and gives me food');
});
When I run Test1, I get the Before and After Hooks being logged in my test and report even though they are not defined in Test1.
The reason why this is an issue for me is because if I am working on a large project with many files with different hooks, these will all be logged in my report which I don't want. I wish to have only the hooks in the test that is being called to be displayed.
Please is there a way of implementing Before and After Hooks without it logging if the test being run does not have them? Thanks.
I am using Cypress v10.11.0, cucumber by Badeball and multiple-cucumber-html-reporter
I tried running what I have in the hooks in each scenario and deleted the hooks, to avoid the Hooks from being logged but it feels reduntant.
Upvotes: 0
Views: 330
Reputation: 105
I would suggest you to use tags for it. Here are some dummy code snippet for your understanding:
const {After, Before} = require('@cucumber/cucumber') // you can use the cypress cucumber preprocessor here
Before({tags: "@foo and @bar"}, function () {
// This hook will be executed before scenarios tagged with @foo and @bar
});
Before({tags: "@foo or @bar"}, function () {
// This hook will be executed before scenarios tagged with @foo or @bar
});
For reference, please see the documentation: https://github.com/cucumber/cucumber-js/blob/main/docs/support_files/hooks.md
Upvotes: 1