Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSOE-664: Downloading the Chrome WebDriver fails with Chrome >=15 in Lombiq.UITestingToolbox #303

Merged
merged 7 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions Lombiq.Tests.UI/Lombiq.Tests.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<PackageReference Include="Atata.Bootstrap" Version="2.1.0" />
<PackageReference Include="Atata.HtmlValidation" Version="2.3.0" />
<PackageReference Include="Atata.WebDriverExtras" Version="2.2.0" />
<PackageReference Include="Atata.WebDriverSetup" Version="2.7.0" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.16.0" />
<PackageReference Include="Ben.Demystifier" Version="0.4.1" />
<PackageReference Include="Codeuctivity.ImageSharpCompare" Version="3.0.156" />
Expand Down
28 changes: 8 additions & 20 deletions Lombiq.Tests.UI/Services/WebDriverFactory.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Atata.WebDriverSetup;
Piedone marked this conversation as resolved.
Show resolved Hide resolved
using Lombiq.Tests.UI.Extensions;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
Expand All @@ -9,15 +10,12 @@
using System.Collections.Concurrent;
using System.IO;
using System.Net;
using WebDriverManager;
using WebDriverManager.DriverConfigs;
using WebDriverManager.DriverConfigs.Impl;

namespace Lombiq.Tests.UI.Services;

public static class WebDriverFactory
{
private static readonly ConcurrentDictionary<string, Lazy<bool>> _driverSetups = new();
private static readonly ConcurrentDictionary<string, Lazy<string>> _driverSetups = new();

public static ChromeDriver CreateChromeDriver(BrowserConfiguration configuration, TimeSpan pageLoadTimeout)
{
Expand Down Expand Up @@ -58,11 +56,11 @@ ChromeDriver CreateDriverInner(ChromeDriverService service)
return CreateDriverInner(ChromeDriverService.CreateDefaultService(driverPath));
}

return CreateDriver(new ChromeConfig(), () => CreateDriverInner(service: null));
return CreateDriver(BrowserNames.Chrome, () => CreateDriverInner(service: null));
}

public static EdgeDriver CreateEdgeDriver(BrowserConfiguration configuration, TimeSpan pageLoadTimeout) =>
CreateDriver(new EdgeConfig(), () =>
CreateDriver(BrowserNames.Edge, () =>
{
var options = new EdgeOptions().SetCommonOptions();

Expand Down Expand Up @@ -93,11 +91,11 @@ public static FirefoxDriver CreateFirefoxDriver(BrowserConfiguration configurati

configuration.BrowserOptionsConfigurator?.Invoke(options);

return CreateDriver(new FirefoxConfig(), () => new FirefoxDriver(options).SetCommonTimeouts(pageLoadTimeout));
return CreateDriver(BrowserNames.Firefox, () => new FirefoxDriver(options).SetCommonTimeouts(pageLoadTimeout));
}

public static InternetExplorerDriver CreateInternetExplorerDriver(BrowserConfiguration configuration, TimeSpan pageLoadTimeout) =>
CreateDriver(new InternetExplorerConfig(), () =>
CreateDriver(BrowserNames.InternetExplorer, () =>
{
var options = new InternetExplorerOptions().SetCommonOptions();

Expand Down Expand Up @@ -164,7 +162,7 @@ private static TDriver SetCommonTimeouts<TDriver>(this TDriver driver, TimeSpan
return driver;
}

private static TDriver CreateDriver<TDriver>(IDriverConfig driverConfig, Func<TDriver> driverFactory)
private static TDriver CreateDriver<TDriver>(string browserName, Func<TDriver> driverFactory)
where TDriver : IWebDriver
{
// We could just use VersionResolveStrategy.MatchingBrowser as this is what DriverManager.SetUpDriver() does.
Expand All @@ -173,20 +171,10 @@ private static TDriver CreateDriver<TDriver>(IDriverConfig driverConfig, Func<TD

try
{
// Firefox: The FirefoxConfig.GetMatchingBrowserVersion() resolves the browser version but not the
// geckodriver version.
version = driverConfig is FirefoxConfig
? driverConfig.GetLatestVersion()
: driverConfig.GetMatchingBrowserVersion();

// While SetUpDriver() does locking and caches the driver it's faster not to do any of that if the setup was
// already done. For 100 such calls it's around 16s vs <100ms. The Lazy<T> trick taken from:
// https://stackoverflow.com/a/31637510/220230
_ = _driverSetups.GetOrAdd(driverConfig.GetName(), _ => new Lazy<bool>(() =>
{
new DriverManager().SetUpDriver(driverConfig, version);
return true;
})).Value;
version = _driverSetups.GetOrAdd(browserName, name => new Lazy<string>(() => DriverSetup.AutoSetUp(name).BrowserName)).Value;
Piedone marked this conversation as resolved.
Show resolved Hide resolved

return driverFactory();
}
Expand Down