繁体   English   中英

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

[英]How to open a browser in Capybara and Selenium

我对水豚非常陌生,也从未使用过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}

因此,经过大量的工作,我终于弄清楚了。 感谢@RAJ关于将配置信息放在何处的建议。 feature / support / env.rb适用于黄瓜,我正在使用rspec。

我读到的有关硒和水豚的大多数文章都告诉我在代码块的开头使用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。 因为我以前从未使用过硒,所以我认为我所需要的只是宝石。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM