在 CI/CD-Pipeline 中运行测试时,我在将 chrome 浏览器的语言设置为英语时遇到一些问题。我使用 Selenium 和 TestNG 进行测试。每次我在本地运行测试时,它都工作得很好,但是当管道执行测试时,它总是会导致断言值失败。这似乎是因为管道将浏览器语言更改为英语而不是德语。
我已经尝试过以下代码片段来更改浏览器语言,但它们都不起作用:
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.setHeadless(headless); //headless is true
options.setExperimentalOption("prefs", Collections.singletonMap("intl.accept_languages", "de-DE"));
我
options.addArguments("--lang=de-DE")
而且
options.addArguments("lang=de-DE")
几乎
options.addArguments("lang=de")
尝试了在网上找到的所有解决方案。我认为问题可能是无头模式,但我不知道如何解决它。
I am having some problems setting the language of my chrome-browser to english when running Tests in my CI/CD-Pipeline. I use Selenium and TestNG for testing. Everytime I run my tests locally it works perfectly fine but when the Pipeline executes the tests, it always ends up failing the asserted value. This seems to be because the pipeline changes the browsers language to english instead of german.
I already tried the following code snippets to change the browsers language but none of them worked:
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.setHeadless(headless); //headless is true
options.setExperimentalOption("prefs", Collections.singletonMap("intl.accept_languages", "de-DE"));
and
options.addArguments("--lang=de-DE")
and
options.addArguments("lang=de-DE")
and
options.addArguments("lang=de")
I tried literally every solution I found online. I think the problem might be the headless mode but I couldn't figure out how to solve it.
发布评论
评论(2)
执行 ChromeDriver 将语言更改为德语/7429447">Selenium 测试你可以使用以下代码行:
To change the language to German through ChromeDriver while executing Selenium tests you can use the following line of code:
在 Linux 上,Chrome 使用
LANGUAGE
环境变量 ( chrome.i18n)。如果您使用 docker 运行 WebDriver,只需添加
-e LANGUAGE=de_DE -e LANG=de_DE
。如果您使用部署到 Kubernetes 的 selenium-grid,您可以设置 chromeNode.extraEnvironmentVariables
在最新版本的 docker-selenium 它们添加了一个包装器来设置基于
LANGUAGE
env var关于--lang
参数(请参阅 PullRequest)On Linux, Chrome uses the
LANGUAGE
environment variable (chrome.i18n).If you run the WebDriver with docker just add
-e LANGUAGE=de_DE -e LANG=de_DE
.If you use selenium-grid deployed to Kubernetes you can set chromeNode.extraEnvironmentVariables to
In the newest version of docker-selenium they are added a wrapper to set the
LANGUAGE
env var based on the--lang
arugment (see PullRequest)