试图创建ChomeDriver实例抛出异常

时间:2016-06-10 18:07:07

标签: c# selenium-webdriver selenium-chromedriver

从昨天6/10/2016开始,当我通过TeamCity运行时,我的ChromeDriver测试都失败,给出以下异常。

[Step 4/4] OneTimeSetUp: System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
  ----> OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:57539/session timed out after 60 seconds.
  ----> System.Net.WebException : The operation has timed out

以下是我OneTimeSetUp的部分内容

[OneTimeSetUp]
public void SetUp()
{
    foreach (var process in Process.GetProcessesByName("chromedriver"))
    {
        process.Kill();
    }
    foreach (var process in Process.GetProcessesByName("chrome"))
    {
        process.Kill();
    }
    Driver = new TWebDriver();
    string s = (string)((IJavaScriptExecutor)Driver).ExecuteScript("return navigator.userAgent;");
    log.Debug("Browser name/version: " + s);

我已经通过查看我的日志验证了我没有通过Driver = new TWebDriver();

这是我的TeamCity构建日志中的堆栈跟踪。

[10:51:34][Step 4/4] 1) TearDown Error : POMAuctivaTest.TestSuite.ExistingUserTestSuite<ChromeDriver>
[10:51:34][Step 4/4] System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
[10:51:34][Step 4/4]   ----> OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:57539/session timed out after 60 seconds.
[10:51:34][Step 4/4]   ----> System.Net.WebException : The operation has timed out
[10:51:34][Step 4/4] TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
[10:51:34][Step 4/4]    at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
[10:51:34][Step 4/4]    at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
[10:51:34][Step 4/4]    at System.Activator.CreateInstance[T]()
[10:51:34][Step 4/4]    at POMAuctivaTest.TestSuite.ExistingUserTestSuite`1.SetUp() in c:\TCAgents\2\work\dbc32f79002d0c18\POMAuctivaTest.TestSuite\ExistingUserTestSuite.cs:line 49
[10:51:34][Step 4/4] --WebDriverException
[10:51:34][Step 4/4]    at OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest request)
[10:51:34][Step 4/4]    at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
[10:51:34][Step 4/4]    at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
[10:51:34][Step 4/4]    at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
[10:51:34][Step 4/4]    at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
[10:51:34][Step 4/4]    at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
[10:51:34][Step 4/4]    at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeOptions options)
[10:51:34][Step 4/4] --WebException
[10:51:34][Step 4/4]    at System.Net.HttpWebRequest.GetResponse()
[10:51:34][Step 4/4]    at OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest request)

在我看来,webdriver无法创建ChromeDriver的实例?当我通过NUnitConsoleRunner3.2.1在本地运行测试时(与在TeamCity中运行的方式相同),测试运行正常。只有团队合作才能解决这个问题。我没有权限直接登录他们运行的服务器,并检查Chrome是否有什么问题我已经让DevOps团队的某个人看了一眼,但我正在等待回复。非常感谢任何帮助我真的不希望这些测试在周末被打破我有足够的睡眠问题。

2 个答案:

答案 0 :(得分:0)

测试后清理的更好方法是在每次测试后销毁驱动程序。我使用以下代码:

[TearDown]
public void TearDown()
{
    var state = TestContext.CurrentContext.Result.State;
    if (state == TestState.Error || state == TestState.Failure)
    {
        Utilities.Core.TakeScreenshot(this._chromeDriver, TestContext.CurrentContext.Test.FullName);
    }

    _driver.Quit();
}

此代码的上半部分是在出错时捕获快照。当您想使用TeamCity

时,这是一个非常好的功能

答案 1 :(得分:0)

当您的开发机器上运行Selenium测试但在TeamCity中没有运行时,以下错误的最常见原因是:

  

OpenQA.Selenium.WebDriverException:对远程的HTTP请求   URL http://localhost:????/session的WebDriver服务器超时   60秒后

是您的TeamCity作为Windows服务运行。

停止服务&#34; TeamCity Build Agent&#34;和#34; TeamCity Server&#34;而是使用runAll.bat:

<TeamCity folder>\bin\runAll.bat start