运行 在 VSTS 托管代理上 Chrome(无头模式)中的 Selenium 测试

Running Selenium Tests in Chrome (Headless mode) on a VSTS Hosted Agent

是否可以在 Visual Studio Team Services build/release 中使用 Chrome 驱动程序在无头模式下对托管代理进行 运行 Selenium C# 测试?

我看到了相互矛盾的信息,表明目前这是不可能的。这个用户声音想法似乎表明 Chrome 已预安装在托管代理上:

https://visualstudio.uservoice.com/forums/330519-visual-studio-team-services/suggestions/19387174-install-headless-chrome-on-hosted-build-servers

当前设置

作为我的测试设置的一部分(在代码中),我表示我希望 Chrome 驱动程序在无头模式下 运行 并且当 运行 在 VS 的本地实例中。

示例:

var options = new ChromeOptions();
options.AddArgument("headless");
options.AddArgument("disable-gpu");

var driver = new ChromeDriver(options);

当测试 运行 作为 VSTS 中发布定义的一部分时,将引发以下错误:

2018-02-20T13:21:10.7954002Z Error Message:
2018-02-20T13:21:10.7954156Z  unknown error: cannot find Chrome binary
2018-02-20T13:21:10.7954340Z   (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.14393 x86_64)
2018-02-20T13:21:10.7954487Z Stack Trace:
2018-02-20T13:21:10.7954620Z System.InvalidOperationException: unknown error: cannot find Chrome binary
2018-02-20T13:21:10.7955947Z   (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.14393 x86_64)
2018-02-20T13:21:10.7956136Z    at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
2018-02-20T13:21:10.7956387Z    at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
2018-02-20T13:21:10.7956557Z    at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
2018-02-20T13:21:10.7956729Z    at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
2018-02-20T13:21:10.7956927Z    at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
2018-02-20T13:21:10.7957106Z    at OpenQA.Selenium.Chrome.ChromeDriver..ctor(String chromeDriverDirectory, ChromeOptions options)

我已确保 chromedriver.exe 已复制到 bin 目录并包含在构建工件中正在被发布过程使用。

备选方案

我知道你可以设置一个私人代理来 运行 这些测试,但我想尽可能避免这样做并使用 VSTS 中的托管代理。

我还成功地 运行 使用 VSTS 中的 PhantomJS 驱动程序进行了测试,但如果可能的话,我们再次希望能够专门针对 Chrome 浏览器进行测试。

更新

根据下面接受的答案(Bernard Vander Beken),我设法通过插入一个额外的发布任务来解决这个问题,以通过 PowerShell 脚本安装 Chrome 静默和无人值守。这个任务(显然)需要在测试之前执行 运行 以便浏览器可以在 Selenium 测试中以无头模式使用。

应该可以,见https://github.com/Microsoft/vsts-agent/issues/1378

The hosted agent is running as Admin, so you should be able to install chrome as part of your build as long as chrome has unattended installation mode.

在最近的 Hosted VS Agents 中,例如Hosted VS2017,Web 浏览器甚至 Selenium 驱动程序都已安装。您可以在 Azure Piplelines Image Generation Github Repo. For example, here is the reference to Chrome being present on the VS2017 agent.

上关注托管代理的文档

单击管道顶部的 Agent Phase 标题。然后在 Agent pool select Hosted VS2017 而不是香草 Hosted 池下。 (Windows 2019 with VS 2019 最近也可用)。

我遇到了同样的问题。这个问题主要是因为版本兼容性,我尝试了一些组合并使其工作。早些时候,我使用了最新版本的 ChromeDriver,即 76.*.*.*,但这在任何 Hosted Agent 上都不支持(截至撰写此答案之日)我使用的是 Win 1803。我后来将我的 ChromeDriver 版本更改为 72.*.*.* 并将 Hosted Agent 更改为 VS2017-Win2016 并且现在工作正常。它也适用于 Windows2019

所以结论是 DevOps 需要一些时间才能为最新版本提供支持。根据您的项目环境尝试各种组合。