My Question
Simple: What am I doing wrong, if anything? If I'm doing everything right, then what can I do to get my tests running again?
Back-story
I'm trying to revive an old Selenium project of mine that hasn't been worked on in ~2 years, and I can't even get the simplest of tests to run. In debugging, it seems to be that the session ID isn't being persisted to the Selenium WebDriver, and as such it fails to make all subsequent requests.
The exception I'm currently getting is as follows:
OpenQA.Selenium.WebDriverException
HResult=0x80131500
Message=A exception with a null response was thrown sending an HTTP request to the remote WebDriver server for URL http://localhost:64334/session//url. The status of the exception was UnknownError, and the message was: An error occurred while sending the request. A connection with the server could not be established
Source=WebDriver
StackTrace:
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.set_Url(String value)
at MySeleniumProject.Pages.LoginPage..ctor(IWebDriver driver, IConfiguration config) in C:\TeamFS\Project\Dev\MySeleniumProject\MySeleniumProject\Pages\LoginPage.cs:line 12
at MySeleniumProject.LoginTests.Can_Log_In_Successfully() in C:\TeamFS\Project\Dev\MySeleniumProject\MySeleniumProject\LoginTests.cs:line 16
Inner Exception 1:
WebException: An error occurred while sending the request. A connection with the server could not be established
Inner Exception 2:
HttpRequestException: An error occurred while sending the request.
Inner Exception 3:
WinHttpException: A connection with the server could not be established
At the same time, I am able to open Postman and call the driver directly, as seen below:
Code
C# .NET Core 2.0
Selenium.WebDriver (3.141.0)
xunit (2.4.1)
chromedriver.exe (76.0.3809.126)
Chrome (76.0.3809.100)
protected TestBase()
{
try
{
Config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
Username = Config["Credentials:user"];
Password = Config["Credentials:password"];
Driver = CreateDriverFromConfig();
}
// This should be a catch. I'm leaving this here so that the question makes sense for future audiences
finally
{
Dispose();
}
}
private IWebDriver CreateDriverFromConfig()
{
IWebDriver driver;
var wd = "WebDriver";
var selected = Config[$"{wd}:selected"];
var url = Config[$"{wd}:{selected}:url"] ?? "http://127.0.0.1";
if (!int.TryParse(Config[$"{wd}:{selected}:port"], out var port))
port = 26901;
if (!int.TryParse(Config[$"{wd}:{selected}:timeout"], out var timeout))
timeout = 10;
var uri = new Uri($"{url}:{port}");
Console.WriteLine($"URI: {uri} | Timeout (seconds): {timeout}");
if (selected.Contains("Edge"))
driver = CreateEdgeDriver(uri, timeout);
else if (selected.Contains("Firefox"))
driver = CreateFirefoxDriver(uri, timeout);
else if (selected.Contains("Explorer") || selected.Equals("IE"))
driver = CreateInternetExplorerDriver(uri, timeout);
else
driver = CreateChromeDriver(uri, timeout);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(timeout);
driver.Manage().Window.Size = new Size(1600, 900);
return driver;
}
private IWebDriver CreateChromeDriver(Uri uri, int timeout)
{
_cds = ChromeDriverService.CreateDefaultService();
// _cds.HostName = uri.Host;
// _cds.Port = uri.Port;
_cds.Start();
var co = new ChromeOptions();
// co.AddArgument("no-sandbox");
_capabilities = co.ToCapabilities();
_options = co;
return new ChromeDriver(_cds, (ChromeOptions) _options, TimeSpan.FromSeconds(timeout));
}
public class LoginTests : TestBase
{
[Fact]
public void Can_Log_In_Successfully()
{
// arrange
// act
var result = new LoginPage(Driver, Config)
.Login(Username, Password);
// assert
Assert.IsType<BlankPage>(result);
}
}
public class LoginPage : PageBase<LoginPage>
{
#region Constructor
public LoginPage(IWebDriver driver, IConfiguration config) : base(driver, config)
{
Driver
.Navigate()
.GoToUrl(System.Environment.GetEnvironmentVariable("Site_URL") ?? "http://mywebsite.com");
}
}
Disregard the question. I was a dumb-dumb and disposing of my service in the constructor via a try-finally block. Changed it to only run Dispose()
in the event of an exception, and now it runs.
Thanks anyway. facepalm
User contributions licensed under CC BY-SA 3.0