在赛普拉斯中设置浏览器语言 [英] Setting the browser language in Cypress

查看:108
本文介绍了在赛普拉斯中设置浏览器语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我有一个应用程序需要用多种语言进行测试,所以可以告诉赛普拉斯使用某种语言(例如德语)启动Chrome。我在文档中的任何地方都看不到此详细信息,这表明目前尚不可能。

Is it possible to tell Cypress to launch Chrome with a certain language (e.g. German) as I have an application which I need to test in multiple languages. I can't see this detailed anywhere in the documentation which suggests it is not possible at present.

我尝试添加-lang 参数启动Chrome时启动,但这似乎没有任何效果,Chrome仍使用英语。参见下面的 pluginsFile 代码。

I have tried adding the --lang argument when Chrome is launched but this does not seem to have any effect and Chrome still uses English. See the pluginsFile code below.

module.exports = (on, config) => {
  on('before:browser:launch', (browser = {}, args) => {
    if (browser.name === 'chrome') {
      args.push('--lang=de')
      return args
    }
  })
}

我也尝试过-lang = de-DE ,它也没有用。

I have also tried --lang=de-DE which also did not work.

推荐答案

除了添加命令行选项外,您还可以使用Cypress的Browser Launch API(文档)。这使您可以像这样覆盖Accept-Language标头设置:

Besides adding command line options, you can also change browser preferences using Cypress' Browser Launch API (documentation). This allows you to override the Accept-Language header setting like this:

on('before:browser:launch', (browser, launchOptions) => {
  if (browser.family === 'chromium' && browser.name !== 'electron') {
    launchOptions.preferences.default.intl = { accept_languages: "nl" }
    return launchOptions
  }
}

请注意, launchOptions.preferences.default 对象可能为空,因此尝试分配给 launchOptions.preferences.default.intl.accept_languages

Note that the launchOptions.preferences.default object may be empty, so trying to assign to launchOptions.preferences.default.intl.accept_languages directly may fail.

对于我们的一个项目,这足以使我们测试的网站以正确的语言显示。如果您需要更多,您可以尝试更改其他语言设置(请参见 Chrome浏览器的源代码并查找 intl)。

For one of our projects, this was enough to get the site we were testing to appear in the right language. If you need more, there are more language settings you can try altering (see Chrome's source code and look for "intl").

在旁注中,它看起来很不错根据 --lang 命令行选项仅在Windows上有效 rel = noreferrer> Chrome的文档。在Mac上,您需要更改系统偏好设置;在Linux上,可以使用LANGUAGE环境变量。

On a side note, it looks like the --lang command line option only works on Windows, according to Chrome's documentation. On Mac, you are required to change your system preferences, and on Linux, you can use the LANGUAGE environment variable.

这篇关于在赛普拉斯中设置浏览器语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆