Capybara-webkit cannot simulate a click on an element (non link / button)

In the rspec function script, I have:

scenario 'shows things', js: true do page.find('#my_div').trigger(:click) 

This is not like the jQuery handler fires the click event of this element. In the debugger, this call returns an empty string "".

If capybara-webkit can simulate mouse events using jQuery handlers that run ajax requests that update dom? If so, any suggestions on this case? I switched from selenium-webdriver because the capybara #trigger method is not supported.

And trying to fire a jQuery event directly throws an error:

 page.execute_script "$('#masthead .fclass').trigger('click')" 

gives

 Capybara::Webkit::InvalidResponseError: Javascript failed to execute 

In my gemfile there is

 group :test do gem 'capybara' gem "database_cleaner", "~> 0.9.1" gem 'capybara-webkit', git: 'git://github.com/thoughtbot/capybara-webkit.git' gem 'launchy' end 

I am using the 'edge' capybara-webkit, as I hit the error with xpath error otherwise (v2.04?).

I was not able to get the javascript error log, although I tried to add stack exceptions to spec / spec_helper.rb:

 ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'rspec/autorun' require 'capybara/rspec' require 'rack/utils' Capybara.app = Rack::ShowExceptions.new(MyApp::Application) # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} RSpec.configure do |config| #... Capybara.javascript_driver = :webkit config.before (scope = :suite) do %x[bundle exec rake assets:precompile] end config.use_transactional_fixtures = false config.before(:suite) do DatabaseCleaner.strategy = :truncation end config.before(:each) do DatabaseCleaner.start end config.before(:each) do DatabaseCleaner.clean end config.infer_base_class_for_anonymous_controllers = false end 

Update: I replaced capybara-webkit with

 gem 'poltergeist' 

And in spec / spec_helper.rb:

  require 'capybara/poltergeist' ... Capybara.register_driver :poltergeist do |app| Capybara::Poltergeist::Driver.new(app, { debug: true }) end Capybara.javascript_driver = :poltergeist Capybara.current_driver = :poltergeist 

I get these console error messages from the page ("# my_id"). trigger ('click'):

 ... {"name"=>"click", "args"=>[3, 2]} poltergeist [1366370900594] state default -> mouse_event poltergeist [1366370900596] state mouse_event -> loading Not allowed to load local resource: file:///home/me/rails/my_app/public/assets_test/application.css ... Not allowed to load local resource: file:///home/me/rails/my_app/public/assets_test/application.js 

I tried chmod poltergeist permissions, but the same result.

And trying to run page.execute_script (% Q ($ ("my_id"). Trigger ('click'))) gives an error like jQuery undefined:

  ReferenceError: Can't find variable: $ at phantomjs://webpage.evaluate():1 at phantomjs://webpage.evaluate():1 at phantomjs://webpage.evaluate():1 at phantomjs://webpage.evaluate():1 
+4
source share
1 answer

I had problems and solutions:

1) The Capybara trigger () method is not supported using selenium-webdriver or capybara-webkit.

  • Solution: fire the jquery event directly from javascript: page.execute_script(%Q($("#... , although this is not so useful as it can fire the event for an item that is not displayed to the user.

2) save_and_open_page does not use the asset pipeline, so it is useless if you have hidden elements that you can use to click. The poltergeist driver has save_screenshot, which works, but it is not as useful as having a real browser page with loaded resources.

  • solution: precompiling assets in the test, although I could not load jQuery with any driver ($ is undefined), and poltergiest / phantomjs does not allow access to local compiled files (command line options for the driver do not affect)

  • better solution: use unreleased Capybara 2.1+ gem 'capybara', git: 'git://github.com/jnicklas/capybara.git' , add Capybara.asset_host = "http://localhost:3000" in spec_helper.rb and leave the dev server running

3) capybara-webkit Error undefined method find_xpath for #<Capybara::Webkit::Driver When using Capybara.javascript_driver = :webkit

  • solution: use the main gem 'capybara-webkit', git: 'git://github.com/thoughtbot/capybara-webkit.git' branch gem 'capybara-webkit', git: 'git://github.com/thoughtbot/capybara-webkit.git'

My setup at the end was Poltergeist (supports trigger method) + Capybara beta with assets served from dev server (not precompiled), so save_and_open_page works.

+6
source

All Articles