title: How to run Selenium tests (C#, .NET) with Firefox in headless mode on Jenkins running as Windows service
body: <p>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.</p>
  
  <p>I'm attempting to use the Firefox web driver with Firefox 56, which supports a headless mode.</p>
  
  <h2>Machine and Software</h2>
  
  <ul>
  <li>Windows 10 pro</li>
  <li>Jenkins 2.117</li>
  <li>Selenium Webdriver (latest version as of this question)</li>
  <li>Firefox 56, 32 bit</li>
  <li>GeckoDriver v0.16.1</li>
  <li>Visual Studio 2017 Enterprise</li>
  </ul>
  
  <h2>Selenium Test</h2>
  
  <pre><code>[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");
      }
  }
  </code></pre>
  
  <p>I can get a test working through Visual Studio, but fails during a build on Jenkins.</p>
  
  <p>The command used to run the test:</p>
  
  <pre><code>"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
  </code></pre>
  
  <h2>Jenkins Build Output</h2>
  
  <p>Here is the abbreviated output of the log for the build:</p>
  
  <pre><code>"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 &lt; getInstallState()@resource://gre/modules/addons/XPIProvider.jsm:1589 &lt; checkForChanges()@resource://gre/modules/addons/XPIProvider.jsm:3109 &lt; startup()@resource://gre/modules/addons/XPIProvider.jsm:2188 &lt; callProvider()@resource://gre/modules/AddonManager.jsm:269 &lt; _startProvider()@resource://gre/modules/AddonManager.jsm:739 &lt; startup()@resource://gre/modules/AddonManager.jsm:906 &lt; startup()@resource://gre/modules/AddonManager.jsm:3090 &lt; 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. ---&gt; 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
  </code></pre>
  
  <p>When browsing to the URL it mentions in the log output (<a href="http://localhost:58807/session" rel="nofollow noreferrer">http://localhost:58807/session</a>) I get the following response:</p>
  
  <pre><code>{
    "value": {
      "error": "unknown command",
      "message": "GET /session did not match a known command",
      "stacktrace": "stack backtrace:\n   0:           0x489f6f - &lt;no info&gt;\n   1:           0x48ad59 - &lt;no info&gt;\n   2:           0x43a15d - &lt;no info&gt;\n   3:           0x42ec0f - &lt;no info&gt;\n   4:           0x423c30 - &lt;no info&gt;\n   5:           0x4078fa - &lt;no info&gt;\n   6:           0x6bc939 - &lt;no info&gt;\n   7:           0x415d0d - &lt;no info&gt;\n   8:           0x6b6e43 - &lt;no info&gt;\n   9:     0x7fff56fe8364 - BaseThreadInitThunk"
    }
  }
  </code></pre>
  
  <p>Now the response above could be due to the browser issuing a <code>GET</code> request instead of a <code>POST</code>.</p>
  
  <h2>Jenkins Service Properties</h2>
  
  <p>I've tried several iterations of configs for the Jenkins service:</p>
  
  <h3>Default Configuration</h3>
  
  <ol>
  <li>Using "Local System" account</li>
  <li>No interaction with desktop</li>
  </ol>
  
  <p>Result: Same as above</p>
  
  <h3>Tweaked Local System account</h3>
  
  <ol>
  <li>Using "Local System" account</li>
  <li>Allow service to interact with desktop is check marked</li>
  </ol>
  
  <p>Result: Same as above</p>
  
  <h3>Using local "Jenkins" user</h3>
  
  <p>Result: An exception that it couldn't find the active directory BEAN during the initialization phase</p>
  
  <h3>Using my Active Directory User</h3>
  
  <ol>
  <li>My own user and password</li>
  <li>I have admin privileges on the machine</li>
  </ol>
  
  <p>Result: Same as the original problem above</p>
  
  <p><strong>How can I run Selenium tests in Firefox headless mode when running Jenkins as a Windows Service?</strong></p>
  
  <hr>
  
  <p><strong>Update 1:</strong> 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.</p>