Skip to content

Commit c38dfe2

Browse files
dzed-bymialeska
andauthored
Migration to selenium 4 (#93)
* Updated dependencies, test updated * azure-pipelines.yml update Co-authored-by: Alaksiej Mialeška <[email protected]>
1 parent 308abff commit c38dfe2

File tree

17 files changed

+107
-67
lines changed

17 files changed

+107
-67
lines changed

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Applications/IApplication.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using OpenQA.Selenium.Remote;
2+
using OpenQA.Selenium;
33

44
namespace Aquality.Selenium.Core.Applications
55
{
@@ -11,7 +11,7 @@ public interface IApplication
1111
/// <summary>
1212
/// Current instance of driver
1313
/// </summary>
14-
RemoteWebDriver Driver { get; }
14+
WebDriver Driver { get; }
1515

1616
/// <summary>
1717
/// Defines if the application is already started or not.

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Aquality.Selenium.Core.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@
4343
</None>
4444
</ItemGroup>
4545
<ItemGroup>
46+
<PackageReference Include="DotNetSeleniumExtras.PageObjects" Version="3.11.0" />
4647
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
4748
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
4849
<PackageReference Include="NLog" Version="4.7.10" />
49-
<PackageReference Include="Selenium.Support" Version="3.141.0" />
50-
<PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
50+
<PackageReference Include="Selenium.Support" Version="4.1.0" />
51+
<PackageReference Include="Selenium.WebDriver" Version="4.1.0" />
5152
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
5253
</ItemGroup>
5354
</Project>

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Elements/Element.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using Aquality.Selenium.Core.Visualization;
1010
using Aquality.Selenium.Core.Waitings;
1111
using OpenQA.Selenium;
12-
using OpenQA.Selenium.Remote;
1312

1413
namespace Aquality.Selenium.Core.Elements
1514
{
@@ -118,13 +117,13 @@ public string GetAttribute(string attr)
118117
return value;
119118
}
120119

121-
public virtual RemoteWebElement GetElement(TimeSpan? timeout = null)
120+
public virtual WebElement GetElement(TimeSpan? timeout = null)
122121
{
123122
try
124123
{
125124
return CacheConfiguration.IsEnabled
126125
? Cache.GetElement(timeout)
127-
: (RemoteWebElement) Finder.FindElement(Locator, elementState, timeout, Name);
126+
: (WebElement) Finder.FindElement(Locator, elementState, timeout, Name);
128127
}
129128
catch (NoSuchElementException ex) when (LoggerConfiguration.LogPageSource)
130129
{

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Elements/ElementCacheHandler.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ElementCacheHandler : IElementCacheHandler
1212
private readonly ElementState state;
1313
private readonly IElementFinder elementFinder;
1414

15-
private RemoteWebElement remoteElement;
15+
private WebElement element;
1616

1717
public ElementCacheHandler(By locator, string name, ElementState state, IElementFinder finder)
1818
{
@@ -22,17 +22,17 @@ public ElementCacheHandler(By locator, string name, ElementState state, IElement
2222
elementFinder = finder;
2323
}
2424

25-
public bool IsStale => remoteElement != null && IsRefreshNeeded();
25+
public bool IsStale => element != null && IsRefreshNeeded();
2626

2727
public bool IsRefreshNeeded(ElementState? customState = null)
2828
{
29-
if (remoteElement == null)
29+
if (element == null)
3030
{
3131
return true;
3232
}
3333
try
3434
{
35-
var isDisplayed = remoteElement.Displayed;
35+
var isDisplayed = element.Displayed;
3636
// refresh is needed only if the property is not match to expected element state
3737
return (customState ?? state) == ElementState.Displayed && !isDisplayed;
3838
}
@@ -43,15 +43,15 @@ public bool IsRefreshNeeded(ElementState? customState = null)
4343
}
4444
}
4545

46-
public RemoteWebElement GetElement(TimeSpan? timeout = null, ElementState? customState = null)
46+
public WebElement GetElement(TimeSpan? timeout = null, ElementState? customState = null)
4747
{
4848

4949
if (IsRefreshNeeded(customState))
5050
{
51-
remoteElement = (RemoteWebElement)elementFinder.FindElement(locator, customState ?? state, timeout, name);
51+
element = (WebElement)elementFinder.FindElement(locator, customState ?? state, timeout, name);
5252
}
5353

54-
return remoteElement;
54+
return element;
5555
}
5656
}
5757
}

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Elements/ElementFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using Aquality.Selenium.Core.Localization;
77
using Aquality.Selenium.Core.Waitings;
88
using OpenQA.Selenium;
9-
using OpenQA.Selenium.Support.PageObjects;
9+
using SeleniumExtras.PageObjects;
1010

1111
namespace Aquality.Selenium.Core.Elements
1212
{

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Elements/Interfaces/IElement.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Aquality.Selenium.Core.Visualization;
22
using OpenQA.Selenium;
3-
using OpenQA.Selenium.Remote;
43
using System;
54

65
namespace Aquality.Selenium.Core.Elements.Interfaces
@@ -37,9 +36,9 @@ public interface IElement : IParent
3736
/// Finds current element by specified <see cref="Locator"/>
3837
/// </summary>
3938
/// <param name="timeout">Timeout to find element. Default: <see cref="Configurations.ITimeoutConfiguration.Condition"/></param>
40-
/// <returns>Instance of <see cref="RemoteWebElement"/> if found.</returns>
39+
/// <returns>Instance of <see cref="WebElement"/> if found.</returns>
4140
/// <exception cref="NoSuchElementException">Thrown when no elements found.</exception>
42-
RemoteWebElement GetElement(TimeSpan? timeout = null);
41+
WebElement GetElement(TimeSpan? timeout = null);
4342

4443
/// <summary>
4544
/// Gets element text.

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Elements/Interfaces/IElementCacheHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using OpenQA.Selenium.Remote;
1+
using OpenQA.Selenium;
22
using System;
33

44
namespace Aquality.Selenium.Core.Elements.Interfaces
@@ -26,6 +26,6 @@ public interface IElementCacheHandler
2626
/// <param name="timeout">Timeout used to retrive the element when <see cref="IsRefreshNeeded(ElementState?)"/> is true.</param>
2727
/// <param name="customState">Custom element's existance state used for search.</param>
2828
/// <returns>Cached element.</returns>
29-
RemoteWebElement GetElement(TimeSpan? timeout = null, ElementState? customState = null);
29+
WebElement GetElement(TimeSpan? timeout = null, ElementState? customState = null);
3030
}
31-
}
31+
}

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Visualization/DumpManager.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public virtual float Compare(string dumpName = null)
4444
{
4545
throw new InvalidOperationException($"Dump directory [{directory.FullName}] does not contain any [*{ImageFormat}] files.");
4646
}
47-
var existingElements = ElementsForVisualization.Where(element => element.Value.State.IsExist)
48-
.ToDictionary(el => el.Key, el => el.Value);
47+
var existingElements = FilterElementsForVisualization().ToDictionary(el => el.Key, el => el.Value);
4948
var countOfUnproceededElements = existingElements.Count;
5049
var countOfProceededElements = 0;
5150
var comparisonResult = 0f;
@@ -89,7 +88,7 @@ public virtual void Save(string dumpName = null)
8988
{
9089
var directory = CleanUpAndGetDumpDirectory(dumpName);
9190
LocalizedLogger.Info("loc.form.dump.save", directory.Name);
92-
ElementsForVisualization.Where(element => element.Value.State.IsExist).ToList()
91+
FilterElementsForVisualization()
9392
.ForEach(element =>
9493
{
9594
try
@@ -103,6 +102,11 @@ public virtual void Save(string dumpName = null)
103102
});
104103
}
105104

105+
protected virtual List<KeyValuePair<string, T>> FilterElementsForVisualization()
106+
{
107+
return ElementsForVisualization.Where(element => element.Value.State.IsDisplayed).ToList();
108+
}
109+
106110
protected virtual DirectoryInfo CleanUpAndGetDumpDirectory(string dumpName = null)
107111
{
108112
var dirInfo = GetDumpDirectory(dumpName);

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Visualization/VisualStateProvider.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Aquality.Selenium.Core.Logging;
22
using Aquality.Selenium.Core.Utilities;
3-
using OpenQA.Selenium.Remote;
3+
using OpenQA.Selenium;
44
using System;
55
using System.Drawing;
66
using System.Globalization;
@@ -11,10 +11,10 @@ public class VisualStateProvider : IVisualStateProvider
1111
{
1212
private readonly IImageComparator imageComparator;
1313
private readonly IElementActionRetrier actionRetrier;
14-
private readonly Func<RemoteWebElement> getElement;
14+
private readonly Func<WebElement> getElement;
1515
private readonly LogVisualState logVisualState;
1616

17-
public VisualStateProvider(IImageComparator imageComparator, IElementActionRetrier actionRetrier, Func<RemoteWebElement> getElement, LogVisualState logVisualState)
17+
public VisualStateProvider(IImageComparator imageComparator, IElementActionRetrier actionRetrier, Func<WebElement> getElement, LogVisualState logVisualState)
1818
{
1919
this.imageComparator = imageComparator;
2020
this.actionRetrier = actionRetrier;
@@ -27,9 +27,9 @@ public VisualStateProvider(IImageComparator imageComparator, IElementActionRetri
2727
public Point Location => GetLoggedValue(nameof(Location), element => element.Location);
2828

2929
public Image Image
30-
=> GetLoggedValue(nameof(Image), element => element.GetScreenshot().AsImage(), image => image.Size.ToString());
30+
=> GetLoggedValue(nameof(Image), element => element.GetScreenshot().AsImage(), image => image?.Size.ToString());
3131

32-
private T GetLoggedValue<T>(string name, Func<RemoteWebElement, T> getValue, Func<T, string> toString = null)
32+
private T GetLoggedValue<T>(string name, Func<WebElement, T> getValue, Func<T, string> toString = null)
3333
{
3434
logVisualState($"loc.el.visual.get{name.ToLower()}");
3535
var value = actionRetrier.DoWithRetry(() => getValue(getElement()));
@@ -40,6 +40,8 @@ private T GetLoggedValue<T>(string name, Func<RemoteWebElement, T> getValue, Fun
4040
public float GetDifference(Image theOtherOne, float? threshold = null)
4141
{
4242
var currentImage = Image;
43+
float value = 1;
44+
4345
if (threshold == null)
4446
{
4547
logVisualState("loc.el.visual.getdifference", theOtherOne.Size.ToString());
@@ -48,7 +50,11 @@ public float GetDifference(Image theOtherOne, float? threshold = null)
4850
{
4951
logVisualState("loc.el.visual.getdifference.withthreshold", theOtherOne.Size.ToString(), threshold?.ToString("P", CultureInfo.InvariantCulture));
5052
}
51-
var value = imageComparator.PercentageDifference(currentImage, theOtherOne, threshold);
53+
54+
if (currentImage != default)
55+
{
56+
value = imageComparator.PercentageDifference(currentImage, theOtherOne, threshold);
57+
}
5258
logVisualState("loc.el.visual.difference.value", value.ToString("P", CultureInfo.InvariantCulture));
5359
return value;
5460
}

Aquality.Selenium.Core/tests/Aquality.Selenium.Core.Tests/Applications/Browser/ChromeApplication.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Aquality.Selenium.Core.Applications;
22
using Aquality.Selenium.Core.Configurations;
3+
using OpenQA.Selenium;
34
using OpenQA.Selenium.Chrome;
4-
using OpenQA.Selenium.Remote;
55
using System;
66

77
namespace Aquality.Selenium.Core.Tests.Applications.Browser
@@ -17,7 +17,7 @@ public ChromeApplication(ITimeoutConfiguration timeoutConfiguration)
1717
Driver.Manage().Timeouts().ImplicitWait = implicitWait;
1818
}
1919

20-
public RemoteWebDriver Driver { get; }
20+
public WebDriver Driver { get; }
2121

2222
public bool IsStarted => Driver.SessionId != null;
2323

0 commit comments

Comments
 (0)