简体   繁体   中英

Using chrome for capybara, rails 4.2

I spend more than 2 days trying to figure how to use google chrome for capybara but no luck:( Trying to test a form button. The test that has js: true is failing. I did install firefox because as far as I searched, even though I want to use chrome, I need to have firefox install on the computer then make chrome default.(Please correct me if I'm wrong) I also installed chromedriver. Used to get some timing errors that keeps firefox opening and closing for so long but I have came this far:

My rails_helper:

ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rails'
require 'capybara'
require 'capybara/dsl'
require "selenium-webdriver"
require 'rails_helper'

ActiveRecord::Migration.maintain_test_schema!

Capybara.register_driver :selenium_chrome do |app|
 Capybara::Selenium::Driver.new(app, browser: :chrome)
 Selenium::WebDriver::Chrome.driver_path = '/Users/erincemer/Downloads/chromedriver'
end
(this is where I have chromedriver)
Capybara.javascript_driver = :selenium_chrome

RSpec.configure do |config|

  config.include CsvHelper
  config.include Capybara::DSL
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = false
  config.infer_spec_type_from_file_location!

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end
  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end
  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end
  config.after(:each) do
    DatabaseCleaner.clean
  end
end

and my test is:

describe "visit clearance_batches#new" do

    it 'should get an error message when tries to find an item with empty string', js: true do
      visit '/clearance_batches/new'

      fill_in 'id_field', with: ""
      click_button 'find item'
      expect(page).to have_content('*Id can not be blank!')
    end
   end

and my failure is :

Failures:

  1) visit clearance_batches#new should get an error message when tries to find an item with empty string
     Failure/Error: visit '/clearance_batches/new'
     NoMethodError:
       undefined method `needs_server?' for "/Users/erincemer/Downloads/chromedriver":String

I don't get where "needs_server?" is coming from , I dont have it anywhere in the app.

My test file is inside features folder. Adding

:type => feature

(which I dont know what it's for) to my test doesn't change anything. I have another test that has

visit '/clearance_batches/new' without js: true

and that doesn't give any error so that route is correct. I tried to be as specific as possible. Thanks for any help.

The block passed to register_server needs to return a Capybara::Driver::Base instance (which Capybara::Selenium::Driver derives from). By setting driver_path after creating the instance your block is actually returning a string. Either moving the Chrome.driver_path setting out of the block or swapping the order of the two lines inside the register_driver block will fix the issue you're having

Capybara.register_driver :selenium_chrome do |app|
  Selenium::WebDriver::Chrome.driver_path = '/Users/erincemer/Downloads/chromedriver'
  Capybara::Selenium::Driver.new(app, browser: :chrome)
end

You don't need to install Firefox if you're going to use Chrome, just chromedriver. If trying to use Firefox 48+ you need to install geckodriver.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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