mattheworiordan / capybara-screenshot

Automatically save screen shots when a Capybara scenario fails
MIT License
1.02k stars 168 forks source link

no implicit conversion of nil into String #177

Open petems opened 8 years ago

petems commented 8 years ago

Getting this error when trying to run screenshot_and_save_page:

Failure/Error: File.join(Sinatra::Application.root, capybara_tmp_path)

          TypeError:
            no implicit conversion of nil into String

In Pry, that value is nil for me:

[1] pry(#<RSpec::ExampleGroups::MainPage>)> Sinatra::Application.root
=> nil

Versions:

sinatra (1.4.7)
capybara (2.6.2)
capybara-screenshot (1.0.13)

Sinatra code:

require 'sinatra/base'
require 'sinatra/partial'
require 'haml'
require 'sass'
require 'json'

class Gsubular < Sinatra::Base
  register Sinatra::Partial

  set :root, File.dirname(__FILE__)

  get '/style.css' do
    headers 'Content-Type' => 'text/css; charset=utf-8'
    sass :style
  end

  get '/' do
    haml :index
  end

  # create
  post '/' do
    response = {}
    begin
      str = params[:test_string]
      regex_do_this = Regexp.new(params[:pattern])
      gsubbed_string = str.gsub!(regex_do_this, params[:replacement])
    rescue Exception => e
      response[:error] = e.message
      return response.to_json
    end
    response[:gsub_string] = []
    response[:gsub_string].push(gsubbed_string)
    response.to_json
  end

  get '/ping' do
    'pong'
  end

  run! if app_file == $0
end

Spec code:

require_relative '../spec_helper'

describe 'Main Page', :type => :feature, :js => true do

  it 'creates correct gsub when given correct syntax' do
    visit '/'
    fill_in 'test_string', :with => 'Sandwich'
    fill_in 'gsub_pattern_input', :with => '[wich]+'
    fill_in 'gsub_replacement_input', :with => 'castle'
    screenshot_and_save_page
    expect(page.find("#gsub_answer")).to have_content("Sandcastle")
  end
mattheworiordan commented 8 years ago

Well this doesn't look like a capybara screenshot issue per se, and seems like an issue with how you are using Sinatra. See https://github.com/sinatra/sinatra/blob/939ce04c1b77d24dd78285ba0836768ad57aff6c/lib/sinatra/base.rb#L1823, root should not be nil.

Also, try this:

$ ruby -e "require 'sinatra'; puts Sinatra::Application.root; exit"
/Users/mattheworiordan/Projects/Ably/demos/sinatra-chat

What happens you try that?

petems commented 8 years ago
ruby -e "require 'sinatra'; puts Sinatra::Application.root; exit"
/Users/petersouter/projects/gsubular
pwim commented 7 years ago

I also ran into this issue. I think in modular sinatra applications, Sinatra::Application.root is nil. I was able to work around this with

root = File.expand_path(File.join(File.dirname(__FILE__), "../tmp"))
Capybara::Screenshot.instance_variable_set :@capybara_root, root