centuryfall
centuryfall

Reputation: 409

Can I skip a global hook in Cucumber if a scenario has a tag?

In my suite, I have an After hook that deletes files from a temp directory when it finishes a scenario. However, there are times in certain scenarios where I do not want to remove files from the temp directory, and I am looking to accomplish this by providing a tag named @no_cleanup to the scenario.

Right now, my current After hook looks like this:

After do
  FileUtils.rm_f(Dir.glob(File.join @local_dir, '*'))
end

What I want to do is something like, "skip this hook if the scenario has @no_cleanup tagged on it".

Is this possible?

Upvotes: 1

Views: 924

Answers (1)

Marian13
Marian13

Reputation: 9238

As it is stated in the Cucumber documentation (do not forget to select ruby at the top of the page):

Hooks can be conditionally selected for execution based on the tags of the scenario. To run a particular hook only for certain scenarios, you can associate a Before, After, Around or AfterStep Hook with a tag expression.

Before('@browser and not @headless') do
end

See more documentation on tags.

So, in your case, I suppose it should be:

After('not @no_cleanup') do
  FileUtils.rm_f(Dir.glob(File.join @local_dir, '*'))
end

I hope it will help.

Upvotes: 3

Related Questions