Selenium Chromedriver测试在Windows docker容器中失败并带有http请求超时

时间:2019-04-27 21:49:34

标签: selenium docker selenium-chromedriver docker-windows

UPDATE 0429-您可以使用的完整docker镜像,其中包含Windows,Docker,Chrome,Firefox和所有测试。Dockerhub链接和GitHub存储库上的说明-https://github.com/jhealy/aspnet45-docker-selenium

我的selenium.Chromedrive c#测试失败,超时,试图获取远程WebDriver服务器。主要消息似乎是

  

60秒后,对URL http://localhost:49164/session的远程WebDriver服务器的HTTP请求超时

我使用dockerfile设置了应用程序并将其复制到其中。 我可以通过powershell / wget从外部或从docker文件中访问该应用程序。

以下部分包括Dockerfile,配置命令以及讨厌的小错误屏幕的完整输出。

DOCKERFILE

# extending the `microsoft/aspnet` image.
FROM microsoft/aspnet

# COPY ./bin/Publishoutput/ /inetpub/wwwroot
COPY ./MvcHelloWorld45/bin/Release/Publish/ /inetpub/wwwroot

RUN mkdir seleniumtests

COPY ./SeleniumDockerTest/bin/Release/ /seleniumtests

# choco chrome takes a bit
RUN echo 'Downloading chocolatey...'
RUN powershell -Command Install-PackageProvider -name chocolatey -Force
RUN powershell -Command Set-PackageSource -Name chocolatey -Trusted

RUN powershell -Command Get-PackageSource

RUN echo 'Install Chrome via chocolatey...'
RUN powershell -Command Install-Package GoogleChrome -MinimumVersion 74

提供

# build and run my image in local machine powershell
docker build -f dockerfile -t aspnet45 .
Docker run -d --name aspnet45run -p 5000:80 aspnet45

# verify web server is up and running
curl http://localhost:5000

# powershell into remote machine
Docker exec -it <first couple unique chars of ps> powershell

# inside remote machine make sure we can hit it
curl http://localhost -UseBasicParsing 

# run test which works fine outside but blows up inside container
cd \seleniumtests
.\SeleniumDockerTest.exe

使用NUGET SELENIUM.WEBDRIVER.CHROME for CH74进行C#硒测试

    using System;

    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;

    namespace SeleniumDockerTest
    {
        class Program
        {
            static IWebDriver m_driverGC;
            static void Main(string[] args)
            {

                string targetUrl  = @"http://localhsot";   
                try
                {
                    ChromeOptions option = new ChromeOptions();
                    option.AddArgument("--headless");
                    option.AddArgument("--dns-prefetch-disable");
                    option.AddArgument("--disable-features=VizDisplayCompositon");

                    using (m_driverGC = new ChromeDriver(option))
                    {                     
                        m_driverGC.Navigate().GoToUrl(targetUrl);
                        string msg = "hello world";
                                                   Console.WriteLine($"CheckWebElements('{msg}')={CheckWebElements(msg)}");

                        m_driverGC.Close();
                        m_driverGC.Quit();
                    }
                }
                catch ( Exception ex )
                {
                    Console.WriteLine("!!!error:" + ex.ToString());
                }
            }

            static public bool CheckWebElements(string msg)
            {
                if (string.IsNullOrWhiteSpace(msg)) return false;
                msg = msg.ToLower();
                return m_driverGC.FindElement(By.Id("myH1")).Text.ToLower().Contains(msg);
            }
        }
    }

错误阻止

Chrome WebDriver超时错误

[Target Url] =[http://localhost]
beginning chrome tests
[chrome options:] =[--headless --window-size=1920,1080 --disable-features=VizDisplayCompositor --disable-gpu]
Starting ChromeDriver 74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}) on port 49160
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[0430/150246.015:ERROR:network_change_notifier_win.cc(156)] WSALookupServiceBegin failed with: 0
[0430/150246.046:ERROR:audio_device_listener_win.cc(46)] RegisterEndpointNotificationCallback failed: 80070424

DevTools listening on ws://127.0.0.1:49163/devtools/browser/853e1883-9876-4fad-9dcc-6bf74c060baf
[0430/150246.253:ERROR:network_change_notifier_win.cc(156)] WSALookupServiceBegin failed with: 0
!!!!!!!!!!!!!!!!!!!!!!!!!!!
[exception caught] =[OpenQA.Selenium.WebDriverException: The HTTP request to the remote WebDriver server for URL http://localhost:49160/session timed out after 60 seconds. ---> System.Net.WebException: The operation has timed out
   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.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
   at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeOptions options)
   at SeleniumDockerTest.Program.DoChromeTests() in C:\dev\docker-selenium-aspnet45.git\SeleniumDockerTest\Program.cs:line 53]
!!!!!!!!!!!!!!!!!!!!!!!!!!!
Hit any key to continue

Firefox Web驱动程序超时错误

PS C:\seleniumtests> .\seleniumdockertest.exe http://localhost
[Target Url] =[http://localhost]
firefox tests commencing
BrowserExecutableLocation=C:\Program Files\Mozilla Firefox\firefox.exe
-headless
1556651472894   mozrunner::runner       INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-headless" "-foreground" "-no-remote" "-profile" "C:\\Users\\ContainerAdministrator\\AppData\\Local\\Temp\\rust_mozprofile.hlVVZVvFg66U"
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!error:OpenQA.Selenium.WebDriverException: The HTTP request to the remote WebDriver server for URL http://localhost:49158/session timed out after 60 seconds. ---> System.Net.WebException: The request was aborted: The operation has timed out.
   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 SeleniumDockerTest.Program.DoFirefoxTests() in C:\dev\docker-selenium-aspnet45.git\SeleniumDockerTest\Program.cs:line 150
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
firefox tests completed
Hit any key to continue

感谢您的光临!

1 个答案:

答案 0 :(得分:0)

解决此特定错误的方法是将Web套接字安装到我的Docker映像中。

以下Dockerfile:

FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8    
RUN powershell -Command Add-WindowsFeature Web-WebSockets    
RUN powershell -Command Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))    
RUN powershell -Command choco install googlechrome -y
RUN powershell -Command choco install firefox -y