如何在Capybara和Selenium中打开浏览器

one*_*nch 5 javascript selenium rspec ruby-on-rails capybara

我是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
Run Code Online (Sandbox Code Playgroud)

结束

我还尝试将我的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
Run Code Online (Sandbox Code Playgroud)

更新

浏览器不仅没有打开,而且测试失败,输出如下:

Failure/Error: visit '/users/sign_in'
 ArgumentError:
   unknown option: {:resynchronize=>true}
Run Code Online (Sandbox Code Playgroud)

one*_*nch 6

所以经过大量的工作,我终于想通了。感谢@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
Run Code Online (Sandbox Code Playgroud)

然后我的 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'
Run Code Online (Sandbox Code Playgroud)

我之前做过的另一件事是在 Firefox 上安装 Selenium IDE。因为我以前从未使用过 Selenium,所以我认为我需要的只是 gem。