Using Capybara to test a clean JavaScript application

I'm having trouble using Sinatra with Capybara.

I want to check a clean javascript application. This is just a simple index.html that is maintained by Sinatra.

require "sinatra" get "/" do File.read("public/index.html") end 

Say, for example, that I want to test this code.

 $("a.link").click(function(){ $(this).replaceWith("New String"); }); <a href="#link" class="link">Click me!</a> 

Then the test will look something like this.

 describe "requests", js: true do it "should display a message" do visit "/" click_link "Click me!" page.should have_content("New String") end end 

The problem is that nothing is happening. According to Ryan Bates screencast, Firefox should launch and run the test if js: true added to the describe block.

Here is my spec_helper file.

 require "rspec" require "capybara" require "capybara/dsl" Capybara.javascript_driver = :selenium require_relative "./../server" Capybara.app = Sinatra::Application Capybara.javascript_driver = :selenium Capybara.default_wait_time = 10 RSpec.configure do |config| config.mock_with :rspec config.include Capybara end 

Here is the result when rspec rspec/request_spec.rb starts.

 requests should display a message (FAILED - 1) Failures: 1) requests should display a message Failure/Error: page.should have_content("New String") expected #has_content?("New String") to return true, got false # ./spec/request_spec.rb:5:in `block (2 levels) in <top (required)>' Finished in 4.38 seconds 1 example, 1 failure Failed examples: rspec ./spec/request_spec.rb:2 # requests should display a message 

I created a complete example project on Github, which can be found here: https://github.com/oleander/capybara-js-fails

Does anyone know why it fails?

+4
source share
1 answer

Here is the original answer from Jonas Nicklas .

You need to specify "capybara / rspec" and set: type =>: request.
See the Capybara README Section "Using Capybara with RSpec".
/ Jonas

Here is a working Github example.

+4
source

All Articles