简体   繁体   中英

How can I set http header for all capybara features

I use rspec, capybara. I set locale from http header as in bellow

  before_filter :set_locale

  def extract_locale_from_accept_language_header
    request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
  end

  def set_locale
    return I18n.locale = current_user.locale if user_signed_in?
    I18n.locale = extract_locale_from_accept_language_header || I18n.default_locale
  end

When I run my feature test I get error 'undefined method scan for NilClass'. Apparently capybara don't set http headers.

How I can set http header for all my features or avoid this by another way?

depending on your browser driver, you can set headers globally like this:

  Capybara.current_session.driver.headers = { 'Accept-Language' => 'de' }
  Capybara.current_session.driver.header('Accept-Language', 'de')

You can set your headers like this:

RSpec.configure do |config|
  config.before(:each) do
    page.driver.header 'Accept-Language', 'de'
  end
end

Source: https://github.com/thoughtbot/capybara-webkit#non-standard-driver-methods

header: set the given HTTP header for subsequent requests

page.driver.header 'Referer', ' https://www.thoughtbot.com '

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