是否可以在无头模式下使用Chrome驱动程序在Visual Studio Team Services构建/发布中的托管代理上运行Selenium C#测试?
我已经看到相互矛盾的信息表明这是当前不可能的。此用户语音提示似乎表明Chrome已预先安装在托管代理上:
当前设置
作为我的测试设置的一部分(在代码中),我表示我希望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浏览器进行测试。
UPDATE
根据下面接受的答案(Bernard Vander Beken),我设法通过插入额外的发布任务来安装Chrome静默和通过PowerShell脚本无人值守来解决这个问题。这项任务(显然)需要在测试运行之前执行,以便浏览器可以在Selenium测试中以无头模式使用。
应该可以,请参阅https://github.com/Microsoft/vsts-agent/issues/1378
托管代理程序作为管理员运行,因此只要chrome具有无人参与安装模式,您就应该能够将chrome作为构建的一部分进行安装。
在最近的Hosted VS Agents中,例如Hosted VS2017
,网络浏览器甚至Selenium驱动程序已经安装。您可以在Azure Piplelines Image Generation Github Repo上关注托管代理的文档。例如,here is the reference to Chrome being present on the VS2017 agent。
单击管道顶部的Agent Phase
标题。然后在Agent pool
下选择Hosted VS2017
而不是香草Hosted
池。 (Windows 2019 with VS 2019
最近也有售)。