【问题标题】:Running Selenium Tests in Chrome (Headless mode) on a VSTS Hosted Agent在 VSTS 托管代理上以 Chrome(无头模式)运行 Selenium 测试
【发布时间】:2018-07-30 22:12:41
【问题描述】:

是否可以在无头模式下使用 Chrome 驱动程序在 Visual Studio Team Services 构建/发布中的托管代理上运行 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 测试中以无头模式使用。

【问题讨论】:

    标签: c# google-chrome selenium azure-devops azure-pipelines-release-pipeline


    【解决方案1】:

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

    托管代理以管理员身份运行,因此您应该能够安装 chrome 作为构建的一部分,只要 chrome 无人看管 安装方式。

    【讨论】:

    • 这样就完成了!我最终在发布过程中插入了一个新任务,以下载和安装 Chrome(无人值守),然后运行测试。感谢您的回复。
    • 你能在这里分享一下细节吗?
    • 我已经用解决方案更新了原始问题。
    【解决方案2】:

    在最近的托管 VS 代理中,例如Hosted VS2017,Web 浏览器甚至 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 最近也可用)。

    【讨论】:

    • 我在 VS 2017 中看不到任何“代理池”标题或任何“代理阶段”设置。我看到一个任务标题显示“在代理上运行”。当我选择它时,我可以选择使用哪个部署队列、需要什么需求、是否在 paralle.l 中的多个代理上运行、部署超时、是否跳过工件下载以及是否允许脚本访问 OAuth 令牌。没有“代理池”选项,也没有显示“托管”或“托管 VS2017”的下拉值。你在哪里看到这些?我也查看了您的链接,但它们根本没有帮助。
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 2023-01-22
      相关资源
      最近更新 更多