Assertions
Assertions in .NET
The C# API in C:\CSTesting-DotNet (ICSTestingBrowser, ICSTestingLocator) does not include assertThat. Use NUnit (or your test framework) with Assert.That(actual, constraint), and pass values from the async browser API (await ...). Waits such as WaitForSelectorAsync / WaitForURLAsync run before you assert.
Common checks (element)
| Idea | Example |
|---|---|
| Visible | Assert.That(await Browser.IsVisibleAsync("h1"), Is.True); or await Browser.Locator("h1").IsVisibleAsync() |
| Text content | var t = await Browser.Locator("h1").GetTextContentAsync(); Assert.That(t, Does.Contain("Example")); |
| Attribute | var href = await Browser.Locator("a").First().GetAttributeAsync("href"); Assert.That(href, Is.Not.Empty); |
| Disabled / editable / selected | Assert.That(await Browser.IsDisabledAsync("#x"), Is.True); — same pattern for IsEditableAsync, IsSelectedAsync |
Common checks (page)
| Idea | Example |
|---|---|
| URL | var url = await Browser.GetUrlAsync(); Assert.That(url, Does.Contain("example.com")); or await Browser.WaitForURLAsync("**/welcome", 10_000); |
| Title | var title = await Browser.EvaluateAsync<string>("document.title"); Assert.That(title, Does.Contain("Example")); |
| HTML snippet | var html = await Browser.ContentAsync(); Assert.That(html, Does.Contain("body")); |
Matching many elements
ICSTestingLocator from Browser.Locator(selector) targets the first match (see CSTestingBrowser.Locator). To count matches, use JavaScript, for example:
var n = await Browser.EvaluateAsync<int>(
"() => document.querySelectorAll('li').length");
Assert.That(n, Is.EqualTo(5));
Usage
await Browser!.GotoAsync("https://example.com");
Assert.That(await Browser.Locator("h1").GetTextContentAsync(),
Does.Contain("Example"));
Assert.That(await Browser.GetUrlAsync(), Does.Contain("example.com"));
var title = await Browser.EvaluateAsync<string>("document.title");
Assert.That(title, Is.Not.Empty);
Failed NUnit assertions throw AssertionException (or reported failure), not CSTesting’s Java-style AssertionError.