如何在 Cucumber / Capybara 中设置浏览器语言?

发布于 2024-12-05 11:11:21 字数 335 浏览 2 评论 0原文

我希望根据浏览器语言显示不同的语言,当然我想进行测试以确保它正常工作。我使用的是 Cucumber 和 Capybara,Gem 版本分别为 1.0.6 和 1.1.1。

我在网上找到了一些建议,但到目前为止没有一个有效。我(单独)尝试了以下方法,但没有成功。

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

其中语言设置为“en”或“fr”。

有人可以帮忙吗?谢谢,格雷姆。

I'd like a different language to be displayed depending on the browser language and naturally I'd like to test to make 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黯淡〆 2024-12-12 11:11:21

知道了!

我的线路是正确的,问题出在其他地方。

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

这工作正常,问题是我的场景如下。

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

它应该是

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

“我是匿名用户”步骤只是先注销。似乎必须在执行使用浏览器的任何其他步骤之前设置浏览器语言。

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.

献世佛 2024-12-12 11:11:21

使用 poltergeist

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

更多详细信息:https://github.com/teampoltergeist/poltergeist#manipulate-request-headers

With poltergeist:

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

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

眼泪都笑了 2024-12-12 11:11:21

你用什么水豚驱动?这适用于机械化:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文