Skip to content

Commit 0d2ee6c

Browse files
authored
Updated FindElements (#33)
* added ElementsCount any, added tests for FindElements * updated docs * fix of typo * stabilize a test * added debug info * try to stabilize tests * add debug info * added debug info * added logs * added click to example to hover all info on page * added example.Click to hide all elements on form * removed debug info
1 parent 9880b0f commit 0d2ee6c

File tree

10 files changed

+151
-9
lines changed

10 files changed

+151
-9
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public T FindChildElement<T>(IElement parentElement, By childLocator, ElementSup
4343
return elementSupplier(new ByChained(parentElement.Locator, childLocator), $"Child element of {parentElement.Name}", state);
4444
}
4545

46-
public IList<T> FindElements<T>(By locator, ElementSupplier<T> supplier = null, ElementsCount expectedCount = ElementsCount.MoreThenZero, ElementState state = ElementState.Displayed) where T : IElement
46+
public IList<T> FindElements<T>(By locator, ElementSupplier<T> supplier = null, ElementsCount expectedCount = ElementsCount.Any, ElementState state = ElementState.Displayed) where T : IElement
4747
{
4848
var elementSupplier = ResolveSupplier(supplier);
4949
switch (expectedCount)
@@ -54,9 +54,14 @@ public IList<T> FindElements<T>(By locator, ElementSupplier<T> supplier = null,
5454
message: LocalizationManager.GetLocalizedMessage("loc.elements.found.but.should.not", locator.ToString(), state.ToString()));
5555
break;
5656
case ElementsCount.MoreThenZero:
57-
ConditionalWait.WaitFor(driver => driver.FindElements(locator).Any(),
57+
ConditionalWait.WaitFor(driver => driver.FindElements(locator).Any(
58+
webElement => state == ElementState.ExistsInAnyState || webElement.Displayed),
5859
message: LocalizationManager.GetLocalizedMessage("loc.no.elements.found.by.locator", locator.ToString()));
5960
break;
61+
case ElementsCount.Any:
62+
ConditionalWait.WaitFor(driver => driver.FindElements(locator),
63+
message: LocalizationManager.GetLocalizedMessage("loc.search.of.elements.failed", locator.ToString()));
64+
break;
6065
default:
6166
throw new ArgumentOutOfRangeException($"No such expected value: {expectedCount}");
6267
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
public enum ElementsCount
77
{
88
Zero,
9-
MoreThenZero
9+
MoreThenZero,
10+
Any
1011
}
1112
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public interface IElementFactory
3737
/// <typeparam name="T">Type of elements that have to implement IElement</typeparam>
3838
/// <param name="locator">Base elements locator</param>
3939
/// <param name="supplier">Delegate that defines constructor of element in case of custom elements</param>
40-
/// <param name="expectedCount">Expected number of elements that have to be found (zero ot more then zero)</param>
40+
/// <param name="expectedCount">Expected number of elements that have to be found (zero, more then zero, any)</param>
4141
/// <param name="state">Elements state</param>
4242
/// <exception cref="System.InvalidOperationException">Thrown when the supplier is null, and no constructor with required arguments was found.</exception>
4343
/// <returns>List of elements that found by locator</returns>
44-
IList<T> FindElements<T>(By locator, ElementSupplier<T> supplier = null, ElementsCount expectedCount = ElementsCount.MoreThenZero, ElementState state = ElementState.Displayed) where T : IElement;
44+
IList<T> FindElements<T>(By locator, ElementSupplier<T> supplier = null, ElementsCount expectedCount = ElementsCount.Any, ElementState state = ElementState.Displayed) where T : IElement;
4545
}
4646
}

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Resources/Localization/be.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"loc.no.elements.found.in.state": "Не знайшлі элементаў па лакатару '{0}' у {1} стане",
77
"loc.no.elements.found.by.locator": "Не знайшлі элементаў па лакатару '{0}'",
88
"loc.elements.were.found.but.not.in.state": "Знайшлі элементы па лакатару '{0}', але яны не ў жаданым стане {1}",
9-
"loc.elements.found.but.should.not": "Не павінна быць знойдзена элементаў па лакатару '{0}' у {1} стане"
9+
"loc.elements.found.but.should.not": "Не павінна быць знойдзена элементаў па лакатару '{0}' у {1} стане",
10+
"loc.search.of.elements.failed": "Пошук элемента па лакатару '{0}' прайшоў няўдала"
1011
}

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Resources/Localization/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"loc.no.elements.found.in.state": "No elements with locator '{0}' were found in {1} state",
77
"loc.no.elements.found.by.locator": "No elements were found by locator '{0}'",
88
"loc.elements.were.found.but.not.in.state": "Elements were found by locator '{0}' but not in desired state {1}",
9-
"loc.elements.found.but.should.not": "No elements should be found by locator '{0}' in {1} state"
9+
"loc.elements.found.but.should.not": "No elements should be found by locator '{0}' in {1} state",
10+
"loc.search.of.elements.failed": "Search of element by locator '{0}' failed"
1011
}

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Resources/Localization/ru.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"loc.no.elements.found.in.state": "Не удалось найти элементов по локатору '{0}' в {1} состоянии",
77
"loc.no.elements.found.by.locator": "Не удалось найти элементов по локатору '{0}'",
88
"loc.elements.were.found.but.not.in.state": "Удалось найти элементы по локатору '{0}', но они не в желаемом состоянии {1}",
9-
"loc.elements.found.but.should.not": "Не должно быть найдено элементов по локатору '{0}' в {1} состоянии"
9+
"loc.elements.found.but.should.not": "Не должно быть найдено элементов по локатору '{0}' в {1} состоянии",
10+
"loc.search.of.elements.failed": "Поиск элемента по локатору '{0}' прошел неудачно"
1011
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ public class ApplicationManager : ApplicationManager<ChromeApplication>
1313
private static readonly object downloadDriverLock = new object();
1414

1515
public static ChromeApplication Application => GetApplication(services => StartChrome(services));
16-
16+
17+
public static IServiceProvider ServiceProvider => GetServiceProvider(services => Application);
18+
1719
private static ChromeApplication StartChrome(IServiceProvider services)
1820
{
1921
lock (downloadDriverLock)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Aquality.Selenium.Core.Elements;
2+
using Aquality.Selenium.Core.Tests.Applications.Browser;
3+
using OpenQA.Selenium;
4+
5+
namespace Aquality.Selenium.Core.Tests.Applications.WindowsApp.Elements
6+
{
7+
public class Label : WebElement
8+
{
9+
public Label(By locator, string name, ElementState state) : base(locator, name, state)
10+
{
11+
}
12+
13+
protected override string ElementType { get; } = "Label";
14+
}
15+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Aquality.Selenium.Core.Applications;
2+
using Aquality.Selenium.Core.Elements;
3+
using Aquality.Selenium.Core.Elements.Interfaces;
4+
using Aquality.Selenium.Core.Localization;
5+
using Aquality.Selenium.Core.Utilities;
6+
using Aquality.Selenium.Core.Waitings;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using OpenQA.Selenium;
9+
10+
namespace Aquality.Selenium.Core.Tests.Applications.Browser
11+
{
12+
public abstract class WebElement : Element
13+
{
14+
protected WebElement(By locator, string name, ElementState state) : base(locator, name, state)
15+
{
16+
}
17+
18+
protected override ElementActionRetrier ActionRetrier => ApplicationManager.ServiceProvider.GetRequiredService<ElementActionRetrier>();
19+
20+
protected override IApplication Application => ApplicationManager.Application;
21+
22+
protected override ConditionalWait ConditionalWait => ApplicationManager.ServiceProvider.GetRequiredService<ConditionalWait>();
23+
24+
protected override IElementFactory Factory => ApplicationManager.ServiceProvider.GetRequiredService<IElementFactory>();
25+
26+
protected override IElementFinder Finder => ApplicationManager.ServiceProvider.GetRequiredService<IElementFinder>();
27+
28+
protected override ILocalizedLogger LocalizedLogger => ApplicationManager.ServiceProvider.GetRequiredService<ILocalizedLogger>();
29+
}
30+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using NUnit.Framework;
2+
using System;
3+
using Aquality.Selenium.Core.Elements;
4+
using Aquality.Selenium.Core.Elements.Interfaces;
5+
using Aquality.Selenium.Core.Tests.Applications.WindowsApp.Elements;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using OpenQA.Selenium;
8+
9+
namespace Aquality.Selenium.Core.Tests.Applications.Browser
10+
{
11+
public class FindElementsTests : TestWithBrowser
12+
{
13+
private static readonly By HiddenElementsLoc = By.XPath("//h5");
14+
private static readonly By DisplayedElementsLoc = By.XPath("//img[@alt='User Avatar']");
15+
private static readonly By NotExistElementLoc = By.XPath("//div[@class='testtest']");
16+
private static readonly By ContentLoc = By.XPath("//div[contains(@class,'example')]");
17+
private static readonly Uri HoversURL = new Uri("http://the-internet.herokuapp.com/hovers");
18+
private IElementFactory elementFactory;
19+
20+
[SetUp]
21+
public new void SetUp()
22+
{
23+
elementFactory = ServiceProvider.GetRequiredService<IElementFactory>();
24+
ApplicationManager.Application.Driver.Navigate().GoToUrl(HoversURL);
25+
var example = new Label(ContentLoc, "Example", ElementState.Displayed);
26+
example.Click();
27+
}
28+
29+
[TestCase(ElementsCount.MoreThenZero, ElementState.Displayed, 3)]
30+
[TestCase(ElementsCount.MoreThenZero, ElementState.ExistsInAnyState, 3)]
31+
[TestCase(ElementsCount.Any, ElementState.Displayed, 3)]
32+
[TestCase(ElementsCount.Any, ElementState.ExistsInAnyState, 3)]
33+
public void Should_BePossibleTo_FindElements_ForDisplayedElements(ElementsCount count, ElementState state, int expectedCount)
34+
{
35+
var elementsCount = elementFactory.FindElements<Label>(DisplayedElementsLoc, expectedCount: count, state: state).Count;
36+
Assert.AreEqual(expectedCount, elementsCount, $"Elements count for displayed elements should be {expectedCount}");
37+
}
38+
39+
[TestCase(ElementsCount.Zero, ElementState.Displayed, 0)]
40+
[TestCase(ElementsCount.MoreThenZero, ElementState.ExistsInAnyState, 3)]
41+
[TestCase(ElementsCount.Any, ElementState.Displayed, 0)]
42+
[TestCase(ElementsCount.Any, ElementState.ExistsInAnyState, 3)]
43+
public void Should_BePossibleTo_FindElements_ForHiddenElements(ElementsCount count, ElementState state, int expectedCount)
44+
{
45+
var elementsCount = elementFactory.FindElements<Label>(HiddenElementsLoc, expectedCount: count, state: state).Count;
46+
Assert.AreEqual(expectedCount, elementsCount, $"Elements count for hidden elements should be {expectedCount}");
47+
}
48+
49+
[TestCase(ElementsCount.Zero, ElementState.Displayed, 0)]
50+
[TestCase(ElementsCount.Zero, ElementState.ExistsInAnyState, 0)]
51+
[TestCase(ElementsCount.Any, ElementState.Displayed, 0)]
52+
[TestCase(ElementsCount.Any, ElementState.ExistsInAnyState, 0)]
53+
public void Should_BePossibleTo_FindElements_ForNotExistsElements(ElementsCount count, ElementState state, int expectedCount)
54+
{
55+
var elementsCount = elementFactory.FindElements<Label>(NotExistElementLoc, expectedCount: count, state: state).Count;
56+
Assert.AreEqual(expectedCount, elementsCount, $"Elements count for not existing elements should be {expectedCount}");
57+
}
58+
59+
[TestCase(ElementsCount.Zero, ElementState.Displayed)]
60+
[TestCase(ElementsCount.Zero, ElementState.ExistsInAnyState)]
61+
public void Should_BeImpossibleTo_FindDisplayedElements_WithWrongArguments(ElementsCount count, ElementState state)
62+
{
63+
Assert.Throws<WebDriverTimeoutException>(
64+
() => elementFactory.FindElements<Label>(DisplayedElementsLoc, expectedCount: count, state: state),
65+
$"Tried to find elements with expected count '{count}' and state '{state}'");
66+
}
67+
68+
[TestCase(ElementsCount.MoreThenZero, ElementState.Displayed)]
69+
[TestCase(ElementsCount.Zero, ElementState.ExistsInAnyState)]
70+
public void Should_BeImpossibleTo_FindHiddenElements_WithWrongArguments(ElementsCount count, ElementState state)
71+
{
72+
Assert.Throws<WebDriverTimeoutException>(
73+
() => elementFactory.FindElements<Label>(HiddenElementsLoc, expectedCount: count, state: state),
74+
$"Tried to find elements with expected count '{count}' and state '{state}'");
75+
}
76+
77+
[TestCase(ElementsCount.MoreThenZero, ElementState.Displayed)]
78+
[TestCase(ElementsCount.MoreThenZero, ElementState.ExistsInAnyState)]
79+
public void Should_BeImpossibleTo_FindNotExistElements_WithWrongArguments(ElementsCount count, ElementState state)
80+
{
81+
Assert.Throws<WebDriverTimeoutException>(
82+
() => elementFactory.FindElements<Label>(NotExistElementLoc, expectedCount: count, state: state),
83+
$"Tried to find elements with expected count '{count}' and state '{state}'");
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)