I've been new to automated test creation and have been dipping my hands on it recently. Every test seems to be running successfully individually except whenever I run them all together sequentially, sometimes, on random tests, I get an invalid session id error as described in the title. This happens completely randomly and on random tests too, it isn't always on the same ones where I get failures like that. I tried browsing to see if it was something related to my base automated test, perhaps it could be related to drivers overlapping but couldn't reach a solution that would make this stop happening. Here's the base I have for all my tests:
public class BaseAutomatedTest : IDisposable
{
public IWebDriver Driver;
public BaseAutomatedTest()
{
Driver = new TestDriverFactory().CreateDriver();
Driver.Manage().Window.Maximize();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~BaseAutomatedTest()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
//// Cleanup
if (Driver != null)
{
Driver.Close();
Driver.Quit();
Driver.Dispose();
}
}}
All tests inherit the code above. Any ideas of what could possibly be going wrong? The internet made me inclined that my issue is with this base/driver initialization/closure but none of the solutions I've found seem to work. I'm using Selenium paired up with Chromedriver to execute my tests.
Edit: I've added a stack trace of an example of the error I'm getting. I'm using Chromedriver77 for this and I don't always get this error on this specific test, it's just for example purposes since it passes successfully sometimes:
System.AggregateException : One or more errors occurred.
---- OpenQA.Selenium.WebDriverException : invalid session id
---- OpenQA.Selenium.WebDriverException : A exception with a null response was thrown sending an HTTP request to the remote WebDriver server for URL http://localhost:51059/session/68f5fa5dfe6981eb48b5cdaf26f83abd/window. The status of the exception was ConnectFailure, and the message was: Unable to connect to the remote server
-------- System.Net.WebException : Unable to connect to the remote server
------------ System.Net.Sockets.SocketException : No connection could be made because the target machine actively refused it 127.0.0.1:51059
----- Inner Stack Trace #1 (OpenQA.Selenium.WebDriverException) -----
RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
RemoteWebDriver.FindElement(String mechanism, String value)
RemoteWebDriver.FindElementById(String id)
<>c__DisplayClass16_0.<Id>b__0(ISearchContext context)
By.FindElement(ISearchContext context)
RemoteWebDriver.FindElement(By by)
ChangeUsernameTest.NavigateTo() line 91
ChangeUsernameTest.ChangeUsername_ConclusionNavigateToLinkValidation_Success(String username, String password) line 979
----- Inner Stack Trace #2 (OpenQA.Selenium.WebDriverException) -----
HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
HttpCommandExecutor.Execute(Command commandToExecute)
DriverServiceCommandExecutor.Execute(Command commandToExecute)
RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
RemoteWebDriver.Close()
BaseFunctionalTest.Dispose(Boolean disposing) line 34
BaseFunctionalTest.Dispose() line 22
ReflectionAbstractionExtensions.DisposeTestClass(ITest test, Object testClass, IMessageBus messageBus, ExecutionTimer timer, CancellationTokenSource cancellationTokenSource) line 79
----- Inner Stack Trace -----
HttpWebRequest.GetResponse()
HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
----- Inner Stack Trace -----
Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)