最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to open a browser in Capybara and Selenium - Stack Overflow

programmeradmin2浏览0评论

I'm very new to Capybara and have also never used Selenium before. I'm doing a ruby on rails project on MacOSX and for whatever reason, a browser window never opens when I run my test. My stack is: Capybara, Selenium, Rspec, Ruby on Rails. My test is as follows:

describe 'Downloads', js: true do

context ' press 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 => '[email protected]'
      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

I've also tried to change stuff in my features/support/env.rb to be this:

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

Update

Not only is the browser not opening, but the test is failing with the following output:

Failure/Error: visit '/users/sign_in'
 ArgumentError:
   unknown option: {:resynchronize=>true}

I'm very new to Capybara and have also never used Selenium before. I'm doing a ruby on rails project on MacOSX and for whatever reason, a browser window never opens when I run my test. My stack is: Capybara, Selenium, Rspec, Ruby on Rails. My test is as follows:

describe 'Downloads', js: true do

context ' press 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 => '[email protected]'
      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

I've also tried to change stuff in my features/support/env.rb to be this:

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

Update

Not only is the browser not opening, but the test is failing with the following output:

Failure/Error: visit '/users/sign_in'
 ArgumentError:
   unknown option: {:resynchronize=>true}
Share Improve this question edited Aug 4, 2014 at 21:28 onetwopunch asked Aug 4, 2014 at 20:23 onetwopunchonetwopunch 3,3492 gold badges33 silver badges44 bronze badges 3
  • move features/support/env.rb content in spec/spec_helper.rb – RAJ Commented Aug 4, 2014 at 20:24
  • possible duplication: stackoverflow./questions/17030455/… – Filip Bartuzi Commented Aug 4, 2014 at 20:47
  • Are your tests failing or just not opening a browser? – jkeuhlen Commented Aug 4, 2014 at 20:47
Add a ment  | 

1 Answer 1

Reset to default 6

So after a lot of work I finally figured it out. Thanks to @RAJ for the suggestion of where to put that config info. The feature/support/env.rb is for cucumber and I'm using rspec.

Most of the articles I read about selenium and capybara told me to use the js: true option at the start of the block, but that didn't work. Once I changed that to feature: true it worked. My final solution looks like this:

describe 'Downloads', feature: true do

context 'press 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 => '[email protected]'
    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

Then my spec_helper.rb looks like:

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'

Another thing that I did and I wasn't aware before was install the Selenium IDE on Firefox. Since I'd never used Selenium before, I thought all I needed was the gem.

发布评论

评论列表(0)

  1. 暂无评论