TestPage Class
A page of a browser engine written in .NET (Meziantou.Framework.WebPlatform), driven by a test: the user input, the queries of the elements, the waits, and the snapshots of the DOM, of the accessibility tree and of the pixels. A BrowserPage is a page of an application (see WebAppBrowser).
public abstract class TestPage : LocatorScope, IAsyncDisposable, IDisposable- Inheritance
LocatorScopeTestPage- Implements
Constructors
| Name | Description |
|---|---|
| TestPage(TestPageSettings) | Initializes a new instance of the TestPage class. |
TestPage(TestPageSettings)
TestPage.csInitializes a new instance of the TestPage class.
protected TestPage(TestPageSettings settings)Parameters
settingsTestPageSettings- The settings of the browsing context of the page.
Properties
| Name | Description |
|---|---|
| BrowsingContext | Gets the browsing context of the page (viewport, user input, event loop, history...). |
| ConsoleMessages | Gets the messages written by the scripts of the page to the console. |
| ContentSecurityPolicyViolations | Gets the violations of the content security policies of the page (the Content-Security-Policy and Content-Security-Policy-Report-Only headers and the meta elements), as reported by the securitypolicyviolation events, including the ones raised while the document loads. |
| DefaultTimeout | Gets the maximum time the waits of the page wait for. |
| Document | Gets the active document of the page. |
| Input | Gets the user input: the mouse, the keyboard and the touch screen. |
| IsDisposed | Gets whether the page is disposed. |
| ScriptErrors | Gets the errors of the scripts of the page that were not handled. |
| TestIdAttribute | Gets the name of the attribute that LocatorScope.GetByTestId matches. |
| Url | Gets the URL of the active document. |
BrowsingContext
TestPage.csGets the browsing context of the page (viewport, user input, event loop, history...).
public BrowsingContext BrowsingContext { get; }Property value
ConsoleMessages
TestPage.csGets the messages written by the scripts of the page to the console.
public IReadOnlyList<string> ConsoleMessages { get; }Property value
ContentSecurityPolicyViolations
TestPage.csGets the violations of the content security policies of the page (the Content-Security-Policy and Content-Security-Policy-Report-Only headers and the meta elements), as reported by the securitypolicyviolation events, including the ones raised while the document loads.
public IReadOnlyList<ContentSecurityPolicyViolationReport> ContentSecurityPolicyViolations { get; }Property value
DefaultTimeout
TestPage.csDocument
TestPage.csInput
TestPage.csIsDisposed
TestPage.csScriptErrors
TestPage.csGets the errors of the scripts of the page that were not handled.
public IReadOnlyList<ScriptError> ScriptErrors { get; }Property value
TestIdAttribute
TestPage.csGets the name of the attribute that LocatorScope.GetByTestId matches.
public string TestIdAttribute { get; }Property value
Url
TestPage.csMethods
| Name | Description |
|---|---|
| Dispose() | Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| Dispose(bool) | Releases the resources of the page. |
| DisposeAsync() | Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously. |
| DisposeAsyncCore() | Releases the resources of the page asynchronously. Calls TestPage.Dispose by default. |
| EnterEventLoop() | Runs the code that follows on the event loop of the page until the result is disposed: a derived page runs the continuations of its own asynchronous code on the event loop (for instance the components of Blazor). Returns null by default. |
| Find(string) | Gets the first element of the page that matches a CSS selector, now (see LocatorScope.Locator for a locator that waits). |
| Find<TElement>(string) | Gets the first element of the page of a type that matches a CSS selector, now. |
| FindAll(string) | Gets the elements of the page that match a CSS selector now, in document order. |
| FindOrDefault(string) | Gets the first element of the page that matches a CSS selector now, or null. |
| GetAccessibilitySnapshot() | Gets the accessibility tree of the page, as a YAML snapshot (the format of the ARIA snapshots of Playwright). |
| GetMarkup(MarkupOptions?) | Gets the HTML of the body of the page, formatted for snapshots. |
| GetPage(Node) | Gets the page of a node of one of its documents. |
| GetPendingTasks() | Returns the tasks whose completion can make the page idle (a wait wakes up when one completes). |
| GetRoots() | Gets the nodes that the queries of the page search: the document element by default. |
| IsIdle() | Returns whether the page is idle: the event loop has no task and no pending network request. A derived page adds its own pending work. |
| RunEventLoop() | Runs the tasks of the event loop of the page until there is none. |
| TakeScreenshotAsPng(ScreenshotOptions?) | Takes a screenshot of the viewport, encoded as PNG. |
| ThrowIfFailed() | Throws the errors that the page collected while the event loop ran (for instance the unhandled exceptions of the components). |
| WaitForAssertionAsync(Action, TimeSpan?, CancellationToken) | Runs the event loop of the page until an assertion passes. |
| WaitForAsync(Func<bool>, TimeSpan?, CancellationToken) | Runs the event loop of the page until a condition is true. |
| WaitForIdleAsync(TimeSpan?, CancellationToken) | Runs the event loop of the page until it is idle: no task, no pending network request (see TestPage.IsIdle). |
Dispose overloads
Dispose()
TestPage.csPerforms application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()Dispose(bool)
TestPage.csReleases the resources of the page.
Parameters
disposingbool- True when called from
TestPage.Dispose.
DisposeAsync()
TestPage.csDisposeAsyncCore()
TestPage.csReleases the resources of the page asynchronously. Calls TestPage.Dispose by default.
protected virtual ValueTask DisposeAsyncCore()Returns
ValueTask A task completed once the resources are released.
EnterEventLoop()
TestPage.csRuns the code that follows on the event loop of the page until the result is disposed: a derived page runs the continuations of its own asynchronous code on the event loop (for instance the components of Blazor). Returns null by default.
protected internal virtual IDisposable? EnterEventLoop()Returns
IDisposable? An object that leaves the event loop when disposed, or null.
Find overloads
Find(string)
TestPage.csGets the first element of the page that matches a CSS selector, now (see LocatorScope.Locator for a locator that waits).
Parameters
selectorstring- The CSS selector.
Returns
Element The element.
Exceptions
ElementNotFoundException- No element matches the selector.
Find<TElement>(string)
TestPage.csGets the first element of the page of a type that matches a CSS selector, now.
Type parameters
TElement- The type of the element.
Parameters
selectorstring- The CSS selector.
Returns
TElement The element.
Exceptions
ElementNotFoundException- No element of type
TElementmatches the selector.
FindAll(string)
TestPage.csGets the elements of the page that match a CSS selector now, in document order.
public IReadOnlyList<Element> FindAll(string selector)FindOrDefault(string)
TestPage.csGetAccessibilitySnapshot()
TestPage.csGetMarkup(MarkupOptions?)
TestPage.csGets the HTML of the body of the page, formatted for snapshots.
public string GetMarkup(MarkupOptions? options = null)Parameters
optionsMarkupOptions?- The options, or null for the default options.
Returns
string The HTML.
GetPage(Node)
TestPage.csGetPendingTasks()
TestPage.csReturns the tasks whose completion can make the page idle (a wait wakes up when one completes).
protected virtual IReadOnlyCollection<Task> GetPendingTasks()Returns
IReadOnlyCollection<Task> The pending tasks.
GetRoots()
TestPage.csGets the nodes that the queries of the page search: the document element by default.
protected virtual IReadOnlyList<Node> GetRoots()Returns
IReadOnlyList<Node> The root nodes.
IsIdle()
TestPage.csRunEventLoop()
TestPage.csRuns the tasks of the event loop of the page until there is none.
protected internal void RunEventLoop()TakeScreenshotAsPng(ScreenshotOptions?)
TestPage.csTakes a screenshot of the viewport, encoded as PNG.
public byte[] TakeScreenshotAsPng(ScreenshotOptions? options = null)Parameters
optionsScreenshotOptions?- The options, or null for the default options.
Returns
byte[] The PNG image.
ThrowIfFailed()
TestPage.csThrows the errors that the page collected while the event loop ran (for instance the unhandled exceptions of the components).
protected virtual void ThrowIfFailed()WaitForAssertionAsync(Action, TimeSpan?, CancellationToken)
TestPage.csRuns the event loop of the page until an assertion passes.
net10.0
public Task WaitForAssertionAsync(Action assertion, TimeSpan? timeout = null, CancellationToken cancellationToken = default)net11.0
public Task WaitForAssertionAsync(Action assertion, TimeSpan? timeout = null, CancellationToken cancellationToken = default)Parameters
assertionAction- The assertion, which throws when it fails.
timeoutTimeSpan?- The maximum time to wait, or null for
TestPage.DefaultTimeout. cancellationTokenCancellationToken- A token to cancel the wait.
Returns
Task A task completed once the assertion passes.
Exceptions
TimeoutException- The assertion fails after
timeout; the inner exception is the last failure.
WaitForAsync(Func<bool>, TimeSpan?, CancellationToken)
TestPage.csRuns the event loop of the page until a condition is true.
public Task WaitForAsync(Func<bool> condition, TimeSpan? timeout = null, CancellationToken cancellationToken = default)Parameters
conditionFunc<bool>- The condition, evaluated after each task of the event loop.
timeoutTimeSpan?- The maximum time to wait, or null for
TestPage.DefaultTimeout. cancellationTokenCancellationToken- A token to cancel the wait.
Returns
Task A task completed once the condition is true.
Exceptions
TimeoutException- The condition is false after
timeout.
WaitForIdleAsync(TimeSpan?, CancellationToken)
TestPage.csRuns the event loop of the page until it is idle: no task, no pending network request (see TestPage.IsIdle).
public Task WaitForIdleAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)Parameters
timeoutTimeSpan?- The maximum time to wait, or null for
TestPage.DefaultTimeout. cancellationTokenCancellationToken- A token to cancel the wait.
Returns
Task A task completed once the page is idle.
Exceptions
TimeoutException- The page is not idle after
timeout.