Skip to content

Commit

Permalink
make sure chromedriver is properly closed...
Browse files Browse the repository at this point in the history
  • Loading branch information
sensslen committed May 21, 2024
1 parent 66a7490 commit ec6669f
Showing 1 changed file with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ namespace NuGetUtility.Test.LicenseValidator
[TestFixture]
public class UrlToLicenseMappingTest
{
[Parallelizable(scope: ParallelScope.All)]
[TestCaseSource(typeof(UrlToLicenseMapping), nameof(UrlToLicenseMapping.Default))]
public async Task License_Should_Be_Available_And_Match_Expected_License(KeyValuePair<Uri, string> mappedValue)
{
int retryCount = 0;
while (true)
{
var options = new ChromeOptions();
options.AddArguments("--no-sandbox", "--disable-dev-shm-usage", "--headless");
var driver = new ChromeDriver(options);
try
{
using var driver = new DisposableWebDriver();
driver.Navigate().GoToUrl(mappedValue.Key.ToString());

await Verify(driver.FindElement(By.TagName("body")).Text).HashParameters().UseStringComparer(CompareLicense);
Expand All @@ -36,10 +35,6 @@ public async Task License_Should_Be_Available_And_Match_Expected_License(KeyValu
TestContext.Out.WriteLine($"Failed to check license for the {retryCount} time - retrying");
TestContext.Out.WriteLine(e);
}
finally
{
driver.Quit();
}
}

}
Expand All @@ -48,5 +43,27 @@ private Task<CompareResult> CompareLicense(string received, string verified, IRe
{
return Task.FromResult(new CompareResult((!string.IsNullOrWhiteSpace(verified)) && received.Contains(verified)));
}

private sealed class DisposableWebDriver : IDisposable
{
private readonly IWebDriver _driver;

public DisposableWebDriver()
{
var options = new ChromeOptions();
options.AddArguments("--no-sandbox", "--disable-dev-shm-usage", "--headless");
_driver = new ChromeDriver(options);
}

public void Dispose()
{
_driver.Close();
_driver.Quit();
_driver.Dispose();
}

internal IWebElement FindElement(By by) => _driver.FindElement(by);
internal INavigation Navigate() => _driver.Navigate();
}
}
}

0 comments on commit ec6669f

Please sign in to comment.