capybara-screenshot icon indicating copy to clipboard operation
capybara-screenshot copied to clipboard

Screenshots not being auto generated with Ruby-Rspec (no rails)

Open zwarburg opened this issue 6 years ago • 5 comments

I am using Ruby Rspec to test a webapp that doesn't use rails, thus I don't have a rails_helper.rb, just a spec_helper.rb. I can manually trigger a screenshot but I can't figure out how to trigger the screenshots on a failure.

spec_helper.rb:

# frozen-string-literal: true
require 'rspec'
require 'capybara/rspec'
require 'capybara-screenshot/rspec'
require 'capybara/dsl'
require 'selenium-webdriver'
require 'site_prism'

Dir[File.dirname(__FILE__) + '/page_objects/*/*.rb'].each do |page_object|
  require page_object
end

Capybara.register_driver :selenium do |app|
 Capybara::Selenium::Driver.new(app, browser: :chrome)
end

Capybara.save_path = "#{Dir.pwd}/screenshots"

Capybara.default_driver = :selenium
Capybara.app_host = "REDACTED"
Capybara.default_max_wait_time = 20

my_spec.rb

describe 'spec' do
    before(:each) do
      @my_page = login_and_open_my_page
    end
    it "fails" do
       expect(5).to eq(6)
    end
end

zwarburg avatar May 08 '18 21:05 zwarburg

I have this set:

Capybara::Screenshot.autosave_on_failure = true

ndp avatar May 10 '18 19:05 ndp

@ndp tried that too.

zwarburg avatar May 14 '18 18:05 zwarburg

@zwarburg I ran into this as well and noticed that it seems to only catch feature specs. I was also using describe, but switching to rspec feature syntax made it work for me.

aterris avatar Jun 20 '18 13:06 aterris

@zwarburg if you can provide me with a working example i.e. one that I can drop in the Gem, I can see if I can find a more permanent fix or perhaps update the docs to indicate what people should do.

mattheworiordan avatar Jul 31 '18 21:07 mattheworiordan

I believe this is working as expected. describe is part of the RSpec library, not Capybara. That's why Capybara hooks are not being triggered on failures.

If you want a different behavior, you can tag a describe block with type: :feature.

describe 'spec', type: :feature do
    before(:each) do
      @my_page = login_and_open_my_page
    end
    it "fails" do
       expect(5).to eq(6)
    end
end

For more information, here's the documentation: https://github.com/teamcapybara/capybara#using-capybara-with-rspec

skmichaelson avatar Mar 05 '19 01:03 skmichaelson