【问题标题】:“including Capybara::DSL in the global scope is not recommended!” want to remove it. console warning“不建议在全局范围内包含 Capybara::DSL!”想删除它。控制台警告
【发布时间】:2020-03-16 15:36:59
【问题描述】:

解决错误访问未找到。我在我的一个辅助模块中包含了 Capybara::DSL,如下所示: 我正在使用 ruby​​ 2.7.0

include Capybara::DSL

module LoginHelper
  def self.login_user
    visit 'https://staging.have2have.it/login'  
    within(".container-fluid") do
      fill_in("email", with: 'shinsaurab@gmail.com', :match => :prefer_exact)
      fill_in("password", with: '123', :match => :prefer_exact)
    end
    click_button('Log In')
  end
end

spec_helper.rb

require 'capybara'
require 'capybara/dsl'
require 'capybara/rspec'
require './spec/helpers/login_helper'

Capybara.default_driver = :selenium

RSpec.configure do |config|
  config.include Capybara::DSL
  config.include LoginHelper
end

如果我做错了什么,任何人都可以提出建议。我尝试了一些建议,但对我不起作用

【问题讨论】:

    标签: ruby capybara integration-testing


    【解决方案1】:

    我遇到了类似的问题,尝试了很多方法,但对我有用的是从 spec_helper 中删除 config.include Capybara::DSL 并将 LoginHelper 包含在 Helpers 模块中。在您的情况下,它们可能如下所示:

    login_helper.rb

    module Helpers
      module LoginHelper
        def login_user
          visit 'https://staging.have2have.it/login'  
          within(".container-fluid") do
            fill_in("email", with: 'shinsaurab@gmail.com', :match => :prefer_exact)
            fill_in("password", with: '123', :match => :prefer_exact)
          end
          click_button('Log In')
        end
      end
    end
    

    spec_helper 看起来像这样:

    require 'capybara'
    require 'capybara/dsl'
    require 'capybara/rspec'
    require './spec/helpers/login_helper.rb'
    
    Capybara.default_driver = :selenium
    
    RSpec.configure do |config|
      config.include Helpers::LoginHelper
    end
    

    谢谢! 如果您有任何疑问,请告诉我

    【讨论】:

    • 谢谢!这对我有用,但我不知道为什么?你能解释一下为什么我们删除了 config.include Capybara::DSL。
    猜你喜欢
    • 1970-01-01
    • 2014-10-02
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 2015-11-24
    • 1970-01-01
    相关资源
    最近更新 更多