Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions examples/dotnet/SeleniumDocs/Browsers/ChromeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void Cleanup()
driver?.Quit();
if (_logLocation != null && File.Exists(_logLocation))
{
File.Delete(_logLocation);
LogFile.TryDelete(_logLocation);
}
}

Expand Down Expand Up @@ -108,7 +108,7 @@ public void LogsToFile()

driver = new ChromeDriver(service);
driver.Quit(); // Close the Service log file before reading
var lines = File.ReadLines(GetLogLocation());
var lines = LogFile.ReadLines(GetLogLocation());
Assert.IsNotNull(lines.FirstOrDefault(line => line.Contains("Starting ChromeDriver")));
}

Expand All @@ -123,7 +123,7 @@ public void LogsLevel()
driver = new ChromeDriver(service);

driver.Quit(); // Close the Service log file before reading
var lines = File.ReadLines(GetLogLocation());
var lines = LogFile.ReadLines(GetLogLocation());
Assert.IsNotNull(lines.FirstOrDefault(line => line.Contains("[DEBUG]:")));
}

Expand All @@ -140,7 +140,7 @@ public void ConfigureDriverLogs()
driver = new ChromeDriver(service);

driver.Quit(); // Close the Service log file before reading
var lines = File.ReadLines(GetLogLocation());
var lines = LogFile.ReadLines(GetLogLocation());
var regex = new Regex(@"\[\d\d-\d\d-\d\d\d\d \d\d:\d\d:\d\d\.\d+\]");
Assert.IsNotNull(lines.FirstOrDefault(line => regex.Matches(line).Count > 0));
}
Expand All @@ -157,7 +157,7 @@ public void DisableBuildCheck()
driver = new ChromeDriver(service);
driver.Quit(); // Close the Service log file before reading
var expected = "[WARNING]: You are using an unsupported command-line switch: --disable-build-check";
var lines = File.ReadLines(GetLogLocation());
var lines = LogFile.ReadLines(GetLogLocation());
Assert.IsNotNull(lines.FirstOrDefault(line => line.Contains(expected)));
}

Expand Down
4 changes: 2 additions & 2 deletions examples/dotnet/SeleniumDocs/Browsers/EdgeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ private static string[] ReadLogLines(string path)
{
try
{
return File.ReadAllLines(path);
return LogFile.ReadLines(path);
}
catch (IOException) when (attempt < maxAttempts)
{
Thread.Sleep(500);
}
}

return File.ReadAllLines(path);
return LogFile.ReadLines(path);
}

private string GetLogLocation()
Expand Down
8 changes: 4 additions & 4 deletions examples/dotnet/SeleniumDocs/Browsers/FirefoxTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void Cleanup()
driver?.Quit();
if (!String.IsNullOrEmpty(_logLocation) && File.Exists(_logLocation))
{
File.Delete(_logLocation);
LogFile.TryDelete(_logLocation);
}
if (_tempPath != null && Directory.Exists(_tempPath))
{
Expand Down Expand Up @@ -65,7 +65,7 @@ public void LogsToFile()

driver = new FirefoxDriver(service);
driver.Quit(); // Close the Service log file before reading
var lines = File.ReadLines(GetLogLocation());
var lines = LogFile.ReadLines(GetLogLocation());
Assert.IsNotNull(lines.FirstOrDefault(line => line.Contains("geckodriver INFO Listening on")));
}

Expand Down Expand Up @@ -97,7 +97,7 @@ public void LogsLevel()

driver = new FirefoxDriver(service);
driver.Quit(); // Close the Service log file before reading
var lines = File.ReadLines(GetLogLocation());
var lines = LogFile.ReadLines(GetLogLocation());
Assert.IsNotNull(lines.FirstOrDefault(line => line.Contains("Marionette\tDEBUG")));
}

Expand All @@ -111,7 +111,7 @@ public void StopsTruncatingLogs()

driver = new FirefoxDriver(service);
driver.Quit(); // Close the Service log file before reading
var lines = File.ReadLines(GetLogLocation());
var lines = LogFile.ReadLines(GetLogLocation());
Assert.IsNull(lines.FirstOrDefault(line => line.Contains(" ... ")));
}

Expand Down
4 changes: 2 additions & 2 deletions examples/dotnet/SeleniumDocs/Browsers/InternetExplorerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void Cleanup()
_driver?.Quit();
if (_logLocation != null && File.Exists(_logLocation))
{
File.Delete(_logLocation);
LogFile.TryDelete(_logLocation);
}
if (_tempPath != null && File.Exists(_tempPath))
{
Expand Down Expand Up @@ -55,7 +55,7 @@ public void LogsLevel()

_driver = new InternetExplorerDriver(service);
_driver.Quit(); // Close the Service log file before reading
var lines = File.ReadLines(GetLogLocation());
var lines = LogFile.ReadLines(GetLogLocation());
Assert.IsNotNull(lines.FirstOrDefault(line => line.Contains("Invalid capability setting: timeouts is type null")));
}

Expand Down
48 changes: 48 additions & 0 deletions examples/dotnet/SeleniumDocs/LogFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

namespace SeleniumDocs
{
// Driver log files can still be open by the driver process after Quit(),
// especially on Windows. Share the file instead of requesting exclusive access.
public static class LogFile
{
public static string[] ReadLines(string path)
{
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite | FileShare.Delete);
using var reader = new StreamReader(stream);
var lines = new List<string>();
string line;
while ((line = reader.ReadLine()) != null)
{
lines.Add(line);
}
return lines.ToArray();
}

public static void TryDelete(string path)
{
const int maxAttempts = 10;
for (var attempt = 1; attempt <= maxAttempts; attempt++)
{
try
{
File.Delete(path);
return;
}
catch (IOException) when (attempt < maxAttempts)
{
// Still held by the driver process; give it time to exit.
Thread.Sleep(200);
}
catch (IOException e)
{
Console.Error.WriteLine($"Could not delete driver log {path}: {e.Message}");
}
}
}
}
}
Loading