简体   繁体   English

将铬用于水豚,护栏4.2

[英]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: 我花了两天多的时间试图弄清楚如何使用Google chrome来显示水豚但没有运气:(试图测试表单按钮。具有js:true的测试失败了。我确实安装了firefox,因为据我搜索,虽然我想使用chrome,但我需要在计算机上安装Firefox,然后将chrome设置为默认值。(如果我输入错了,请更正我)我还安装了chromedriver。很长一段时间,但我走了这么远:

My rails_helper: 我的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 描述“访问clearance_batches#new”

    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?" 我不知道哪里“ needs_server?” is coming from , I dont have it anywhere in the app. 来自,我在应用程序中的任何地方都没有它。

My test file is inside features folder. 我的测试文件位于features文件夹中。 Adding 添加

:type => feature :type =>功能

(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 在没有js的情况下访问'/ clearance_batches / new':是

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). 传递给register_server的块需要返回Capybara :: Driver :: Base实例(Capybara :: Selenium :: Driver派生自该实例)。 By setting driver_path after creating the instance your block is actually returning a string. 通过在创建实例后设置driver_path ,您的块实际上正在返回一个字符串。 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 Chrome.driver_path设置移出该块或交换register_driver块中两行的顺序将解决您遇到的问题

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. 如果要使用Chrome,则无需安装Firefox,只需安装chromedriver。 If trying to use Firefox 48+ you need to install geckodriver. 如果尝试使用Firefox 48+,则需要安装geckodriver。

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

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