-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathWaitForElementsHelper.cs
More file actions
39 lines (32 loc) · 1.54 KB
/
WaitForElementsHelper.cs
File metadata and controls
39 lines (32 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System.Globalization;
using System.Text;
using AngleSharp.Dom;
namespace Bunit.Extensions.WaitForHelpers;
/// <summary>
/// Represents an async wait helper, that will wait for a specified time for element(s) to become available in the DOM.
/// </summary>
internal class WaitForElementsHelper<TComponent> : WaitForHelper<IReadOnlyList<IElement>, TComponent>
where TComponent : IComponent
{
internal const string TimeoutBeforeFoundMessage = "The CSS selector did not result in any matching element(s) before the timeout period passed.";
internal static readonly CompositeFormat TimeoutBeforeFoundWithCountMessage = CompositeFormat.Parse("The CSS selector did not result in exactly {0} matching element(s) before the timeout period passed.");
private readonly int? matchElementCount;
/// <inheritdoc/>
protected override string? TimeoutErrorMessage => matchElementCount is null
? TimeoutBeforeFoundMessage
: string.Format(CultureInfo.InvariantCulture, TimeoutBeforeFoundWithCountMessage, matchElementCount);
/// <inheritdoc/>
protected override bool StopWaitingOnCheckException => true;
public WaitForElementsHelper(IRenderedComponent<TComponent> renderedComponent, string cssSelector, int? matchElementCount, TimeSpan? timeout = null)
: base(renderedComponent, () =>
{
var elements = renderedComponent.FindAll(cssSelector);
var checkPassed = matchElementCount is null
? elements.Count > 0
: elements.Count == matchElementCount;
return (checkPassed, elements);
}, timeout)
{
this.matchElementCount = matchElementCount;
}
}