$未在Selenium测试中定义错误

时间:2014-07-26 22:04:36

标签: jquery ruby-on-rails selenium selenium-webdriver capybara

我正在尝试使用Selenium在Rails 3.2应用程序中测试jQuery自动完成。在this blog post之后,我创建了一个fill_autocomplete辅助方法,如下所示:

def fill_autocomplete(field, options = {})
  fill_in field, with: options[:with]

  page.execute_script %Q{ $('##{field}').trigger('focus') }
  page.execute_script %Q{ $('##{field}').trigger('keydown') }
end

当我运行RSpec时,我收到此错误:

Failure/Error: fill_autocomplete 'crop', with: 'm'
 Selenium::WebDriver::Error::JavascriptError:
   $ is not defined

问题行是page.execute_script %Q{ $('##{field}').trigger('focus') }


spec_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'coveralls'
require 'simplecov'
require 'capybara'

include Warden::Test::Helpers

SimpleCov.configure do
  add_filter 'spec/'
end
Coveralls.wear!('rails')

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  config.use_transactional_fixtures = true

  config.infer_base_class_for_anonymous_controllers = false

  config.order = "random"

  config.include Devise::TestHelpers, :type => :controller
  config.extend ControllerMacros, :type => :controller
end

Gemfile的开发/测试组

group :development, :test do
  gem 'haml-rails'                     
  gem 'rspec-rails', '~> 2.12.1'       
  gem 'webrat'                         
  gem 'capybara', '~> 2.4.1'            
  gem 'selenium-webdriver', '~> 2.42.0' 
  gem 'factory_girl_rails', '~> 4.0'    
  gem 'coveralls', require: false      
end

我尝试降级RSpec,Capybara和Selenium-Webdriver。我尝试过使用Capybara-Webkit代替Selenium。一切都没有用。

我一直在假设jQuery自动完成挂钩到'keydown'事件,并且Capybara的fill_in不会自己触发此事件。它是否正确?

欢迎任何解决方案或解决方法。谢谢!

1 个答案:

答案 0 :(得分:2)

感谢Arran的建议,我意识到在定义JQuery之前,JS中的其他地方发生了错误。