简体   繁体   English

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

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

Is it possible to run Selenium C# tests on a hosted agent within a Visual Studio Team Services build/release using the Chrome driver in headless mode? 是否可以在无头模式下使用Chrome驱动程序在Visual Studio Team Services构建/发布中的托管代理上运行Selenium C#测试?

I've seen contradictory information indicating this is and isn't possible currently. 我已经看到相互矛盾的信息表明这是当前不可能的。 This user voice idea seems to suggest Chrome is pre-installed on the hosted agents: 此用户语音提示似乎表明Chrome已预先安装在托管代理上:

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

Current Setup 当前设置

As part of the setup of my tests (in code), I'm indicating that I want the Chrome driver to run in headless mode and they do so when run within a local instance of VS. 作为我的测试设置的一部分(在代码中),我表示我希望Chrome驱动程序在无头模式下运行,并且在VS的本地实例中运行时它们会这样做。

Example: 例:

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

var driver = new ChromeDriver(options);

When the tests are run as part of the release definition within VSTS, the following error is thrown: 当测试作为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)

I've ensured the chromedriver.exe has been copied over to the bin directory and is included in the build artifacts that are being used by the release process. 我确保chromedriver.exe已被复制到bin目录,并包含在发布过程正在使用的构建工件中。

Alternatives 备择方案

I know you can setup a private agent to run these tests on, but I wanted to avoid doing this if possible and make use of the hosted agents within VSTS. 我知道您可以设置一个私有代理来运行这些测试,但我想尽可能避免这样做,并在VSTS中使用托管代理。

I've also managed to successfully run the tests using the PhantomJS driver within VSTS, but again we want to be able to test exclusively against the Chrome browser if possible. 我还设法使用VSTS中的PhantomJS驱动程序成功运行测试,但我们希望能够在可能的情况下专门针对Chrome浏览器进行测试。

UPDATE UPDATE

As per the accepted answer below (by Bernard Vander Beken), I managed to resolve this by inserting an additional release task to install Chrome silenty and unattended via a PowerShell script. 根据下面接受的答案(Bernard Vander Beken),我设法通过插入额外的发布任务来安装Chrome静默和通过PowerShell脚本无人值守来解决这个问题。 This task (obviously) needs to be excuted prior to the test run so that the browser can then be used in headless mode within the Selenium tests. 这项任务(显然)需要在测试运行之前执行,以便浏览器可以在Selenium测试中以无头模式使用。

发布定义示例

It should be possible, see https://github.com/Microsoft/vsts-agent/issues/1378 应该可以,请参阅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. 托管代理程序作为管理员运行,因此只要chrome具有无人参与安装模式,您就应该能够将chrome作为构建的一部分进行安装。

I was having Same Issue. 我遇到了同样的问题。 This issue is mostly because of Version Compatibility, I have tried some combinations and make it work. 这个问题主要是因为版本兼容性,我尝试了一些组合并使其工作。 Earlier I used latest version of ChromeDriver ie 76.*.*.* but this is not supported on any of Hosted Agent (As on date of writing this answer) I was using Win 1803 . 之前我使用过ChromeDriver最新版本,即76.*.*.*但是任何Hosted Agent都不支持此功能(截至编写此答案的日期)我使用的是Win 1803 I have later changed my ChromeDriver Version to 72.*.*.* and Hosted Agent to VS2017-Win2016 and it is working fine now. 我后来将我的ChromeDriver版本更改为72.*.*.*Hosted AgentVS2017-Win2016 ,现在工作正常。 It also works with Windows2019 . 它也适用于Windows2019

So conclusion is DevOps takes some time to make support available for latest release. 因此,DevOps需要一些时间来为最新版本提供支持。 Try out variety of combinations based on your project environment. 根据您的项目环境尝试各种组合。

In the more recent Hosted VS Agents, eg Hosted VS2017 , web browsers and even Selenium drivers are already installed. 在最近的Hosted VS Agents中,例如Hosted VS2017 ,已经安装了Web浏览器甚至Selenium驱动程序。 You can follow documentation for hosted agents at the Azure Piplelines Image Generation Github Repo . 您可以在Azure Piplelines Image Generation Github Repo上关注托管代理的文档。 For example, here is the reference to Chrome being present on the VS2017 agent . 例如,以下是对VS2017代理程序中存在的Chrome的引用

Click the Agent Phase title at the top of your pipeline. 单击管道顶部的“ Agent Phase标题。 Then under Agent pool select Hosted VS2017 rather than the vanilla Hosted pool. 然后在Agent pool选择Hosted VS2017而不是vanilla Hosted池。 ( Windows 2019 with VS 2019 has also recently become available). Windows 2019 with VS 2019最近也可用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在作为 Windows 服务运行的 Jenkins 上以无头模式使用 Firefox 运行 Selenium 测试(C#、.NET) - How to run Selenium tests (C#, .NET) with Firefox in headless mode on Jenkins running as Windows service 使用Chrome驱动程序在不含chrome的build agent上进行硒测试 - Selenium tests with Chrome driver on build agent without chrome 为什么硒铬驱动程序在后台运行(无头模式) - Why selenium chrome driver run in the background (headless mode) C# selenium 脚本无法在 Chrome 无头模式下运行,但是相同的脚本在 Firefox 无头模式下运行良好 - C# selenium script fails to run in Chrome headless mode however same script runs fine in Firefox headless mode 在无头 Chrome 硒中具有身份验证的代理 - Proxy with authenticaiton in headless Chrome selenium 具有代理身份验证的 Selenium 无头 chrome - Selenium headless chrome with proxy authentication Selenium C# - Chrome 驱动程序不会在无头模式下下载文件 - Selenium C# - Chrome Driver doesn't download files on headless mode VSTS托管代理无法加载DLL MSB3246 - VSTS hosted agent can't load DLL MSB3246 在硒网格上运行测试时无法调整Chrome窗口大小的大小 - Unable to resize the chrome window size when tests running on selenium Grid 在 Selenium 无头模式下暂时显示 window - Show window temporarily in Selenium headless mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM