Skip to content

Commit

Permalink
Update to Selenium 4.19.0 (#116)
Browse files Browse the repository at this point in the history
* Update to Selenium 4.19.0
 and use .NET 8.0 in Tests project

* Migrate to NUnit 4, use new language constructions from suggestions

* Fix consistency/maintainability coding issues
  • Loading branch information
mialeska authored Apr 17, 2024
1 parent 0c5c863 commit 96c02b2
Show file tree
Hide file tree
Showing 30 changed files with 222 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@
<ItemGroup>
<PackageReference Include="DotNetSeleniumExtras.PageObjects" Version="3.11.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageReference Include="NLog" Version="5.2.8" />
<PackageReference Include="Selenium.Support" Version="4.17.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.17.0" />
<PackageReference Include="SkiaSharp" Version="2.88.7" />
<PackageReference Include="Selenium.Support" Version="4.19.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.19.0" />
<PackageReference Include="SkiaSharp" Version="2.88.8" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,33 @@ public class AqualityServicesTests
[Test]
public void Should_BePossibleTo_RegisterCustomServices()
{
Assert.IsInstanceOf<TestTimeoutConfiguration>(TestAqualityServices.ServiceProvider.GetService<ITimeoutConfiguration>());
Assert.That(TestAqualityServices.ServiceProvider.GetService<ITimeoutConfiguration>(), Is.InstanceOf<TestTimeoutConfiguration>());
}

[Test]
public void Should_BePossibleTo_GetCustomValues()
{
var timeoutConfiguration = TestAqualityServices.ServiceProvider.GetService<ITimeoutConfiguration>() as TestTimeoutConfiguration;
Assert.AreEqual(SpecialTimeoutValue, timeoutConfiguration.CustomTimeout);
Assert.That(timeoutConfiguration.CustomTimeout, Is.EqualTo(SpecialTimeoutValue));
}

[Test]
public void Should_BePossibleTo_GetCustomLoggerValues()
{
TestAqualityServices.SetStartup(new CustomStartup());
var timeoutConfiguration = TestAqualityServices.ServiceProvider.GetService<ILoggerConfiguration>() as CustomLoggerConfiguration;
Assert.AreEqual(SpecialLogger, timeoutConfiguration.CustomLogger);
Assert.That(timeoutConfiguration.CustomLogger, Is.EqualTo(SpecialLogger));
}

[Test]
public void Should_BePossibleTo_RegisterCustomServices_WithCustomSettingsFile()
{
Assert.AreEqual(SpecialLanguageValue, TestAqualityServices.ServiceProvider.GetService<ILoggerConfiguration>().Language);
Assert.That(TestAqualityServices.ServiceProvider.GetService<ILoggerConfiguration>().Language, Is.EqualTo(SpecialLanguageValue));
}

private class TestAqualityServices : AqualityServices<IApplication>
{
private static ThreadLocal<TestStartup> startup = new ThreadLocal<TestStartup>();
private static readonly ThreadLocal<TestStartup> startup = new();

private static IApplication Application => GetApplication(StartApplicationFunction, () => startup.Value.ConfigureServices(new ServiceCollection(), services => Application));

Expand Down Expand Up @@ -85,24 +85,14 @@ public override IServiceCollection ConfigureServices(IServiceCollection services
}
}

private class TestTimeoutConfiguration : TimeoutConfiguration
private class TestTimeoutConfiguration(ISettingsFile settingsFile) : TimeoutConfiguration(settingsFile)
{
public TestTimeoutConfiguration(ISettingsFile settingsFile) : base(settingsFile)
{
CustomTimeout = SpecialTimeoutValue;
}

public TimeSpan CustomTimeout { get; }
public TimeSpan CustomTimeout { get; } = SpecialTimeoutValue;
}

private class CustomLoggerConfiguration : LoggerConfiguration
private class CustomLoggerConfiguration(ISettingsFile settingsFile) : LoggerConfiguration(settingsFile)
{
public CustomLoggerConfiguration(ISettingsFile settingsFile) : base(settingsFile)
{
CustomLogger = SpecialLogger;
}

public string CustomLogger { get; }
public string CustomLogger { get; } = SpecialLogger;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Aquality.Selenium.Core.Tests.Applications.Browser
public class ApplicationNotStartedTests : TestWithBrowser
{
private static readonly Type[] TypesNotRequireApplication =
{
[
typeof(IElementActionRetrier),
typeof(IConditionalWait),
typeof(Logger),
Expand All @@ -24,13 +24,13 @@ public class ApplicationNotStartedTests : TestWithBrowser
typeof(ITimeoutConfiguration),
typeof(ILoggerConfiguration),
typeof(IRetryConfiguration)
};
];

[TestCaseSource(nameof(TypesNotRequireApplication))]
public void Should_NotStartApplication_ForServiceResolving(Type type)
{
Assert.IsNotNull(ServiceProvider.GetRequiredService(type));
Assert.IsFalse(AqualityServices.IsApplicationStarted);
Assert.That(ServiceProvider.GetRequiredService(type), Is.Not.Null);
Assert.That(AqualityServices.IsApplicationStarted, Is.False);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,38 @@ public class CachedElementTests : TestWithBrowser
private static readonly By ContentLoc = By.Id("checkbox");
private static readonly By StartLoc = By.XPath("//*[@id='start']//button");
private static readonly By LoadingLoc = By.Id("loading");
private static readonly Uri DynamicContentUrl = new Uri($"{TestSite}/dynamic_controls");
private static readonly Uri DynamicLoadingUrl = new Uri($"{TestSite}/dynamic_loading/1");
private static readonly Uri DynamicContentUrl = new($"{TestSite}/dynamic_controls");
private static readonly Uri DynamicLoadingUrl = new($"{TestSite}/dynamic_loading/1");

private const string ElementCacheVariableName = "elementCache.isEnabled";

private static readonly Func<IElementStateProvider, bool>[] StateFunctionsFalseWhenElementStale
= new Func<IElementStateProvider, bool>[]
{
=
[
state => state.IsDisplayed,
state => state.IsExist,
state => !state.WaitForNotDisplayed(TimeSpan.Zero),
state => !state.WaitForNotExist(TimeSpan.Zero),
};
];

private static readonly Func<IElementStateProvider, bool>[] StateFunctionsTrueWhenElementStaleWhichRetriveElement
= new Func<IElementStateProvider, bool>[]
{
=
[
state => state.IsEnabled,
state => state.IsClickable,
state => state.WaitForDisplayed(TimeSpan.Zero),
state => state.WaitForExist(TimeSpan.Zero),
state => state.WaitForEnabled(TimeSpan.Zero),
state => !state.WaitForNotEnabled(TimeSpan.Zero),
};
];

private static readonly Func<IElementStateProvider, bool>[] StateFunctionsThrowNoSuchElementException
= new Func<IElementStateProvider, bool>[]
{
=
[
state => state.IsEnabled,
state => state.WaitForEnabled(TimeSpan.Zero),
state => !state.WaitForNotEnabled(TimeSpan.Zero),
};
];

private static IConditionalWait ConditionalWait => AqualityServices.ServiceProvider.GetRequiredService<IConditionalWait>();

Expand Down Expand Up @@ -80,7 +80,7 @@ public void Should_ReturnFalse_AtWaitForDisplayed_WhenElementIsNotDisplayed()
var loader = new Label(LoadingLoc, "loader", ElementState.Displayed);
StartLoading();
WaitForLoading(loader);
Assert.IsFalse(loader.State.WaitForDisplayed(TimeSpan.Zero), nameof(Should_ReturnFalse_AtWaitForDisplayed_WhenElementIsNotDisplayed));
Assert.That(loader.State.WaitForDisplayed(TimeSpan.Zero), Is.False, nameof(Should_ReturnFalse_AtWaitForDisplayed_WhenElementIsNotDisplayed));
}

[Test]
Expand All @@ -89,7 +89,7 @@ public void Should_ReturnTrue_AtWaitForExist_WhenElementIsNotDisplayed()
var loader = new Label(LoadingLoc, "loader", ElementState.Displayed);
StartLoading();
WaitForLoading(loader);
Assert.IsTrue(loader.State.WaitForExist(TimeSpan.Zero), nameof(Should_ReturnTrue_AtWaitForExist_WhenElementIsNotDisplayed));
Assert.That(loader.State.WaitForExist(TimeSpan.Zero), nameof(Should_ReturnTrue_AtWaitForExist_WhenElementIsNotDisplayed));
}

[Test]
Expand All @@ -98,10 +98,10 @@ public void Should_BeStale_WhenBecameInvisible()
StartLoading();
var loader = new Label(LoadingLoc, "loader", ElementState.Displayed);
Assume.That(loader.State.WaitForDisplayed(), "Loader should be displayed in the beginning");
Assert.IsTrue(ConditionalWait.WaitFor(() => loader.Cache.IsStale), "Loader should become invisible and be treated as stale");
Assert.IsFalse(loader.State.IsDisplayed, "Invisible loader should be not displayed");
Assert.IsFalse(loader.State.IsExist, "Loader that was displayed previously and become invisible should be treated as disappeared");
Assert.IsTrue(loader.State.WaitForExist(TimeSpan.Zero), "When waiting for existance, we should get an actual element's state");
Assert.That(ConditionalWait.WaitFor(() => loader.Cache.IsStale), "Loader should become invisible and be treated as stale");
Assert.That(loader.State.IsDisplayed, Is.False, "Invisible loader should be not displayed");
Assert.That(loader.State.IsExist, Is.False, "Loader that was displayed previously and become invisible should be treated as disappeared");
Assert.That(loader.State.WaitForExist(TimeSpan.Zero), "When waiting for existance, we should get an actual element's state");
}

[Test]
Expand All @@ -113,7 +113,7 @@ public void Should_RefreshElement_WhenItIsStale()
var exToString = example.GetElement().ToString();
AqualityServices.Application.Driver.Navigate().Refresh();
var newToString = example.GetElement().ToString();
Assert.AreNotEqual(exToString, newToString);
Assert.That(newToString, Is.Not.EqualTo(exToString));
}

[Test]
Expand Down Expand Up @@ -159,7 +159,7 @@ private static void AssertStateConditionAfterReopen(Func<IElementStateProvider,

Assume.That(testElement, Is.Not.Null);
OpenDynamicContent();
Assert.AreEqual(expectedValue, stateCondition(testElement.State),
Assert.That(stateCondition(testElement.State), Is.EqualTo(expectedValue),
"Element state condition is not expected after reopening the window");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ namespace Aquality.Selenium.Core.Tests.Applications.Browser
{
public class ConditionalWaitTests : TestWithBrowser
{
private static readonly Uri WikiURL = new Uri("https://wikipedia.org");
private static readonly Uri WikiURL = new("https://wikipedia.org");
private static readonly TimeSpan LittleTimeout = TimeSpan.FromSeconds(1);
private static readonly TimeSpan PollingInterval = AqualityServices.ServiceProvider.GetRequiredService<ITimeoutConfiguration>().PollingInterval;

private static readonly Action<Func<bool>, IList<Type>>[] WaitWithHandledException
= new Action<Func<bool>, IList<Type>>[]
{
=
[
(condition, handledExceptions) => ConditionalWait.WaitFor(condition, timeout: LittleTimeout, exceptionsToIgnore: handledExceptions),
(condition, handledExceptions) => ConditionalWait.WaitFor(driver => condition(), timeout: LittleTimeout, exceptionsToIgnore: handledExceptions),
(condition, handledExceptions) => ConditionalWait.WaitForTrue(condition, timeout: LittleTimeout, exceptionsToIgnore: handledExceptions)
};
];

private static readonly Func<Func<bool>, IList<Type>, Task>[] WaitWithHandledExceptionAsync
= new Func<Func<bool>, IList<Type>, Task>[]
{
=
[
(condition, handledExceptions) => ConditionalWait.WaitForAsync(condition, timeout: LittleTimeout, exceptionsToIgnore: handledExceptions),
(condition, handledExceptions) => ConditionalWait.WaitForTrueAsync(condition, timeout: LittleTimeout, exceptionsToIgnore: handledExceptions)
};
];

private static IConditionalWait ConditionalWait => AqualityServices.ServiceProvider.GetRequiredService<IConditionalWait>();

Expand Down Expand Up @@ -97,7 +97,7 @@ public void Should_BePossibleTo_UseConditionalWait_WithElementFinder()
{
bool elementFinderCondition() => ServiceProvider.GetRequiredService<IElementFinder>()
.FindElements(By.XPath("//*[contains(., 'wikipedia')]"), timeout: LittleTimeout).Count > 0;
Assert.IsFalse(elementFinderCondition());
Assert.That(elementFinderCondition(), Is.False);
Assert.DoesNotThrow(() => ConditionalWait.WaitFor(driver =>
{
GoToUrl(WikiURL, driver);
Expand All @@ -113,9 +113,9 @@ public void Should_BePossibleTo_UseConditionalWait_ForAsyncWaiting()
stopWatch.Start();
Assert.DoesNotThrow(() => returnedResult = ConditionalWait.WaitForAsync(() => result = false, LittleTimeout).Result);
stopWatch.Stop();
Assert.IsFalse(result, $"{nameof(ConditionalWait.WaitForAsync)} should work at least once");
Assert.IsFalse(returnedResult, $"{nameof(ConditionalWait.WaitForAsync)} should return valid result");
Assert.AreEqual(LittleTimeout.TotalSeconds, stopWatch.Elapsed.TotalSeconds, PollingInterval.TotalSeconds * 2,
Assert.That(result, Is.False, $"{nameof(ConditionalWait.WaitForAsync)} should work at least once");
Assert.That(returnedResult, Is.False, $"{nameof(ConditionalWait.WaitForAsync)} should return valid result");
Assert.That(stopWatch.Elapsed.TotalSeconds, Is.EqualTo(LittleTimeout.TotalSeconds).Within(PollingInterval.TotalSeconds * 2),
$"{nameof(ConditionalWait.WaitForAsync)} should wait correct time");
}

Expand All @@ -131,9 +131,9 @@ public void Should_BePossibleTo_UseConditionalWait_ForAsyncWaitingForTrue()
$"{nameof(ConditionalWait.WaitForTrueAsync)} should not fail after the calling");
Assume.That(awaitableResult, Is.Not.Null);
Assert.ThrowsAsync<TimeoutException>(async () => await awaitableResult, $"{nameof(ConditionalWait.WaitForTrueAsync)} should throw when awaited");
Assert.IsFalse(result, $"{nameof(ConditionalWait.WaitForTrueAsync)} should work");
Assert.That(result, Is.False, $"{nameof(ConditionalWait.WaitForTrueAsync)} should work");
stopWatch.Stop();
Assert.AreEqual(LittleTimeout.TotalSeconds, stopWatch.Elapsed.TotalSeconds, PollingInterval.TotalSeconds * 2,
Assert.That(stopWatch.Elapsed.TotalSeconds, Is.EqualTo(LittleTimeout.TotalSeconds).Within(PollingInterval.TotalSeconds * 2),
$"{nameof(ConditionalWait.WaitForTrueAsync)} should wait correct time");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@

namespace Aquality.Selenium.Core.Tests.Applications.Browser.Elements
{
public class Label : WebElement
public class Label(By locator, string name, ElementState state) : WebElement(locator, name, state)
{
public Label(By locator, string name, ElementState state) : base(locator, name, state)
{
}

protected override string ElementType { get; } = "Label";

public new IElementCacheHandler Cache => base.Cache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@

namespace Aquality.Selenium.Core.Tests.Applications.Browser.Elements
{
internal class WebElementFactory : ElementFactory
internal class WebElementFactory(IConditionalWait conditionalWait, IElementFinder elementFinder, ILocalizationManager localizationManager) : ElementFactory(conditionalWait, elementFinder, localizationManager)
{
public WebElementFactory(IConditionalWait conditionalWait, IElementFinder elementFinder, ILocalizationManager localizationManager) : base(conditionalWait, elementFinder, localizationManager)
{
}

protected override By GenerateXpathLocator(By baseLocator, IWebElement webElement, int elementIndex)
{
return baseLocator.ToString().StartsWith("By.XPath")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ namespace Aquality.Selenium.Core.Tests.Applications.Browser
{
public class FindChildElementsTests : FindElementsTests
{
private readonly Label customParent = new Label(By.XPath("//div[contains(@class,'figure')]"),
private readonly Label customParent = new(By.XPath("//div[contains(@class,'figure')]"),
"custom parent", ElementState.ExistsInAnyState);
protected override By HiddenElementsLoc => By.XPath(".//h5");
protected override By DisplayedElementsLoc => By.XPath(".//img[@alt='User Avatar']");
protected override By NotExistElementLoc => By.XPath(".//div[@class='testtest']");

private static readonly By[] SupportedLocators = new By[]
{
private static readonly By[] SupportedLocators =
[
By.XPath("//img"),
By.XPath(".//img"),
By.TagName("img")
};
];

protected override IList<T> FindElements<T>(By locator, string name = null, ElementSupplier<T> supplier = null,
ElementsCount expectedCount = ElementsCount.Any, ElementState state = ElementState.Displayed)
Expand All @@ -34,7 +34,7 @@ public void Should_GetCorrectNumberOfChilds_ForRelativeChildLocator(
{
var expectedCount = 3;
var elementsCount = ElementFactory.FindChildElements<Label>(customParent, childRelativeLocator).Count;
Assert.AreEqual(expectedCount, elementsCount,
Assert.That(elementsCount, Is.EqualTo(expectedCount),
$"Elements count for relative locator [{childRelativeLocator}] should be {expectedCount}");
}

Expand Down Expand Up @@ -68,9 +68,9 @@ public void Should_SetXPathLocator_InFindChildElements_IfBothParentAndChildHaveS

private void CheckChildLocatorIsXpathAndStartsFromParent(string childLocatorString)
{
StringAssert.StartsWith("By.XPath", childLocatorString);
Assert.That(childLocatorString, Does.StartWith("By.XPath"));
var parentLocatorString = customParent.Locator.ToString();
StringAssert.Contains(parentLocatorString.Substring(parentLocatorString.IndexOf(':') + 1).Trim(), childLocatorString);
Assert.That(childLocatorString, Contains.Substring(parentLocatorString[(parentLocatorString.IndexOf(':') + 1)..].Trim()));
}
}
}
Loading

0 comments on commit 96c02b2

Please sign in to comment.