How to load CSS and JS in Capybara?

2.3k views Asked by At

I'm trying to write integration tests for my Rails application. Its GUI is pretty complex, so it needs lots of integration tests (as opposed to controller or unit tests) to make sure all the AJAX requests and DOM rendering work correctly.

I've tried Capybara, Capybara-webkit, Poltergeist, and Chromedriver - but none of them are executing any of my CSS or document.ready functions.

I've done an extensive amount of reading and tried many solutions, but am having no success. I think it may be something to do with the asset pipeline.

I'm not sure where to look next to troubleshoot the issue.

Thanks, Louise

1

There are 1 answers

3
Louise On

In my case, it did have something to do with the asset pipeline.

After I turned off asset concatenation in test.rb, it all worked fine.

test.rb:

# Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

spec_helper:

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

Capybara.javascript_driver = :chrome
Capybara.asset_host = "http://localhost:3000"

Sorted. I needed to switch to chromedriver to find this out - it depended on me actually being able to watch the test driver do its job in a real browser, and open the Chrome javascript console (while capybara was paused in the debugger) to poke around and see what was going on.