Skip to content

Commit

Permalink
Add defensive code to circumvent test failures because of selenium ti…
Browse files Browse the repository at this point in the history
…meouts
  • Loading branch information
sensslen committed May 21, 2024
1 parent 89f193e commit 99d23b5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
38 changes: 24 additions & 14 deletions tests/NuGetUtility.Test/LicenseValidator/UrlToLicenseMappingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using NuGetUtility.LicenseValidator;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

namespace NuGetUtility.Test.LicenseValidator
{
Expand All @@ -15,22 +14,33 @@ public class UrlToLicenseMappingTest
[TestCaseSource(typeof(UrlToLicenseMapping), nameof(UrlToLicenseMapping.Default))]
public async Task License_Should_Be_Available_And_Match_Expected_License(KeyValuePair<Uri, string> mappedValue)
{
var options = new ChromeOptions();
options.AddArguments("--no-sandbox", "--disable-dev-shm-usage", "--headless");
var driver = new ChromeDriver(options);
try
int retryCount = 0;
while (true)
{
driver.Navigate().GoToUrl(mappedValue.Key.ToString());
var options = new ChromeOptions();
options.AddArguments("--no-sandbox", "--disable-dev-shm-usage", "--headless");
var driver = new ChromeDriver(options);
try
{
driver.Navigate().GoToUrl(mappedValue.Key.ToString());

IWait<IWebDriver> wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));

await Verify(driver.FindElement(By.TagName("body")).Text).HashParameters().UseStringComparer(CompareLicense);
}
finally
{
driver.Quit();
await Verify(driver.FindElement(By.TagName("body")).Text).HashParameters().UseStringComparer(CompareLicense);
return;
}
catch (Exception)
{
if (retryCount >= 3)
{
throw;
}
retryCount++;
}
finally
{
driver.Quit();
}
}

}

private Task<CompareResult> CompareLicense(string received, string verified, IReadOnlyDictionary<string, object> context)
Expand Down
4 changes: 2 additions & 2 deletions tests/NuGetUtility.Test/NuGetUtility.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="AutoFixture.AutoNSubstitute" Version="4.18.1" />
<PackageReference Include="AutoFixture.NUnit3" Version="4.18.1" />
<PackageReference Include="Bogus" Version="35.5.0" />
<PackageReference Include="Bogus" Version="35.5.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="NetArchTest.Rules" Version="1.3.2" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
Expand All @@ -30,7 +30,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Selenium.WebDriver" Version="4.19.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.21.0" />
<PackageReference Include="Verify.NUnit" Version="22.5.0" />
</ItemGroup>

Expand Down

0 comments on commit 99d23b5

Please sign in to comment.