Capybara Rspec 在使用 Chromedriver 时运行方式不同

问题描述 投票:0回答:0

我有一个有效的 Rspec 测试:

scenario "Fans in Canada can sign in, visit signed in home page, and log out in" do

    create_fan_in_canada

    visit root_path

    within('#myNavbar') do
      click_link('LOG IN')
    end  

    fill_in "Email", with: @fan.email
    fill_in "Password", with: @fan.password
    click_button "Sign in"
    find('.dropdown-toggle').click
    expect(page).to have_link "Logout"
    expect(page).to have_current_path root_path
    

错误:

但是后来我尝试添加到测试中,使其打开一个模式,这需要场景中的 :js => true 。突然,Capybara 以同样的方式找不到 #myNavbar(click_link 不起作用),并且 factorybot/devise 用户会话停止工作(无法登录 Factorybot - 电子邮件或密码无效) 。所以这是导致这些问题的更新场景:

scenario "Fans in Canada can sign in, visit signed in home page, and log out in", :js => true do

    create_fan_in_canada

    visit root_path

    #HAVE TO CHANGE click_link CODE TO THIS BRITTLE because click_link stopped working with chromedriver:
    find(:css, 'body > header > div > div.navbar-header > button').click
    find(:css, '#myNavbar > ul.nav.navbar-nav.navbar-right.signlogin > li:nth-child(2) > a').click

    fill_in "Email", with: @fan.email
    fill_in "Password", with: @fan.password
    click_button "Sign in"

    find('.dropdown-toggle').click
    expect(page).to have_link "Logout"
    expect(page).to have_current_path root_path

    # ... TEST THAT ALL MODALS ARE LOADING

    find('#actionLink').click
    within('#actionModal') do
      page.should have_content('#fan1')
    end
    expect(page).to have_css '#fan1'

rails_helper

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
require "devise"
require "pundit/rspec"
require 'capybara/rails'

Dir[Rails.root.join('spec/support/*.rb')].each { |f| require f }

# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

Capybara.register_driver :selenium_chrome do |app|
  Capybara::Selenium::Driver.new(app, browser: :chrome)
end

Capybara.javascript_driver = :selenium_chrome

RSpec.configure do |config|
  config.include Devise::Test::ControllerHelpers, type: :controller

  # controller spec macros
  config.extend ControllerMacros, :type => :controller

  # Include Factory Girl syntax to simplify calls to factories
  config.include FactoryGirl::Syntax::Methods 
  
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = false

  config.infer_spec_type_from_file_location!
  config.include Rails.application.routes.url_helpers
  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
  # arbitrary gems may also be filtered via:
  # config.filter_gems_from_backtrace("gem name")
  config.include Warden::Test::Helpers, type: :feature
  config.after(type: :feature) { Warden.test_reset! }
  config.include Capybara::DSL
  config.filter_run_when_matching focus: true

end

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end
module ControllerMacros
    def login_fan(fan=nil)
        before(:each) do
        if :fan
              @request.env["devise.mapping"] = Devise.mappings[:fan]
          sign_in (fan || create(:fan))
        else
          @request.env["devise.mapping"] = Devise.mappings[:us_fan]
          sign_in (fan || create(:fan))
        end
        end
    end

    def logout_fan(fan=nil)
      after(:each) do
        logout (fan)
      end
    end

    def create_site_counter
      before(:each) do
        create :site_counter
      end
    end
  end

回顾一下,我遇到登录错误(电子邮件或密码错误),而如果没有 chromedriver,它可以正常登录,而且我还需要更改代码才能单击相同的链接(方便的“click_link”不会)工作)。当我检查时,factorybot 用户 @fan 仍然像往常一样存在,但现在由于某种原因(密码或电子邮件无效)而无法登录,可能是因为 chromedriver 和 devise 之间存在一些设置问题。

所以我在某处遗漏了一些东西

ruby-on-rails rspec selenium-chromedriver capybara
© www.soinside.com 2019 - 2024. All rights reserved.