Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Skip BDD Scenario at runtime using custom hook #3349

Unanswered
brunoklein asked this question in Q&A
Discussion options

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:

Allure Report - Overview Tab

Allure Report - Retries Tab

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /