Cucumber after each test I need to run a specific scenario, same as Background. Background is for before each, do we have similar for after each

Viewed 44

Groups page functionality

Background: Given When the user logs in Then add the test group

@smoke Scenario: Verify the groups list page When the user opens the groups page Then the group's page title should be visible

1 Answers

Cucumber does not have an after hook like background, but there is a workaround: if you want to run a after hook for a specific feature file then add a tag to the feature file, and then use that tag to write an after hook in the env file.

The given example is for capyabra with ruby

Test.feature

@test_tag
Feature: test feature file

env.rb file

After('@test_tag') do
  # code for after hook
end

If you want to run an after hook for a specific scenario, use the same method. Add a tag for the scenario and pass it in after hook.

After('scenario1') do
  # code
end

If you want a general after hook to run after every scenario, then we can directly write the After hook without tag

Example:

After do
  # code
end

Note: The scenario parameter passed gives information for the scenario e.g scenario.failed? to check if the scenario ran successfully if you want to run logic not associated with scenario details in the After hook you can omit the parameter

Example:

After do|scenario|
  if scenario.failed?
    page.save_screenshot("path_#{scenario.name.parameterize}.png")
  end
end
Related