I’m encountering an issue where Google Chrome processes persist in the task manager even after I dispose of the WebDriver instance in my Selenium-based automation tests. Below is a minimal example that demonstrates this problem:
IExample Interface
using System;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.DevTools.V85.Page;
using OpenQA.Selenium.Support.UI;
namespace ExampleNamespace
{
public interface IExample : IDisposable
{
Task Login();
void Logout();
}
public class ExampleBaseClass : IExample
{
private readonly IWebDriver _driver;
private bool _disposed = false;
public ExampleBaseClass()
{
var options = new ChromeOptions();
options.AddArgument("--headless");
options.AddArgument("--no-sandbox");
options.AddArgument("--disable-dev-shm-usage");
_driver = new ChromeDriver(options);
}
public async Task Login()
{
_driver.Navigate().GoToUrl("https://example.com/login");
await Task.Delay(2000); // Simulate login process
}
public void Logout()
{
_driver.Navigate().GoToUrl("https://example.com/logout");
}
// Public implementation of Dispose pattern callable by consumers.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// Protected implementation of Dispose pattern.
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
try
{
// Dispose managed resources.
_driver.Quit();
_driver.Dispose();
}
catch (Exception)
{
throw;
}
}
_disposed = true;
}
}
// Finalizer
~ExampleBaseClass()
{
Dispose(false);
}
}
}
ExampleProcesses Class
using System;
using System.Threading.Tasks;
namespace ExampleNamespace
{
public class ExampleProcesses : IDisposable
{
private readonly IExample _exampleBase;
public ExampleProcesses(IExample exampleBase)
{
_exampleBase = exampleBase ?? throw new ArgumentNullException(nameof(exampleBase));
}
public async Task UpdateExample()
{
try
{
await _exampleBase.Login();
// Simulate some operations
await Task.Delay(2000);
_exampleBase.Logout();
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
throw;
}
finally
{
_exampleBase.Close();
_exampleBase.Dispose();
}
}
}
}
Run
[Fact]
public async Task ExampleRun()
{
await _exampleProcess.UpdateExample();
}
Problem:
Despite calling _driver.Quit()
and _driver.Dispose()
, Chrome processes remain in the task manager when running tests using dotnet test
. This causes resource leakage and potential conflicts with other tests.