New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
[馃悰 Bug]: LogContext does not work when created asynchronously #13859
Comments
@MJB222398, thank you for creating this issue. We will troubleshoot it as soon as we can. Info for maintainersTriage this issue by using labels.
If information is missing, add a helpful comment and then
If the issue is a question, add the
If the issue is valid but there is no time to troubleshoot it, consider adding the
If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C),
add the applicable
After troubleshooting the issue, please add the Thank you! |
To simplify the example to reproduce the issue: using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Internal.Logging;
namespace SeleniumPlayground
{
public class Tests
{
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 Test2()
{
using var driver = await CreateDriverAsync();
driver.Navigate("https://www.amazon.co.uk/");
}
private static async Task<MyDriver> CreateDriverAsync()
{
var logContext = Log.CreateContext(LogEventLevel.Trace);
var driver = new ChromeDriver();
await Task.Delay(100);
return new MyDriver(driver, logContext);
}
}
} |
@MJB222398, I spent couple of hours, no ideas why it is not working as expected. I would suggest to use the following pattern: using (Log.CreateContext(...))
// do something Hopefully it will work. |
@nvborisenko Afraid I cannot wrap it in a using due to the structure of the application. Disposing of it myself manually is the only option. For me at least it feels like I want to be able to attach a logging context to a driver instance. Having the logging be static via the |
Basically, the way how In our example So what we can do about that. We can extract sync [Test]
public async Task Test2()
{
var logContext = CreateLogContext();
using var driver = await CreateDriverAsync(logContext);
driver.Navigate("...");
}
private ILogContext CreateLogContext() =>
Log.CreateContext(LogEventLevel.Trace); But I would go further. I would use NUnit's SetUp functionality which is a good place for driver creation and similar initialization things. private ILogContext _logContext;
private MyDriver _driver;
[SetUp]
public void SetUpLogContext() =>
_logContext = CreateLogContext();
[SetUp]
public async Task SetUpDriver() =>
_driver = await CreateDriverAsync(_logContext);
[TearDown]
public void TearDownDriver() =>
_driver?.Dispose();
private ILogContext CreateLogContext() =>
Log.CreateContext(LogEventLevel.Trace);
[Test]
public async Task Test2()
{
_driver.Navigate("...");
//...
} The trick is that |
@YevgeniyShunevych Is that behaviour of NUnit's documented? As I understood it they provide zero guarantee about the order in which those two setup methods would be called. No guarantees it will be consistent for a given version and no guarantees that it will not change in the future. Anyway, due to the complexity of my solution creating the log context in a setup method like that is not ideal. But I can make it work with your first suggestion I think. So is it the case that there is nothing to do here on the Selenium side? No fixes/changes/documentation? |
Right, there is no guarantee in order of SetUp methods. It just works this way at the moment. It's up to you whether to rely on this option.
From https://docs.nunit.org/articles/nunit/writing-tests/attributes/setup.html |
Moreover, NUnit has different execution contexts for Historical note: static |
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?
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
Operating 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
The text was updated successfully, but these errors were encountered: