-
-
Notifications
You must be signed in to change notification settings - Fork 747
Skip BDD Scenario at runtime using custom hook #3349
-
Hello CodeceptJS community!
Is there any proper way to skip a BDD scenario at runtime?
As I didn't find a proper solution for this on CodeceptJS docs I decided to create my own solution :) I would like to see your thoughts and also appreciate any feedback.
Feature file:
Feature: Messenger
Scenario: Send a text message
Given I am on Messenger page
And the submit message button is available
When I send a text message
Then I see the text message on chat thread
Steps file:
const { I } = inject();
const textMessage = `Automatic message ${Date.now()}.`;
Given("I am on Messenger page", () => {
I.amOnPage("/messenger");
});
Then("the submit message button is available", async () => {
const isSubmitButtonAvailable = await tryTo(() => {
I.seeElement("#messageSubmit");
});
if (!isSubmitButtonAvailable) {
throw 'Skip Test - Submit message button is not available';
}
});
When("I send a text message", () => {
I.fillField('#messageInput', textMessage);
I.click('#messageSubmit');
});
Then("I see the text message on chat thread", () => {
I.see(textMessage, "#chat");
});
Custom hook file:
const event = require('codeceptjs').event;
module.exports = function () {
event.dispatcher.on(event.test.failed, function (test, err) {
if (err.message.includes('Skip Test')) {
try {
test.skip();
return {};
} catch {
return {};
}
}
});
};
How it works
The test step validates the element (or anything you want) inside a tryTo;
If the element is not available then we throw an error with a custom message containing 'Skip Test';
On custom hook level, for failed event, we catch the message and use the test.skip() function;
*Note the test.skip() function is inside a try/catch because it throws an error:nding { message: 'async skip; aborting execution' }.
The final result on Allure looks like this:
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 3