简体   繁体   English

如何在作为 Windows 服务运行的 Jenkins 上以无头模式使用 Firefox 运行 Selenium 测试(C#、.NET)

[英]How to run Selenium tests (C#, .NET) with Firefox in headless mode on Jenkins running as Windows service

I'm trying to set up Jenkins (version 2.117) and run automated tests using Selenium on the .NET stack in Windows 10 with Jenkins running as a Windows Service.我正在尝试设置 Jenkins(版本 2.117)并在 Windows 10 中的 .NET 堆栈上使用 Selenium 运行自动化测试,Jenkins 作为 Windows 服务运行。

I'm attempting to use the Firefox web driver with Firefox 56, which supports a headless mode.我正在尝试将 Firefox 网络驱动程序与支持无头模式的 Firefox 56 一起使用。

Machine and Software机器和软件

  • Windows 10 pro Windows 10 专业版
  • Jenkins 2.117詹金斯 2.117
  • Selenium Webdriver (latest version as of this question) Selenium Webdriver(这个问题的最新版本)
  • Firefox 56, 32 bit火狐 56,32 位
  • GeckoDriver v0.16.1壁虎驱动 v0.16.1
  • Visual Studio 2017 Enterprise Visual Studio 2017 企业版

Selenium Test硒测试

[TestClass]
public class SeleniumTest
{
    [TestMethod]
    [TestProperty("Selenium", "Google")]
    public void GoToGoogle()
    {
        var options = new FirefoxOptions();
        options.AddArgument("-headless");
        var driver = new FirefoxDriver(options);

        driver.Navigate().GoToUrl("https://www.google.com");
        StringAssert.Contains(driver.PageSource, "Google");
    }
}

I can get a test working through Visual Studio, but fails during a build on Jenkins.我可以通过 Visual Studio 进行测试,但在 Jenkins 上构建时失败。

The command used to run the test:用于运行测试的命令:

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\TestPlatform\vstest.console.exe" C:\Jenkins\workspace\WorkSpaceName\Tests\bin\ContinuousIntegration\Tests.dll /tests:Google /logger:trx

Jenkins Build Output Jenkins 构建输出

Here is the abbreviated output of the log for the build:这是构建日志的缩写输出:

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\TestPlatform\vstest.console.exe" C:\Jenkins\workspace\WorkSpaceName\Tests\bin\ContinuousIntegration\Tests.dll /tests:Google /logger:trx 
Microsoft (R) Test Execution Command Line Tool Version 15.6.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test discovery, please wait...
1525798591265   geckodriver INFO    Listening on 127.0.0.1:58807
1525798592565   geckodriver::marionette INFO    Starting browser \\?\C:\Program Files (x86)\Mozilla Firefox\firefox.exe with args ["-marionette", "-headless"]
*** You are running in headless mode.
1525798592835   addons.xpi  WARN    Error parsing extensions state: [Exception... "Component returned failure code: 0x80520012 (NS_ERROR_FILE_NOT_FOUND) [amIAddonManagerStartup.readStartupData]"  nsresult: "0x80520012 (NS_ERROR_FILE_NOT_FOUND)"  location: "JS frame :: resource://gre/modules/addons/XPIProvider.jsm :: loadExtensionState :: line 1554"  data: no] Stack trace: loadExtensionState()@resource://gre/modules/addons/XPIProvider.jsm:1554 < getInstallState()@resource://gre/modules/addons/XPIProvider.jsm:1589 < checkForChanges()@resource://gre/modules/addons/XPIProvider.jsm:3109 < startup()@resource://gre/modules/addons/XPIProvider.jsm:2188 < callProvider()@resource://gre/modules/AddonManager.jsm:269 < _startProvider()@resource://gre/modules/AddonManager.jsm:739 < startup()@resource://gre/modules/AddonManager.jsm:906 < startup()@resource://gre/modules/AddonManager.jsm:3090 < observe()@jar:file:///C:/Program%20Files%20(x86)/Mozilla%20Firefox/omni.ja!/components/addonManager.js:65
1525798593164   Marionette  INFO    Enabled via --marionette

###!!! [Parent][MessageChannel] Error: (msgtype=0x240058,name=PContent::Msg_SetPluginList) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24004C,name=PContent::Msg_GMPsChanged) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x150084,name=PBrowser::Msg_UpdateNativeWindowHandle) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x150078,name=PBrowser::Msg_Destroy) Channel error: cannot send/recv

###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24003F,name=PContent::Msg_LoadProcessScript) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24003F,name=PContent::Msg_LoadProcessScript) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x2400FC,name=PContent::Msg_AsyncMessage) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x2400FC,name=PContent::Msg_AsyncMessage) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x24001F,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x150084,name=PBrowser::Msg_UpdateNativeWindowHandle) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x150078,name=PBrowser::Msg_Destroy) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x150084,name=PBrowser::Msg_UpdateNativeWindowHandle) Channel error: cannot send/recv


###!!! [Parent][MessageChannel] Error: (msgtype=0x150078,name=PBrowser::Msg_Destroy) Channel error: cannot send/recv

1525798595876   addons.productaddons    WARN    Failed downloading via XHR, status: 0, reason: error
Failed   GoToGoogle
Error Message:
 Test method SeleniumTest.GoToGoogle threw exception: 
OpenQA.Selenium.WebDriverException: The HTTP request to the remote WebDriver server for URL http://localhost:58807/session timed out after 60 seconds. ---> System.Net.WebException: The request was aborted: The operation has timed out.
Stack Trace:
    at System.Net.HttpWebRequest.GetResponse()
   at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
 --- End of inner exception stack trace ---
    at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
   at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
   at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
   at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
   at OpenQA.Selenium.Firefox.FirefoxDriver..ctor(FirefoxOptions options)
   at StateOfMichigan.DisabilityServices.BusinessResources.Tests.UnitTests.SeleniumTest.GoToGoogle() in C:\Jenkins\workspace\MiBRS-22944-02\Tests\UnitTests\SeleniumTest.cs:line 16


Total tests: 1. Passed: 0. Failed: 1. Skipped: 0.
Test Run Failed.
Test execution time: 1.0295 Minutes

When browsing to the URL it mentions in the log output ( http://localhost:58807/session ) I get the following response:当浏览到它在日志输出 ( http://localhost:58807/session ) 中提到的 URL 时,我得到以下响应:

{
  "value": {
    "error": "unknown command",
    "message": "GET /session did not match a known command",
    "stacktrace": "stack backtrace:\n   0:           0x489f6f - <no info>\n   1:           0x48ad59 - <no info>\n   2:           0x43a15d - <no info>\n   3:           0x42ec0f - <no info>\n   4:           0x423c30 - <no info>\n   5:           0x4078fa - <no info>\n   6:           0x6bc939 - <no info>\n   7:           0x415d0d - <no info>\n   8:           0x6b6e43 - <no info>\n   9:     0x7fff56fe8364 - BaseThreadInitThunk"
  }
}

Now the response above could be due to the browser issuing a GET request instead of a POST .现在,上面的响应可能是由于浏览器发出GET请求而不是POST

Jenkins Service Properties Jenkins 服务属性

I've tried several iterations of configs for the Jenkins service:我已经为 Jenkins 服务尝试了几次配置迭代:

Default Configuration默认配置

  1. Using "Local System" account使用“本地系统”帐户
  2. No interaction with desktop不与桌面交互

Result: Same as above结果:同上

Tweaked Local System account调整本地系统帐户

  1. Using "Local System" account使用“本地系统”帐户
  2. Allow service to interact with desktop is check marked允许服务与桌面交互被选中

Result: Same as above结果:同上

Using local "Jenkins" user使用本地“Jenkins”用户

Result: An exception that it couldn't find the active directory BEAN during the initialization phase结果:初始化阶段找不到活动目录BEAN的异常

Using my Active Directory User使用我的 Active Directory 用户

  1. My own user and password我自己的用户名和密码
  2. I have admin privileges on the machine我在机器上有管理员权限

Result: Same as the original problem above结果:与上面的原始问题相同

How can I run Selenium tests in Firefox headless mode when running Jenkins as a Windows Service?将 Jenkins 作为 Windows 服务运行时,如何在 Firefox 无头模式下运行 Selenium 测试?


Update 1: Looks like there might be an issue with my version of Firefox (v56) or GeckoDriver (v0.16.1) Switching to Chrome 66 and ChromeDriver 2.38.552522 seems to be working fine.更新 1:看起来我的 Firefox (v56) 或 GeckoDriver (v0.16.1) 版本可能有问题 切换到 Chrome 66 和 ChromeDriver 2.38.552522 似乎工作正常。

I was new to Selenium and doesn't realize the version of GeckoDriver I was using was or of date compared to my version of Firefox.我是 Selenium 的新手,并没有意识到我使用的 GeckoDriver 版本与我的 Firefox 版本相比是或过时的。 Updating GeckoDriver (now called Firefox driver) solved the problem.更新 GeckoDriver(现在称为 Firefox 驱动程序)解决了这个问题。

If you think you have the right way driver, check your browser version.如果您认为您有正确的驱动程序,请检查您的浏览器版本。 Modern browser vendors have automatic updates enabled.现代浏览器供应商已启用自动更新。 Either disable that feature and suffer the consequences of unpatched security vulnerabilities or just periodically check that the web driver is up to date.要么禁用该功能并遭受未修补的安全漏洞的后果,要么只是定期检查 Web 驱动程序是否是最新的。

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

相关问题 C# selenium 脚本无法在 Chrome 无头模式下运行,但是相同的脚本在 Firefox 无头模式下运行良好 - C# selenium script fails to run in Chrome headless mode however same script runs fine in Firefox headless mode C#Selenium 3.8 + Firefox 57作为Windows服务运行 - C# Selenium 3.8 + Firefox 57 run as windows service Selenium C#中的无头Firefox - Headless Firefox in Selenium C# 在VSTS托管代理上以Chrome(无头模式)运行Selenium测试 - Running Selenium Tests in Chrome (Headless mode) on a VSTS Hosted Agent 如何在无头模式下运行我的 Selenium 项目? - How to run my Selenium Project in Headless mode? 使用Selenium WebDriver和NUnit C#在Parallel中运行测试后,如何关闭多个浏览器窗口 - How to close down multiple browser windows after running tests in Parallel using Selenium WebDriver and NUnit C# 如何在同一台 Windows 10 笔记本电脑的 Safari 浏览器上运行 Selenium 编写的测试 C# - How to run Selenium Tests written in C# on a Safari browser in the same Windows 10 laptop 在无头模式 Selenium C# 中找不到文本元素 - Cant find Text Element in Headless Mode Selenium C# Selenium 仅在无头模式下返回错误 C# - Selenium returning error only in headless mode C# 如何设置Selenium和ChromeDriver以无头模式运行 - How to set up Selenium and ChromeDriver to run in headless mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM