onetwopunch onetwopunch - 3 months ago 22
Javascript Question

How to open a browser in Capybara and Selenium

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 ' 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


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}

Answer

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 '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

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.