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

时间:2018-05-08 17:13:14

标签: c# firefox jenkins selenium-webdriver headless-browser

我正在尝试设置Jenkins(版本2.117)并使用Selenium在Windows 10中的.NET堆栈上运行自动化测试,Jenkins作为Windows服务运行。

我正在尝试使用支持无头模式的Firefox 56的Firefox网络驱动程序。

机器和软件

  • Windows 10专业版
  • Jenkins 2.117
  • Selenium Webdriver(此问题的最新版本)
  • Firefox 56,32位
  • GeckoDriver v0.16.1
  • Visual Studio 2017 Enterprise

硒测试

[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");
    }
}

我可以通过Visual Studio进行测试,但在Jenkins构建期间失败。

用于运行测试的命令:

"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构建输出

以下是构建日志的缩写输出:

"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

当浏览到日志输出中提到的URL(http://localhost:58807/session)时,我得到以下回复:

{
  "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"
  }
}

现在上述回复可能是由于浏览器发出GET请求而不是POST

Jenkins服务属性

我已经为Jenkins服务尝试了几次配置迭代:

默认配置

  1. 使用“本地系统”帐户
  2. 没有与桌面交互
  3. 结果:与上述相同

    调整本地系统帐户

    1. 使用“本地系统”帐户
    2. 允许与桌面交互的服务已标记为
    3. 结果:与上述相同

      使用本地“Jenkins”用户

      结果:在初始化阶段无法找到活动目录BEAN的异常

      使用我的Active Directory用户

      1. 我自己的用户和密码
      2. 我在机器上拥有管理员权限
      3. 结果:与上面的原始问题相同

        在将Jenkins作为Windows服务运行时,如何在Firefox无头模式下运行Selenium测试?

        更新1:看起来我的Firefox版本(v56)或GeckoDriver(v0.16.1)可能存在问题切换到Chrome 66和ChromeDriver 2.38.552522似乎工作正常。

1 个答案:

答案 0 :(得分:0)

我是Selenium的新手,与我的Firefox版本相比,我没有意识到我正在使用的GeckoDriver版本。更新GeckoDriver(现在称为Firefox驱动程序)可以解决该问题。

如果您认为驱动程序正确,请检查浏览器版本。现代浏览器供应商已启用自动更新。要么禁用该功能并遭受未修补的安全漏洞的后果,要么只是定期检查Web驱动程序是否为最新。