我对Capybara很新,也从未使用过Selenium。我在MacOSX上的rails项目上做了一个ruby,无论出于何种原因,当我运行测试时,浏览器窗口永远不会打开。 我的堆栈是:Capybara,Selenium,Rspec,Ruby on Rails。 我的测试如下:
describe 'Downloads', js: true do
context ' compress zip and download file' do
before do
Capybara.current_driver = :selenium
session = Capybara::Session.new(:selenium)
session.visit '/users/sign_in'
find('#tab_signin').click
within("#new_user") do
fill_in 'user_login', :with => 'admin@local.host'
fill_in 'user_password', :with => 'password'
end
click_button 'Sign in'
end
it 'downloads the project and navigates to downloads page' do
visit 'some/path'
within '.create-download' do
find(:select).find("option[value='zip']").select_option
end
sleep 3
page.should have_css('#download-modal.in')
end
端 端
我还尝试更改我的features / support / env.rb中的内容:
Capybara.javascript_driver = :selenium
Capybara.register_driver :selenium do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
Capybara::Selenium::Driver.new(app, :browser => :firefox, :profile => profile)
end
更新
浏览器不仅没有打开,而且测试失败,输出如下:
Failure/Error: visit '/users/sign_in'
ArgumentError:
unknown option: {:resynchronize=>true}
答案 0 :(得分:4)
经过大量工作后我终于明白了。感谢@RAJ建议放置配置信息的位置。 feature / support / env.rb用于黄瓜,我正在使用rspec。
我读到的关于selenium和capybara的大部分文章告诉我在块的开头使用js: true
选项,但这不起作用。一旦我将其更改为feature: true
它就有效了。我的最终解决方案如下:
describe 'Downloads', feature: true do
context 'compress zip and download file' do
before do
visit '/users/sign_in'
find("a[href$='signin']").click
within("#new_user") do
fill_in 'user_login', :with => 'admin@local.host'
fill_in 'user_password', :with => 'password'
end
click_button 'Sign in'
end
it 'downloads the project and navigates to downloads page' do
visit 'some/path'
within '.create-download' do
find(:select).find("option[value='zip']").select_option
end
sleep 3
page.should have_css('#download-modal.in')
end
end
end
然后我的spec_helper.rb看起来像:
Capybara.register_driver :selenium do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
Capybara::Selenium::Driver.new( app, :profile => profile)
end
Capybara.default_wait_time = 10
Capybara.current_driver = :selenium
Capybara.app_host = 'http://localhost:3000'
我做过的另一件事我之前并不知道是在Firefox上安装Selenium IDE。由于我之前从未使用过Selenium,我认为我所需要的只是宝石。