Skip to content

Commit

Permalink
added logPath property
Browse files Browse the repository at this point in the history
  • Loading branch information
iampopovich committed Jan 10, 2025
1 parent 335c14c commit f394f7b
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
16 changes: 16 additions & 0 deletions dotnet/src/webdriver/Firefox/FirefoxDriverService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public sealed class FirefoxDriverService : DriverService
private string host = string.Empty;
private string browserCommunicationHost = string.Empty;
private FirefoxDriverLogLevel loggingLevel = FirefoxDriverLogLevel.Default;
private string logPath = string.Empty;

/// <summary>
/// Initializes a new instance of the <see cref="FirefoxDriverService"/> class.
Expand Down Expand Up @@ -153,6 +154,15 @@ protected override bool HasShutdown
get { return false; }
}

/// <summary>
/// Gets or sets the path to which service logging should be written.
/// </summary>
public string LogPath
{
get { return this.logPath; }
set { this.logPath = value; }
}

/// <summary>
/// Gets the command-line arguments for the driver service.
/// </summary>
Expand All @@ -170,6 +180,12 @@ protected override string CommandLineArguments
argsBuilder.Append(string.Format(CultureInfo.InvariantCulture, " --websocket-port {0}", PortUtilities.FindFreePort()));
}

StringBuilder argsBuilder = new StringBuilder(base.CommandLineArguments);
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);
Expand Down
63 changes: 63 additions & 0 deletions dotnet/test/firefox/FirefoxDriverServiceTests.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
37 changes: 37 additions & 0 deletions dotnet/test/firefox/FirefoxDriverTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f394f7b

Please sign in to comment.