-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Description
What happened?
I have an NUnit testing solution and I want to collect driver logs per test on the file system. I create a new driver for each test, doing this asynchronously. So my solution was to create a new ILogContext when I do this and use a FileLogHandler with a file path for that test. However I noticed that these log files were almost empty, whatever the tests did with the driver was not being logged.
Eventually I figured out it was because I am creating it asynchronously, if I do it synchronously then all the logs are present in the file.
How can we reproduce the issue?
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Internal.Logging;
using OpenQA.Selenium.Remote;
namespace SeleniumPlayground
{
public class Tests
{
private const string SeleniumHubUrl = "http://localhost:4444/wd/hub";
private const string LogFilePathTest1 = "C:\\Users\\MyName\\Downloads\\Gubbins\\logs-test-1.log";
private const string LogFilePathTest2 = "C:\\Users\\MyName\\Downloads\\Gubbins\\logs-test-2.log";
private class MyDriver(IWebDriver driver, ILogContext logContext) : IDisposable
{
private readonly IWebDriver _driver = driver;
private readonly ILogContext _logContext = logContext;
public void Navigate(string url) => _driver.Navigate().GoToUrl(url);
public void Dispose()
{
_logContext?.Dispose();
_driver?.Dispose();
}
}
[Test]
public async Task Test1()
{
using var myDriver = CreateDriver(LogFilePathTest1);
await DoSomeDriverActions(myDriver);
}
[Test]
public async Task Test2()
{
using var myDriver = await CreateDriverAsync(LogFilePathTest2);
await DoSomeDriverActions(myDriver);
}
private static async Task DoSomeDriverActions(MyDriver driver)
{
driver.Navigate("https://www.amazon.co.uk/");
await Task.Delay(2000);
driver.Navigate("https://www.bbc.co.uk/");
await Task.Delay(2000);
driver.Navigate("https://www.ebay.co.uk/");
await Task.Delay(2000);
}
private static MyDriver CreateDriver(string logFilePath)
{
var logContext = Log.CreateContext(LogEventLevel.Trace).Handlers.Add(new FileLogHandler(logFilePath));
var driver = new RemoteWebDriver(new Uri(SeleniumHubUrl), GetDriverCapabilities(), TimeSpan.FromSeconds(30));
return new MyDriver(driver, logContext);
}
private static async Task<MyDriver> CreateDriverAsync(string logFilePath)
{
var logContext = Log.CreateContext(LogEventLevel.Trace).Handlers.Add(new FileLogHandler(logFilePath));
var driver = new RemoteWebDriver(new Uri(SeleniumHubUrl), GetDriverCapabilities(), TimeSpan.FromSeconds(30));
await Task.Delay(100);
return new MyDriver(driver, logContext);
}
private static ICapabilities GetDriverCapabilities()
{
EdgeOptions edgeOptions = new();
edgeOptions.AddArgument("--start-maximized");
edgeOptions.AddArgument("--disable-dev-shm-usage");
return edgeOptions.ToCapabilities();
}
}
}So if you run the tests above you see that Test1 produces a log file with all expected logs. Test2 does not, the only difference between the two tests is that one creates the driver in a synchronous method and the other does it asynchronously.
Relevant log output
N/AOperating System
Windows 10
Selenium version
4.19.0
What are the browser(s) and version(s) where you see this issue?
All
What are the browser driver(s) and version(s) where you see this issue?
All
Are you using Selenium Grid?
Yes