我在 C# 中使用 NUnit 和 Selenium 来构建一个框架,该框架包含在多个浏览器上运行多个测试用例。在这种情况下也支持 Microsoft Edge。当我设置测试时,chrome 和 firefox 运行正常,但 edge 出现以下错误:
OpenQA.Selenium.WebDriverException :抛出了一个带有空响应的异常,向远程 WebDriver 服务器发送 HTTP 请求以获取 URL http://localhost:55992/session. 异常状态为 ReceiveFailure,消息为:底层连接已关闭:接收时发生意外错误。----> System.Net.WebException:底层连接已关闭:接收时发生意外错误。----> System.IO.IOException : Unable to read data from the transport connection: 一个现有的连接被远程主机强行关闭。----> System.Net.Sockets.SocketException : 现有连接被远程主机强行关闭 [2018. 06. 17. 17:08:22 警告] 在 OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo) 在 OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute) 在 OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute( OpenQA 中的命令 commandToExecute)。
我试图查找这个特定的错误,我得到的最接近的是远程驱动程序尝试发送一个 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;
}
}
}
测试代码:
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();
}
}
}