diff --git a/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs b/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs index a3cce2afed357..7b93be2f6bfda 100644 --- a/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs +++ b/dotnet/src/webdriver/Firefox/FirefoxDriverService.cs @@ -31,6 +31,7 @@ namespace OpenQA.Selenium.Firefox public sealed class FirefoxDriverService : DriverService { private const string DefaultFirefoxDriverServiceFileName = "geckodriver"; + private string logPath = string.Empty; /// /// Initializes a new instance of the class. @@ -121,6 +122,15 @@ protected override bool HasShutdown get => false; } + /// + /// Gets or sets the path to which service logging should be written. + /// + public string LogPath + { + get { return this.logPath; } + set { this.logPath = value; } + } + /// /// Gets the command-line arguments for the driver service. /// @@ -138,7 +148,12 @@ protected override string CommandLineArguments argsBuilder.Append(string.Format(CultureInfo.InvariantCulture, " --websocket-port {0}", PortUtilities.FindFreePort())); } - if (this.BrowserCommunicationPort > 0) + if (!string.IsNullOrEmpty(this.logPath)) + { + argsBuilder.Append(string.Format(CultureInfo.InvariantCulture, " --log \"{0}\"", this.logPath)); + } + + if (this.browserCommunicationPort > 0) { argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --marionette-port {0}", this.BrowserCommunicationPort); } diff --git a/dotnet/test/firefox/FirefoxDriverServiceTests.cs b/dotnet/test/firefox/FirefoxDriverServiceTests.cs new file mode 100644 index 0000000000000..2e473aeaca55c --- /dev/null +++ b/dotnet/test/firefox/FirefoxDriverServiceTests.cs @@ -0,0 +1,63 @@ +using NUnit.Framework; +using OpenQA.Selenium.Firefox; +using System; +using System.IO; + +namespace OpenQA.Selenium.Firefox +{ + [TestFixture] + public class FirefoxDriverServiceTests + { + private string tempFileName; + + [SetUp] + public void Setup() + { + tempFileName = Path.GetTempFileName(); + File.Delete(tempFileName); + } + + [TearDown] + public void Teardown() + { + if (File.Exists(tempFileName)) + { + File.Delete(tempFileName); + } + } + + [Test] + public void CanSetLogPath() + { + var service = FirefoxDriverService.CreateDefaultService(); + service.LogPath = tempFileName; + + Assert.That(service.LogPath, Is.EqualTo(tempFileName), "LogPath should be set correctly"); + + string commandLineArgs = service.CommandLineArguments; + Assert.That(commandLineArgs, Contains.Substring($"--log \"{tempFileName}\""), + "Command line arguments should contain the log path"); + } + + [Test] + public void LogFileIsCreatedWhenDriverStarts() + { + var service = FirefoxDriverService.CreateDefaultService(); + service.LogPath = tempFileName; + + service.Start(); + + try + { + Assert.That(File.Exists(tempFileName), Is.True, "Log file should be created when service starts"); + + string logContent = File.ReadAllText(tempFileName); + Assert.That(logContent, Is.Not.Empty, "Log file should contain content"); + } + finally + { + service.Dispose(); + } + } + } +} diff --git a/dotnet/test/firefox/FirefoxDriverTest.cs b/dotnet/test/firefox/FirefoxDriverTest.cs index 0de306815de96..0b134ebc563f7 100644 --- a/dotnet/test/firefox/FirefoxDriverTest.cs +++ b/dotnet/test/firefox/FirefoxDriverTest.cs @@ -360,6 +360,43 @@ public void ShouldInstallAndUninstallUnSignedDirAddon() Assert.That(driver.FindElements(By.Id("webextensions-selenium-example")).Count, Is.Zero); } + [Test] + public void ShouldBeAbleToLogToFile() + { + string tempFileName = Path.GetTempFileName(); + try + { + File.Delete(tempFileName); + FirefoxOptions options = new FirefoxOptions(); + FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(); + service.LogPath = tempFileName; + + IWebDriver firefox = new FirefoxDriver(service, options); + firefox.Url = xhtmlTestPage; + Assert.That(firefox.Title, Is.EqualTo("XHTML Test Page")); + + firefox.Quit(); + + Assert.That(File.Exists(tempFileName), Is.True, "Log file should exist"); + string logContent = File.ReadAllText(tempFileName); + Assert.That(logContent, Is.Not.Empty, "Log file should not be empty"); + } + finally + { + if (File.Exists(tempFileName)) + { + File.Delete(tempFileName); + } + } + } + + [Test] + public void LogPathShouldBeNullByDefault() + { + FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(); + Assert.That(service.LogPath, Is.Empty); + } + private string GetPath(string name) { string sCurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;