Skip to content

Commit e12da0d

Browse files
[dotnet] Sending GeckoDriver output to a log file. (#16081)
* [dotnet] Sending GeckoDriver output to a log file. Related to #12273 * [dotnet] Running format script * [dotnet] Adding test for log flag in Firefox Adding BUILD.bazel to run those tests in CI. * Update dotnet/src/webdriver/Firefox/FirefoxDriverService.cs Co-authored-by: qodo-merge-pro[bot] <151058649+qodo-merge-pro[bot]@users.noreply.github.com> * Update dotnet/src/webdriver/Firefox/FirefoxDriverService.cs Co-authored-by: qodo-merge-pro[bot] <151058649+qodo-merge-pro[bot]@users.noreply.github.com> --------- Co-authored-by: qodo-merge-pro[bot] <151058649+qodo-merge-pro[bot]@users.noreply.github.com>
1 parent 3744929 commit e12da0d

File tree

5 files changed

+188
-2
lines changed

5 files changed

+188
-2
lines changed

.skipped-tests

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
-//dotnet/test/common:NetworkInterceptionTests-chrome
22
-//dotnet/test/common:NetworkInterceptionTests-edge
3+
-//dotnet/test/firefox:FirefoxDriverTest-firefox
34
-//java/test/org/openqa/selenium/chrome:ChromeDriverFunctionalTest
45
-//java/test/org/openqa/selenium/chrome:ChromeDriverFunctionalTest-remote
56
-//java/test/org/openqa/selenium/edge:EdgeDriverFunctionalTest

dotnet/src/webdriver/DriverService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ protected virtual void Dispose(bool disposing)
284284
/// Raises the <see cref="DriverProcessStarting"/> event.
285285
/// </summary>
286286
/// <param name="eventArgs">A <see cref="DriverProcessStartingEventArgs"/> that contains the event data.</param>
287-
protected void OnDriverProcessStarting(DriverProcessStartingEventArgs eventArgs)
287+
protected virtual void OnDriverProcessStarting(DriverProcessStartingEventArgs eventArgs)
288288
{
289289
if (eventArgs == null)
290290
{
@@ -298,7 +298,7 @@ protected void OnDriverProcessStarting(DriverProcessStartingEventArgs eventArgs)
298298
/// Raises the <see cref="DriverProcessStarted"/> event.
299299
/// </summary>
300300
/// <param name="eventArgs">A <see cref="DriverProcessStartedEventArgs"/> that contains the event data.</param>
301-
protected void OnDriverProcessStarted(DriverProcessStartedEventArgs eventArgs)
301+
protected virtual void OnDriverProcessStarted(DriverProcessStartedEventArgs eventArgs)
302302
{
303303
if (eventArgs == null)
304304
{

dotnet/src/webdriver/Firefox/FirefoxDriverService.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using System.Globalization;
2323
using System.IO;
2424
using System.Text;
25+
using System.Threading.Tasks;
2526

2627
namespace OpenQA.Selenium.Firefox;
2728

@@ -32,6 +33,11 @@ public sealed class FirefoxDriverService : DriverService
3233
{
3334
private const string DefaultFirefoxDriverServiceFileName = "geckodriver";
3435

36+
/// <summary>
37+
/// Process management fields for the log writer.
38+
/// </summary>
39+
private StreamWriter? logWriter;
40+
3541
/// <summary>
3642
/// Initializes a new instance of the <see cref="FirefoxDriverService"/> class.
3743
/// </summary>
@@ -87,6 +93,16 @@ protected override DriverOptions GetDefaultDriverOptions()
8793
/// </summary>
8894
public bool OpenBrowserToolbox { get; set; }
8995

96+
/// <summary>
97+
/// Gets or sets the file path where log output should be written.
98+
/// </summary>
99+
/// <remarks>
100+
/// A <see langword="null"/> or <see cref="string.Empty"/> value indicates no log file to specify.
101+
/// This approach takes the process output and redirects it to a file because GeckoDriver does not
102+
/// offer a way to specify a log file path directly.
103+
/// </remarks>
104+
public string? LogPath { get; set; }
105+
90106
/// <summary>
91107
/// Gets or sets the level at which log output is displayed.
92108
/// </summary>
@@ -177,6 +193,75 @@ protected override string CommandLineArguments
177193
}
178194
}
179195

196+
/// <summary>
197+
/// Handles the event when the driver service process is starting.
198+
/// </summary>
199+
/// <param name="eventArgs">The event arguments containing information about the driver service process.</param>
200+
/// <remarks>
201+
/// This method initializes a log writer if a log path is specified and redirects output streams to capture logs.
202+
/// </remarks>
203+
protected override void OnDriverProcessStarting(DriverProcessStartingEventArgs eventArgs)
204+
{
205+
if (!string.IsNullOrEmpty(this.LogPath))
206+
{
207+
string? directory = Path.GetDirectoryName(this.LogPath);
208+
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
209+
{
210+
Directory.CreateDirectory(directory);
211+
}
212+
213+
// Initialize the log writer
214+
logWriter = new StreamWriter(this.LogPath, append: true) { AutoFlush = true };
215+
216+
// Configure process to redirect output
217+
eventArgs.DriverServiceProcessStartInfo.RedirectStandardOutput = true;
218+
eventArgs.DriverServiceProcessStartInfo.RedirectStandardError = true;
219+
}
220+
221+
base.OnDriverProcessStarting(eventArgs);
222+
}
223+
224+
/// <summary>
225+
/// Handles the event when the driver process has started.
226+
/// </summary>
227+
/// <param name="eventArgs">The event arguments containing information about the started driver process.</param>
228+
/// <remarks>
229+
/// This method reads the output and error streams asynchronously and writes them to the log file if available.
230+
/// </remarks>
231+
protected override void OnDriverProcessStarted(DriverProcessStartedEventArgs eventArgs)
232+
{
233+
if (logWriter == null) return;
234+
if (eventArgs.StandardOutputStreamReader != null)
235+
{
236+
_ = Task.Run(() => ReadStreamAsync(eventArgs.StandardOutputStreamReader));
237+
}
238+
239+
if (eventArgs.StandardErrorStreamReader != null)
240+
{
241+
_ = Task.Run(() => ReadStreamAsync(eventArgs.StandardErrorStreamReader));
242+
}
243+
244+
base.OnDriverProcessStarted(eventArgs);
245+
}
246+
247+
/// <summary>
248+
/// Disposes of the resources used by the <see cref="FirefoxDriverService"/> instance.
249+
/// </summary>
250+
/// <param name="disposing">A value indicating whether the method is being called from Dispose.</param>
251+
/// <remarks>
252+
/// If disposing is true, it disposes of the log writer if it exists.
253+
/// </remarks>
254+
protected override void Dispose(bool disposing)
255+
{
256+
if (logWriter != null && disposing)
257+
{
258+
logWriter.Dispose();
259+
logWriter = null;
260+
}
261+
262+
base.Dispose(disposing);
263+
}
264+
180265
/// <summary>
181266
/// Creates a default instance of the FirefoxDriverService.
182267
/// </summary>
@@ -258,4 +343,24 @@ private static string FirefoxDriverServiceFileName()
258343

259344
return fileName;
260345
}
346+
347+
private async Task ReadStreamAsync(StreamReader reader)
348+
{
349+
try
350+
{
351+
string? line;
352+
while ((line = await reader.ReadLineAsync()) != null)
353+
{
354+
if (logWriter != null)
355+
{
356+
logWriter.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} {line}");
357+
}
358+
}
359+
}
360+
catch (Exception ex)
361+
{
362+
// Log or handle the exception appropriately
363+
System.Diagnostics.Debug.WriteLine($"Error reading stream: {ex.Message}");
364+
}
365+
}
261366
}

dotnet/test/firefox/BUILD.bazel

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
load("//dotnet:defs.bzl", "dotnet_nunit_test_suite", "framework")
2+
3+
dotnet_nunit_test_suite(
4+
name = "LargeTests",
5+
size = "large",
6+
srcs = glob(
7+
[
8+
"**/*Test.cs",
9+
"**/*Tests.cs",
10+
],
11+
) + [
12+
"//dotnet/test/common:assembly-fixtures",
13+
],
14+
browsers = [
15+
"firefox",
16+
],
17+
data = [
18+
"//dotnet/test/common:test-data",
19+
],
20+
target_frameworks = ["net8.0"],
21+
deps = [
22+
"//dotnet/src/support",
23+
"//dotnet/src/webdriver:webdriver-net8.0",
24+
"//dotnet/test/common:fixtures",
25+
framework("nuget", "NUnit"),
26+
],
27+
)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// <copyright file="FirefoxDriverServiceTest.cs" company="Selenium Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
// </copyright>
19+
20+
using NUnit.Framework;
21+
using System.IO;
22+
23+
namespace OpenQA.Selenium.Firefox;
24+
25+
[TestFixture]
26+
public class FirefoxDriverServiceTest : DriverTestFixture
27+
{
28+
[Test]
29+
public void ShouldRedirectGeckoDriverLogsToFile()
30+
{
31+
FirefoxOptions options = new FirefoxOptions();
32+
string logPath = Path.GetTempFileName();
33+
options.LogLevel = FirefoxDriverLogLevel.Trace;
34+
35+
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService();
36+
service.LogPath = logPath;
37+
38+
IWebDriver driver2 = new FirefoxDriver(service, options);
39+
40+
try
41+
{
42+
Assert.That(File.Exists(logPath), Is.True);
43+
string logContent = File.ReadAllText(logPath);
44+
Assert.That(logContent, Does.Contain("geckodriver"));
45+
}
46+
finally
47+
{
48+
driver2.Quit();
49+
File.Delete(logPath);
50+
}
51+
}
52+
53+
}

0 commit comments

Comments
 (0)