Skip to content

Commit 1bcafa5

Browse files
committed
cherry-pick(#44003): docs(dotnet): document PlaywrightAssertionException and PlaywrightException
1 parent 2076636 commit 1bcafa5

6 files changed

Lines changed: 60 additions & 13 deletions
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# class: PlaywrightAssertionException
2+
* since: v1.66
3+
* langs: csharp
4+
* extends: [PlaywrightException]
5+
6+
PlaywrightAssertionException is thrown when an [assertion](../test-assertions.md) is not met, e.g. [`method: LocatorAssertions.toBeVisible`] times out
7+
waiting for the element. It inherits from
8+
[PlaywrightException], so `catch (PlaywrightException)` blocks catch it as well.
9+
10+
```csharp
11+
try
12+
{
13+
await Expect(page.GetByRole(AriaRole.Button)).ToBeVisibleAsync();
14+
}
15+
catch (PlaywrightAssertionException e)
16+
{
17+
Console.WriteLine($"Assertion failed: {e.Message}");
18+
}
19+
```
Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
# class: PlaywrightException
22
* since: v1.10
3-
* langs: java
3+
* langs: java, csharp
44
* extends: [RuntimeException]
55

66
PlaywrightException is thrown whenever certain operations are terminated abnormally, e.g.
7-
browser closes while [`method: Page.evaluate`] is running. All Playwright exceptions
8-
inherit from this class.
7+
browser closes while [`method: Page.evaluate`] is running.
8+
9+
```java
10+
try {
11+
page.evaluate("() => new Promise(() => {})");
12+
} catch (PlaywrightException e) {
13+
// When the page closes during the call, the exception message says so.
14+
System.out.println(e.getMessage());
15+
}
16+
```
17+
18+
```csharp
19+
try
20+
{
21+
await page.EvaluateAsync("() => new Promise(() => {})");
22+
}
23+
catch (PlaywrightException e)
24+
{
25+
// When the page closes during the call, the exception message says so.
26+
Console.WriteLine(e.Message);
27+
}
28+
```

‎docs/src/test-assertions-csharp-java-python.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ tests/test_foobar.py:22: AssertionError
109109
```
110110

111111
```bash lang=csharp
112-
Microsoft.Playwright.PlaywrightException : should be logged in
112+
Microsoft.Playwright.PlaywrightAssertionException : should be logged in
113113

114114
Locator expected to be visible
115115
Call log:

‎docs/src/test-runners-csharp.md‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: "Test Runners"
55

66
## Introduction
77

8-
While Playwright for .NET isn't tied to a particular test runner or testing framework, in our experience the easiest way of getting started is by using the base classes we provide for MSTest, NUnit, xUnit, or xUnit v3. These classes support running tests on multiple browser engines, adjusting launch/context options and getting a [Page]/[BrowserContext] instance per test out of the box.
8+
While Playwright for .NET isn't tied to a particular test runner or testing framework, in our experience the easiest way of getting started is by using the base classes we provide for MSTest, NUnit, xUnit, or xUnit v3. These classes support running tests on multiple browser engines, adjusting launch/context options and getting a [Page]/[BrowserContext] instance per test out of the box.
99

1010
Playwright and Browser instances will be reused between tests for better performance. We
1111
recommend running each test case in a new BrowserContext, this way browser state will be
@@ -312,7 +312,7 @@ Playwright for .NET does not retry failed tests itself, use the retries of your
312312

313313
Add the [`Retry`](https://docs.nunit.org/articles/nunit/writing-tests/attributes/retry.html) attribute to a test method. Its argument is the total number of attempts, so `[Retry(3)]` retries a failed test up to two times.
314314

315-
By default NUnit only retries a test that failed an NUnit assertion. Playwright assertions throw `PlaywrightException` and actions that time out throw `TimeoutException`, so pass both as `RetryExceptions`. This property requires NUnit 4.5 or later.
315+
By default NUnit only retries a test that failed an NUnit assertion. Playwright assertions throw [PlaywrightAssertionException], other Playwright errors throw [PlaywrightException] and actions that time out throw `TimeoutException`, so pass them all as `RetryExceptions`. This property requires NUnit 4.5 or later.
316316

317317
```csharp
318318
using Microsoft.Playwright;
@@ -325,7 +325,7 @@ namespace PlaywrightTests;
325325
public class MyTest : PageTest
326326
{
327327
[Test]
328-
[Retry(3, RetryExceptions = [typeof(PlaywrightException), typeof(TimeoutException)])]
328+
[Retry(3, RetryExceptions = [typeof(PlaywrightException), typeof(PlaywrightAssertionException), typeof(TimeoutException)])]
329329
public async Task TestWithRetries()
330330
{
331331
await Page.GotoAsync("https://playwright.dev");
@@ -437,7 +437,7 @@ For example, to specify the number of workers you can use `NUnit.NumberOfTestWor
437437
```xml
438438
<?xml version="1.0" encoding="utf-8"?>
439439
<RunSettings>
440-
<!-- NUnit adapter -->
440+
<!-- NUnit adapter -->
441441
<NUnit>
442442
<NumberOfTestWorkers>24</NumberOfTestWorkers>
443443
</NUnit>
@@ -448,7 +448,7 @@ For example, to specify the number of workers you can use `NUnit.NumberOfTestWor
448448
<DEBUG>pw:api</DEBUG>
449449
</EnvironmentVariables>
450450
</RunConfiguration>
451-
<!-- Playwright -->
451+
<!-- Playwright -->
452452
<Playwright>
453453
<BrowserName>chromium</BrowserName>
454454
<ExpectTimeout>5000</ExpectTimeout>
@@ -467,7 +467,7 @@ For example, to specify the number of workers, you can use `MSTest.Parallelize.W
467467

468468
```xml
469469
<RunSettings>
470-
<!-- MSTest adapter -->
470+
<!-- MSTest adapter -->
471471
<MSTest>
472472
<Parallelize>
473473
<Workers>4</Workers>
@@ -481,7 +481,7 @@ For example, to specify the number of workers, you can use `MSTest.Parallelize.W
481481
<DEBUG>pw:api</DEBUG>
482482
</EnvironmentVariables>
483483
</RunConfiguration>
484-
<!-- Playwright -->
484+
<!-- Playwright -->
485485
<Playwright>
486486
<BrowserName>chromium</BrowserName>
487487
<ExpectTimeout>5000</ExpectTimeout>
@@ -512,7 +512,7 @@ For example, to specify the number of workers, you can use `xUnit.MaxParallelThr
512512
<DEBUG>pw:api</DEBUG>
513513
</EnvironmentVariables>
514514
</RunConfiguration>
515-
<!-- Playwright -->
515+
<!-- Playwright -->
516516
<Playwright>
517517
<BrowserName>chromium</BrowserName>
518518
<ExpectTimeout>5000</ExpectTimeout>
@@ -542,7 +542,7 @@ For example, to specify the number of workers, you can use `xUnit.MaxParallelThr
542542
<DEBUG>pw:api</DEBUG>
543543
</EnvironmentVariables>
544544
</RunConfiguration>
545-
<!-- Playwright -->
545+
<!-- Playwright -->
546546
<Playwright>
547547
<BrowserName>chromium</BrowserName>
548548
<ExpectTimeout>5000</ExpectTimeout>

‎utils/doclint/dotnetXmlDocumentation.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ function _wrapAndEscape(node, maxColumns = 0) {
130130
text = text.replace(/(?<!`)\[(.*?)\]/g, (match, link) => `<see cref="${link}"/>`);
131131
text = text.replace(/`([^`]*)`/g, (match, code) => `<c>${code.replace(/</g, '&lt;').replace(/>/g, '&gt;')}</c>`);
132132
text = text.replace(/ITimeoutError/, 'TimeoutException');
133+
text = text.replace(/IPlaywrightException/g, 'PlaywrightException');
134+
text = text.replace(/IPlaywrightAssertionException/g, 'PlaywrightAssertionException');
133135
text = text.replace(/Promise/, 'Task');
134136

135137
const words = text.split(' ');

‎utils/doclint/generateDotnetApi.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ const classNameMap = new Map(documentation.classesArray.map(x => [x.name, `I${to
7777
// map some types that we know of
7878
classNameMap.set('Error', 'Exception');
7979
classNameMap.set('TimeoutError', 'TimeoutException');
80+
classNameMap.set('PlaywrightException', 'PlaywrightException');
81+
classNameMap.set('PlaywrightAssertionException', 'PlaywrightAssertionException');
8082
classNameMap.set('EvaluationArgument', 'object');
8183
classNameMap.set('boolean', 'bool');
8284
classNameMap.set('any', 'object');
@@ -135,6 +137,10 @@ function renderClass(clazz) {
135137
const name = classNameMap.get(clazz.name);
136138
if (name === 'TimeoutException')
137139
return;
140+
if (name === 'PlaywrightException')
141+
return;
142+
if (name === 'PlaywrightAssertionException')
143+
return;
138144
if (name === 'IAsyncDisposable')
139145
return;
140146

0 commit comments

Comments
 (0)