如何正确设置edgedriver?收到以下错误

时间:2018-06-17 15:24:46

标签: c# .net selenium selenium-webdriver selenium-edgedriver

我在C#中使用NUnit和Selenium来构建一个包含在多个浏览器上运行多个测试用例的框架。在这种情况下,也支持Microsoft Edge。当我设置我的测试时,chrome和firefox正确运行但是edge会出现以下错误:

  

OpenQA.Selenium.WebDriverException:抛出了一个带有空响应的异常,向URL http://localhost:55992/session的远程WebDriver服务器发送HTTP请求。异常的状态是ReceiveFailure,消息是:基础连接已关闭:接收时发生意外错误。     ----> System.Net.WebException:基础连接已关闭:接收时发生意外错误。     ----> System.IO.IOException:无法从传输连接读取数据:远程主机强制关闭现有连接。     ----> System.Net.Sockets.SocketException:远程主机强制关闭现有连接   [2018。 06. 17. 17:08:22警告]在OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)      在OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(命令commandToExecute)      在OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(命令commandToExecute)      在OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute,Dictionary`2参数)      在OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)      在OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor,ICapabilities desiredCapabilities)      在OpenQA.Selenium.Edge.EdgeDriver..ctor(EdgeOptions选项)      在OpenQA.Selenium.Edge.EdgeDriver..ctor()      在SeleniumNUnit.Tests.WebDriverFactory.EdgeDriver()中的C:\ SeleniumNUnit - Copy \ SeleniumNUnit.Tests \ Class1.cs:第70行      在C:\ SeleniumNUnit中的SeleniumNUnit.Tests.WebDriverFactory.WebDriver(BrowserType类型) - Copy \ SeleniumNUnit.Tests \ Class1.cs:第50行      在C:\ SeleniumNUnit中的SeleniumNUnit.Tests.WebDriverFactory..ctor(BrowserType类型) - Copy \ SeleniumNUnit.Tests \ Class1.cs:第24行      在SeleniumNUnit.DemoTest..ctor(BrowserType浏览器)中的C:\ SeleniumNUnit - Copy \ SeleniumNUnit \ Class1.cs:第12行   --WebException      在System.Net.HttpWebRequest.GetResponse()      在OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)   --IOException      在System.Net.Sockets.NetworkStream.Read(Byte []缓冲区,Int32偏移量,Int32大小)      在System.Net.PooledStream.Read(Byte []缓冲区,Int32偏移量,Int32大小)      在System.Net.Connection.SyncRead(HttpWebRequest请求,布尔userRetrievedStream,布尔probeRead)   --SocketException      在System.Net.Sockets.Socket.Receive(Byte []缓冲区,Int32偏移量,Int32大小,SocketFlags socketFlags)      在System.Net.Sockets.NetworkStream.Read(Byte []缓冲区,Int32偏移量,Int32大小)

我试图查找此特定错误,而我得到的最接近的是远程驱动程序尝试发送HTTP请求,在这种情况下失败。由于Chrome和FF运行正常,我不知道Edge出了什么问题。

WebDriverFactory:

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using WebDriverManager;
using WebDriverManager.DriverConfigs.Impl;
using System.IO;
using OpenQA.Selenium.Edge;
using System;

namespace SeleniumNUnit.Tests
{
    /// <summary>
    /// A static factory object for creating WebDriver instances
    /// </summary>
    public class WebDriverFactory
    {
        public static string path = "C:\\Drivers\\";
        public IWebDriver Driver;

    protected WebDriverFactory(BrowserType type)
    {
        Driver = WebDriver(type);
    }

    [OneTimeTearDown]
    public void TestFixtureTearnDown()
    {
        Driver.Quit();
    }

    /// <summary>
    /// Types of browser available for proxy examples.
    /// </summary>
    public enum BrowserType
    {
        IE,
        Chrome,
        Firefox,
    }

    public static IWebDriver WebDriver(BrowserType type)
    {
        IWebDriver driver = null;

        switch (type)
        {
            case BrowserType.IE:
                driver = EdgeDriver();
                break;
            case BrowserType.Firefox:
                driver = FirefoxDriver();
                break;
            case BrowserType.Chrome:
                driver = ChromeDriver();
                break;
        }

        return driver;
    }

    /// <summary>
    /// Creates Internet Explorer Driver instance.
    /// </summary>
    /// <returns>A new instance of IEDriverServer</returns>
    private static IWebDriver EdgeDriver()
    {
        new DriverManager().SetUpDriver("https://download.microsoft.com/download/F/8/A/F8AF50AB-3C3A-4BC4-8773-DC27B32988DD/MicrosoftWebDriver.exe", Path.Combine(path, "MicrosoftWebDriver.exe"), "MicrosoftWebDriver.exe");
        IWebDriver driver = new EdgeDriver();   
        return driver;
    }

    /// <summary>
    /// Creates Firefox Driver instance.
    /// </summary>
    /// <returns>A new instance of Firefox Driver</returns>
    private static IWebDriver FirefoxDriver()
    {
        new DriverManager().SetUpDriver(new FirefoxConfig());
        FirefoxOptions options = new FirefoxOptions();
        IWebDriver driver = new FirefoxDriver(options);
        return driver;
    }


    /// <summary>
    /// Creates Chrome Driver instance.
    /// </summary>
    /// <returns>A new instance of Chrome Driver</returns>
    private static IWebDriver ChromeDriver()
    {
        new DriverManager().SetUpDriver("https://chromedriver.storage.googleapis.com/2.40/chromedriver_win32.zip", Path.Combine(path, "chromedriver.exe"), "chromedriver.exe");
        ChromeOptions chromeOptions = new ChromeOptions();
        IWebDriver driver = new ChromeDriver(chromeOptions);
        return driver;
    }


}
}

TestCode:

using System;
using NUnit.Framework;
namespace SeleniumNUnit
{
    [TestFixture(SeleniumNUnit.Tests.WebDriverFactory.BrowserType.Chrome)]
    [TestFixture(SeleniumNUnit.Tests.WebDriverFactory.BrowserType.Firefox)]
    [TestFixture(SeleniumNUnit.Tests.WebDriverFactory.BrowserType.IE)]
    public class DemoTest : SeleniumNUnit.Tests.WebDriverFactory
    {
        public DemoTest(SeleniumNUnit.Tests.WebDriverFactory.BrowserType browser)
        : base(browser)
    {

    }

    [OneTimeSetUp]
    public void SetUpEnvironment()
    {
        Driver.Navigate().GoToUrl("http://google.com");

    }

    [Test]
    public void validateTitle()
    {
        Assert.That(Driver.Title.Contains("Google"));
    }

    [OneTimeTearDown]
    public void tearDown()
    {
        Driver.Quit();
        Driver.Dispose();
    }
}
}

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,但后来我发现了这个问题:

Selenium.WebDriver.MicrosoftDriver v17.17134.0 crash when running the test

长话短说,请检查您的Windows版本。如果是17134版本,那么获取MicrosoftWebDriver 17134版本。但是,如果您使用的是16299.xxx,则必须使用16299版本的MicrosoftWebDriver。