开发者

How do I set the browser language in Cucumber / Capybara?

I'd like a different language to be displayed depending on the browser language and naturally I'd like to test to ma开发者_运维百科ke sure it's working properly. I'm using Cucumber and Capybara, Gem versions 1.0.6 and 1.1.1 respectively.

I've found a few suggestions on the web, but none that work so far. I've tried the following (individually) without success.

page.driver.language = language
header "Accept-Language", language
page.driver.header 'Accept-Language', language

Where language is set to either "en" or "fr".

Can anyone help? Thanks, Graeme.


Got it!

I had the right line, the problem was elsewhere.

page.driver.header 'Accept-Language', language

This works fine, the problem was that my Scenario was as follows.

Scenario:
  Given I am an anonymous user
  And the browser language is fr
  Then I should see "Bonjour"

And it should have been

Scenario:
  Given the browser language is fr
  And I am an anonymous user
  Then I should see "Bonjour"

The 'I am an anonymous user' step was just doing a sign out first. It would seem that the browser language must be set before any other steps are taken which use the browser.


With poltergeist:

page.driver.add_headers('Accept-Language' => 'fr')

More details: https://github.com/teampoltergeist/poltergeist#manipulating-request-headers


What Capybara driver are you using? This works for Mechanize:

When /^I set my browser language to French$/ do
    page.driver.agent.request_headers['Accept-Language'] = 'fr'
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜