19
votes

I just had my capybara updated to 2.0, and all of my save_and_open_page calls return an html page without styling. It has the style sheets links properly at the top of the page. When I revert back to capybara 1.3, the styles work again. Anyone know how to fix this, and/or what the problem is?

thanks, Dave


Update 1.

hack:

I have found a reasonable way to get around the problem following the links recommended by simonmorley: Capybara Webkit problem which points to: capybara-screenshot

It involves using the capybara-screenshot gem to get a screenshot of what the page looks like. When you combine it with the save_and_open_page, which generates the html, you can see what it looks like, and see the HTML.

    save_and_open_page
    Capybara::Screenshot.screenshot_and_open_image

If I could get Capybara::Screenshot.screenshot_and_save_page to work, then I think that I might have a solution (if the name implies the action I think it does). However, when I try it, I get cannot load such file -- capybara/util/save_and_open_page


Update 2.

!! wait, the screenshot_and_open_image is not working now (no png image created or displayed). !!


Update 3.

Test App:

I created a test app, and posted it at test_capybara_screenshot on Github.

When I run this test app in development, the page come up with the blaring red background style displayed. When I run the tests, the only thing that works for me is the save_and_open_page, except there is no styling.

Note, when I run any tests, I get the following warning:

WARNING: Nokogiri was built against LibXML version 2.7.8, but has dynamically loaded 2.7.3

Details of the rspec test:

  • The save_and_open_page comes up with a boring white background (no styling).
  • the screenshot_and_open_image returns with the error:

Rack::Test capybara driver has no ability to output screen shots. Skipping.

Failure in opening /~/Documents/experiments/test_capybara_screenshot/tmp/capybara/screenshot-2012-11-26-07-48-29.png with options {}: No application found to handle '/~/Documents/experiments/test_capybara_screenshot/tmp/capybara/screenshot-2012-11-26-07-48-29.png'

  • The screenshot_and_save_page returns:

An error occurred in an after hook

LoadError: cannot load such file -- capybara/util/save_and_open_page occurred at ~/.rvm/gems/ruby-1.9.3-p125@global/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:251:in `require'


2
Do you have launchy installed??simonmorley
I don't think you should get styles, because the CSS links will be broken.sevenseacat
What does that mean "the CSS links will be broken"?Taylored Web Sites
Your CSS links will all be in the format /assets/blah.css, and when you save and open the page it opens a raw HTML file from your tmp folder... what CSS files should it be pointing to?sevenseacat
There is a difference in the CSS links. The 1.3 links point to the css file using the full disk path, followed by the querystring version number. The 2.0 link is relative from the public directory. Note, this app was generated pre asset pipeline.Taylored Web Sites

2 Answers

27
votes

Try adding the following to your gem file

group :test do
  gem "capybara"
  gem "launchy"
end

save_and_open_page won't work without this I don't believe.

-- Updated --

I've tested this for you and when I used the save and open method, I get the same error. In my set up, this is because my page is referencing:

<link href="/assets/application.css" media="screen" rel="stylesheet" type="text/css" />

This happens in Firefox, Chrome and Safari. According to the inspector, none of the files are found.

I haven't looked into this in too much detail and don't have an answer why it's not working. However there are a number of people struggling with this also:

https://github.com/jnicklas/capybara/issues/485

And, a bit more information about capybara and asset precompilation:

https://github.com/jnicklas/capybara/pull/609

And what might be a fix here:

https://groups.google.com/forum/#!msg/ruby-capybara/SBCor8UUj7w/hv97EgUQ1P4J

However, if you just want to see your page you could try this: delete scrap save_and_open_page and change your tests to use javascript. This way, my pages open fine.

 require 'spec_helper'

 describe "Foo" do
   it "should fail if Bar is lactose intolerant", :js => true do
     # .....
   end
 end

Try that and let me know if that helps.


response (From Taylored Web Sites):

I see the page for a few seconds, then it goes away. Is there a way for the browser window to not close?

7
votes

I was able to fix the problem with save_and_open_page after some blog browsing and digging. I have dropped the capybara_screenshot gem from my project. You can see my working code on my github test_capybara_screenshot repository. The solution that I figured out uses some pointers that I found on the capybara github site.

Assumptions:

  • You are using rspec for testing.
  • Your app is already configured for using the asset pipeline.

The first thing that I do is set it up so that assets are precompiled into a test directory. I do this by adding the following code into the spec/spec_helper.rb file within the RSpec.configure loop:

config.before (scope = :suite) do
  %x[bundle exec rake assets:precompile]
end

I specify where the assets are pre-compiled to in the config/environments/test.rb file:

config.assets.prefix = "assets_test"    # place test assets in public/assets_test directory
config.action_controller.asset_host = "file://#{::Rails.root}/public"

This makes it so that the test assets are independent of the development assets, and are only generated during a test suite run.

If you do this, you will probably want to have git ignore the /public/assets* directories.