Skip to content

Commit

Permalink
Merge pull request #446 from Lombiq/issue/OSOE-969
Browse files Browse the repository at this point in the history
OSOE-969: Add Downloads folder in temp and test dump directories
  • Loading branch information
Piedone authored Jan 20, 2025
2 parents 6f23d75 + 9a4020f commit d6d7a05
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions Lombiq.Tests.UI/Constants/DirectoryPaths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class DirectoryPaths
public const string SetupSnapshot = nameof(SetupSnapshot);
public const string Temp = nameof(Temp);
public const string Screenshots = nameof(Screenshots);
public const string Downloads = nameof(Downloads);

public static string GetTempDirectoryPath(params string[] subDirectoryNames) =>
Path.Combine([Environment.CurrentDirectory, Temp, .. subDirectoryNames]);
Expand Down
6 changes: 6 additions & 0 deletions Lombiq.Tests.UI/Services/BrowserConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,10 @@ public class BrowserConfiguration
/// Gets a list of command line arguments that were passed to the driver during the driver instance creation.
/// </summary>
public IList<string> Arguments { get; } = [];

/// <summary>
/// Gets or sets the context ID, to be used during driver creation when the <see cref="UITestContext"/> does not
/// exist yet.
/// </summary>
internal string UITestContextId { get; set; }
}
13 changes: 13 additions & 0 deletions Lombiq.Tests.UI/Services/UITestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ public class UITestContext
/// </summary>
public string ScreenshotsDirectoryPath => GetTempSubDirectoryPath(DirectoryPaths.Screenshots);

/// <summary>
/// Gets the absolute path of the <see cref="DirectoryPaths.Downloads"/> subdirectory inside the current test
/// instance's <see cref="DirectoryPaths.Temp"/> directory.
/// </summary>
public string DownloadsDirectoryPath => GetTempSubDirectoryPath(DirectoryPaths.Downloads);

// This is a central context object, we need the data to be passed in the constructor.
#pragma warning disable S107 // Methods should not have too many parameters
public UITestContext(
Expand Down Expand Up @@ -312,6 +318,13 @@ public void SwitchCurrentTenant(string tenantName, Uri baseUri)
public string GetTempSubDirectoryPath(params string[] subDirectoryNames) =>
DirectoryPaths.GetTempDirectoryPath([Id, .. subDirectoryNames]);

/// <summary>
/// Returns a path in the <see cref="DirectoryPaths.Downloads"/> subdirectory inside the current test instance's
/// <see cref="DirectoryPaths.Temp"/> directory.
/// </summary>
public string GetDownloadFilePath(params string[] subDirectoryNames) =>
DirectoryPaths.GetTempDirectoryPath([Id, DirectoryPaths.Downloads, .. subDirectoryNames]);

private bool IsAlert()
{
// If there's an alert (which can happen mostly after a click but also after navigating) then all other driver
Expand Down
8 changes: 8 additions & 0 deletions Lombiq.Tests.UI/Services/UITestExecutionSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ Task AzureBlobStorageManagerBeforeTakeSnapshotHandlerAsync(OrchardCoreAppStartCo
private async Task<UITestContext> CreateContextAsync(Uri testStartRelativeUri)
{
var contextId = Guid.NewGuid().ToString();
_configuration.BrowserConfiguration.UITestContextId = contextId;

FileSystemHelper.EnsureDirectoryExists(DirectoryPaths.GetTempDirectoryPath(contextId));

Expand Down Expand Up @@ -791,6 +792,13 @@ private async Task CaptureBrowserUsingDumpsAsync(string debugInformationPath)
await CreateScreenshotsDumpAsync(debugInformationPath);
}

if (_dumpConfiguration.CaptureDownloads && Directory.Exists(_context.DownloadsDirectoryPath))
{
FileSystem.CopyDirectory(
_context.DownloadsDirectoryPath,
Path.Combine(debugInformationPath, DirectoryPaths.Downloads));
}

if (_dumpConfiguration.CaptureHtmlSource)
{
_context.RefreshCurrentAtataContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class UITestExecutorTestDumpConfiguration
public bool CreateTestDump { get; set; } = true;
public bool CaptureAppSnapshot { get; set; } = true;
public bool CaptureScreenshots { get; set; } = true;
public bool CaptureDownloads { get; set; } = true;
public bool CaptureHtmlSource { get; set; } = true;
public bool CaptureBrowserLog { get; set; } = true;
}
20 changes: 20 additions & 0 deletions Lombiq.Tests.UI/Services/WebDriverFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Atata.WebDriverSetup;
using Lombiq.HelpfulLibraries.Cli.Helpers;
using Lombiq.HelpfulLibraries.Common.Utilities;
using Lombiq.Tests.UI.Constants;
using Lombiq.Tests.UI.Extensions;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
Expand Down Expand Up @@ -113,6 +115,13 @@ public static Task<Func<FirefoxDriver>> CreateFirefoxDriverAsync(BrowserConfigur
options.SetPreference("browser.preferences.defaultPerformanceSettings.enabled", preferenceValue: false);
options.SetPreference("layers.acceleration.disabled", preferenceValue: true);

// Set the download path to inside the context-specific temp directory to avoid clashes from parallel
// tests, and to make it available for test dumps.
options.SetPreference("browser.download.folderList", 2);
options.SetPreference("browser.download.dir", PrepareDownloadDirectory(configuration));
options.SetPreference("browser.download.useDownloadDir", preferenceValue: true);
options.SetPreference("pdfjs.disabled", preferenceValue: true);

if (configuration.Headless) options.AddArgument("--headless");

configuration.BrowserOptionsConfigurator?.Invoke(options);
Expand Down Expand Up @@ -183,6 +192,10 @@ private static TDriverOptions SetCommonChromiumOptions<TDriverOptions>(

if (configuration.Headless) options.AddArgument("headless");

// Set the download path to inside the context-specific temp directory to avoid clashes from parallel tests, and
// to make it available for test dumps.
options.AddUserProfilePreference("download.default_directory", PrepareDownloadDirectory(configuration));

return options;
}

Expand Down Expand Up @@ -230,6 +243,13 @@ private static void AutoSetup(string browserName)
lock (_setupLock) DriverSetup.AutoSetUp(browserName);
}

private static string PrepareDownloadDirectory(BrowserConfiguration configuration)
{
var downloadPath = DirectoryPaths.GetTempDirectoryPath(configuration.UITestContextId, DirectoryPaths.Downloads);
FileSystemHelper.EnsureDirectoryExists(downloadPath);
return downloadPath;
}

private sealed class ChromeConfiguration
{
public ChromeOptions Options { get; init; }
Expand Down

0 comments on commit d6d7a05

Please sign in to comment.