在rspec的功能的情況,我有:水豚-的WebKit不能模擬點擊(非鏈接/按鈕)元素
scenario 'shows things', js: true do
page.find('#my_div').trigger(:click)
這似乎並不火jQuery的處理程序是此元素的click事件。在調試器中,這個調用返回一個空字符串「」。
capybara-webkit應該能夠通過jQuery處理程序來觸發ajax請求來更新dom來模擬鼠標事件嗎?如果是這樣,這個案件的任何建議? 我從selenium-webdriver切換,因爲不支持水豚#trigger方法。
,並試圖觸發jQuery的事件直接引發一個錯誤:
page.execute_script "$('#masthead .fclass').trigger('click')"
給
Capybara::Webkit::InvalidResponseError:
Javascript failed to execute
我的Gemfile有
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
我使用的是 '邊緣' capybara- webkit,因爲我遇到了xpath錯誤,否則(v2.04?)。
我一直沒能得到一個日誌的JavaScript錯誤的,雖然我嘗試添加在規格/ 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
更新: 我代替水豚,用的WebKit
gem 'poltergeist'
而且在規格/ 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
我從頁(「#添加my_id」)的控制檯中看到這些錯誤信息的觸發(「點擊」):
...
{"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
我嘗試和chmod的吵鬧鬼寶石權限,但相同的結果。
,並試圖運行page.execute_script(%Q($( 「添加my_id」)觸發( '點擊'))) 給出了一個jQuery未定義的錯誤類型:
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
選擇器內是否有空元素?這可能是這個bug: https://github.com/thoughtbot/capybara-webkit/issues/494 – RobinGower
poltergeist是一個無頭瀏覽器,你有沒有試過在瀏覽器中打開這個頁面,以便你可以看到哪些元素是在頁面上?您可以簡單地在您的功能中調用「save_and_open_page」。 – Mattherick