← Back to Upcase

Capybara-webkit and Middleman

(Joshua Steele) #1

I’ve recently deployed a simple static site built with Middleman, and now I’m trying to add some basic feature specs. In particular, I’d like to test my Javascript-related features using capybara-webit. As a first try, I’ve got a very simple smoke test that just checks for JS errors:

describe "home page", type: :feature do      
  before do
    visit "/"
  end

  # other specs...

  it "should not have JavaScript errors", :js => true do
    expect(page).not_to have_errors
  end
end

Although the actual site works fine, the test fails. As best I can tell, this is because the Javascript is not actually being loaded in the browser. Here is my test output:

Failures:

  1) home page should not have JavaScript errors
     Failure/Error: expect(page).not_to have_errors

       Expected no Javascript errors, got:
         - ReferenceError: Can't find variable: $
         - ReferenceError: Can't find variable: $
         - ReferenceError: Can't find variable: $
         - ReferenceError: Can't find variable: $
         - ReferenceError: Can't find variable: $
         - ReferenceError: Can't find variable: jQuery
         - ReferenceError: Can't find variable: $
         - ReferenceError: Can't find variable: $
         - ReferenceError: Can't find variable: $
         - ReferenceError: Can't find variable: $
     # ./spec/features/index_spec.rb:16:in `block (2 levels) in <top (required)>'

Also, here’s the contents of my spec_helper.rb file:

require "middleman"
require "rspec"
require "capybara/rspec"
require "capybara-webkit"
require "capybara/webkit/matchers"

Capybara.javascript_driver = :webkit

Capybara.app = Middleman::Application.server.inst do
  set :root, File.expand_path(File.join(File.dirname(__FILE__), ".."))
  set :environment, :development
  set :show_exceptions, false
end

Capybara::Webkit.configure do |config|
  config.block_unknown_urls
end

You can see the full feature branch on Github here:
https://github.com/joshukraine/cmoproject.org/tree/rspec

Thanks for any input!

(Chris Toomey) #2

Hi @joshukraine, I love the attention to detail and the Middlemaning!

That said, I personally have never felt the need to test a static site built with Middleman. If you had more complex JS interactions, I would want to extract those into testable JavaScript units and test those via direct JS. When I think of Capybara, almost always I image dynamic data and multi-page interactions.

Sorry to not have a better answer to your specific question, but hope that helps give some clarity.

(Joshua Steele) #3

Understood, thanks.