save_and_open_page not working with capybara 2.0?

后端 未结 2 1368
太阳男子
太阳男子 2021-02-12 14:47

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

相关标签:
2条回答
  • 2021-02-12 15:08

    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.

    0 讨论(0)
  • 2021-02-12 15:14

    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?

    0 讨论(0)
提交回复
热议问题