Tom
Tom

Reputation: 1105

How to run a hook only after selected Cucumber scenarios?

I've written a hook to capture a screenshot of a successful test:

After do |scenario|
  if scenario.passed?
    screenshot_pass = "/VVR_Browser_BDD/Test Pass/#{scenario.name}.jpeg"
    page.save_screenshot screenshot_pass
  end
end

This element works very well at the moment, but as my test suite grows, I'll be overrun by screenshots. I want to pick and choose when this hook is used.

I know with a feature file, you can tag it like:

@happypath
When /^I log into a page$/ do

etc etc.

Can that same tag mechanism be used to call the hook?

I only want to run screenshots for new tests that I've written to satisfy me they're working correctly before they're integrated into my full test suite.

Upvotes: 1

Views: 813

Answers (1)

Jarod Adair
Jarod Adair

Reputation: 419

If you are calling the code as part of a hook, you should be able to perform the action for tagged scenarios

@take_screenshot
When /^I log into a page$/ do

Then your hook could be called

After('@take_screenshot') do |scenario|
   if scenario.passed?
     screenshot_pass = "/VVR_Browser_BDD/Test Pass/#{scenario.name}.jpeg"
      page.save_screenshot screenshot_pass
  end
end

Upvotes: 2

Related Questions