ruby-on-rails-4capybaraintegration-testingcapybara-webkit

Capybara::Driver::Base#evaluate_script Error After Setting Javascript Driver


I'm using Capybara and Minitest to test my Rails 4 application. We have a page I'm trying to test that loads a Google Map and colorbox via AJAX.

We need to make sure AJAX is loaded before checking for a certain element and are trying to follow this tutorial: https://thoughtbot.com/blog/automatically-wait-for-ajax-with-capybara

Most of the tutorials are using RSpec, so I'm putting the code in test_helper.rb:

class ActionDispatch::IntegrationTest
  # Make the Capybara DSL available in all integration tests
  include Capybara::DSL
  include Warden::Test::Helpers
  Warden.test_mode!

  Capybara.default_max_wait_time = 5
  Capybara.javascript_driver = :webkit

  def wait_for_ajax
    page.evaluate_script("jQuery.active") == 0
    yield
  end
end

Looking through other people that are getting the error, it appears they didn't have the capybara-webkit gem installed. However, we do:

Running bundle install:

...
Using capybara 2.5.0
Using capybara-webkit 1.7.1

Here is the test I'm failing on:

test "colorbox should appear on pageload" do
    visit trips_new_path
    wait_for_ajax do
      assert_selector "#colorbox", "colorbox not created on trips/new"
    end
 end

And I'm receiving the error:

Capybara::NotSupportedByDriverError:         Capybara::NotSupportedByDriverError: Capybara::Driver::Base#evaluate_script
            test/test_helper.rb:35:in `wait_for_ajax'
            test/integration/trip_creation_test.rb:17:in `block in <class:TripCreationTest>'

I've tried setting the JavaScript in a few different places but it still seems not to work.

Update: Tried additional syntax and getting the same error:

  def wait_for_ajax
    Capybara.javascript_driver = :webkit
    Timeout.timeout(Capybara.default_wait_time) do
      loop until finished_all_ajax_requests?
    end
  end

  def finished_all_ajax_requests?
    page.evaluate_script('jQuery.active').zero?
  end

Thank you in advance for your help!


Solution

  • The fact you're getting a NotSupportedByDriver error means your test isn't actually using the capybara-webkit driver and probably is still using the default racktest driver. You need to specify that a specific test is supposed to use the JavaScript capable driver. If you're using something like https://github.com/wojtekmach/minitest-capybara you can look through the gems README to see examples of how to specify it manually, or to set up so you can tag tests with metadata. Once you have the test actually using the correct driver you will probably find you don't actually need the wait_for_ajax method since Capybaras finders will then automatically wait for the content to appear.