【问题标题】:How to open a browser in Capybara and Selenium如何在 Capybara 和 Selenium 中打开浏览器
【发布时间】:2014-09-27 09:56:16
【问题描述】:

我对 Capybara 很陌生,以前也从未使用过 Selenium。我正在 MacOSX 上做一个 ruby​​ on rails 项目,无论出于何种原因,当我运行测试时,浏览器窗口永远不会打开。 我的堆栈是: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}

【问题讨论】:

  • features/support/env.rb的内容移动到spec/spec_helper.rb
  • 你的测试是失败了还是没有打开浏览器?
  • @jkeuhlen 请查看更新。

标签: javascript ruby-on-rails selenium rspec capybara


【解决方案1】:

所以经过大量的工作,我终于弄明白了。感谢@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,所以我认为我需要的只是宝石。

【讨论】:

  • 我能够使用js: true 标签让它工作,虽然我不确定为什么它对我有用,而不是你的设置。 ¯_(ツ)_/¯ 你的 spec_helper.rb 代码对我有用,所以谢谢你!
  • @mycargus 很高兴听到!
  • 注册驱动程序后,当我尝试运行任何测试时出现此错误。知道为什么会这样吗? * ArgumentError:未知选项:{:profile=>#<:webdriver::firefox::profile:0x00000006984468>}
  • @mickael 在实例化 Web 驱动程序时听起来好像有一个未知选项。虽然评论不是这个论坛。如果您无法弄清楚,您可能应该问一个正确的问题。
猜你喜欢
  • 2016-07-31
  • 1970-01-01
  • 2018-02-01
  • 1970-01-01
  • 2018-03-01
  • 2017-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多