0

Capybara screenshot doesn't take snapshot while step fails. I am using below code:

Capybara.save_and_open_page_path = "/file/path"
Capybara::Screenshot.register_filename_prefix_formatter(:rspec) do |example|
 "screenshot_#{example.description.gsub(' ', '-').gsub(/^.*\/spec\//,'')}"
end

I saw it takes screenshots after whole scenario ends but not on the step fails. I want screenshots while steps are failing... Please help

fabdurso
  • 2,366
  • 5
  • 29
  • 55
SaeeK
  • 217
  • 1
  • 4
  • 12

2 Answers2

0

You can use after hook to do it. https://www.relishapp.com/rspec/rspec-core/v/2-2/docs/hooks/before-and-after-hooks

As mentioned there, after hook will be executed even if example fails

jelf
  • 68
  • 5
  • Actually, i tried to signout on each scenario, So if the scenario fail in middle, it never takes the screenshot of failure step rather it takes screenshot after log out. Either i need to find out a way to capture screenshot of failure steps or reset sessions. I had no luck as well for reset sessions using Capybara. – SaeeK Dec 14 '15 at 23:45
  • @Subash the test fails where it fails -- it doesn't continue running the rest. It will however run after hooks -- Are you trying to signout in an after hook? If so you'll need to check the order the after hooks are defined in, so that the after hook that takes the screenshot is called before the after hook that signsout (there really should be no need to signout every test as long as you've set up capybaras after hooks correctly - since resetting the session will have the same effect) – Thomas Walpole Dec 15 '15 at 00:07
  • Even though, i set up Capybara::Screenshot before After hook, it never capture the failure steps screenshot. As well reset sessions for capybara never reset sessions/delete cookies. I want to signout using After hook but want to make sure screenshot can be captured on failure steps. Please help – SaeeK Dec 15 '15 at 01:46
  • @Subash, specs are isolated, so you basicaly don't need to sign out. However, if you are using js specs (e.g. poltergeist, selenium) it could not be solved another way, because you never could be sure that spec fails in context where taking screenshot is possible. The only solution i can suggest is to use `around` hooks if you want something to be after screenshot done – jelf Dec 15 '15 at 03:14
  • Here's my code: After do |scenario| if scenario.failed? Capybara::Screenshot.register_filename_prefix_formatter(:rspec) do |example| "screenshot_#{example.description.gsub(' ', '-').gsub(/^.*\/spec\//,'')}" end step 'user sign out' end > Is this case, if any scenario fails/pass regardless, user logs out. But the issue is, if scenario fails, it never capture screenshots of failure steps rather capture the screenshots after sign out which is a sign out screen. I need to have failure step screenshot rather. Again i need to sign out after each scenario. – SaeeK Dec 15 '15 at 13:08
  • @Subash can you use backtics or even gist next time? Hard to read :( – jelf Dec 16 '15 at 07:11
  • @Subash why do you log out users in after hook? Does it check that user can log out after fail? – jelf Dec 16 '15 at 07:14
  • I need to log out after each scenario. Yes, Even the testcase is fail, it can log out. I modified the method like that to do. The problem is Capybara takes screenshot when it fails but waits until the scenario ends. So when the scenario ends, its already is log out page and Capybara takes the screenshot of it, which i don't want. I want the screenshot before it goes to log out page when the step fails. I believe now its clear to you. – SaeeK Dec 16 '15 at 14:49
  • @Subash it's not clear as to why a log out is required on each and every spec. Sessions are cleared after each spec is completed. You can test log out in a separate scenario on its own. – Phil Dec 17 '15 at 21:41
0

In your spec_helper, you can do set up your after hook to do something like this:

RSpec.configure do |config|
  config.after(:each) do |example|
    if example.exception
      file_name = 'failed_%s.png' % rand(1000).to_s # name your screenshot whatever you'd like
      page.save_screenshot(file_name)
    end
  end
end
Phil
  • 886
  • 7
  • 20