Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

docs(dotnet): examples for verification, video, fixes (#6597)

Pavel Feldman9aa610063c24
diff --git a/docs/src/input.md b/docs/src/input.md
index 42ce14bf64..2f94f579a4 100644
--- a/docs/src/input.md
+++ b/docs/src/input.md
@@ -750,11 +750,12 @@ file_chooser.set_files("myfile.pdf")
```
```csharp
-var fileChooser = await Task.WhenAll(
- page.WaitForFileChooserAsync(),
+var waitForChooserTask = page.WaitForFileChooserAsync();
+await Task.WhenAll(
+ waitForChooserTask,
page.ClickAsync("upload")
);
-await fileChooser.SetFilesAsync("myfile.pdf");
+await waitForChooserTask.Result.SetFilesAsync("myfile.pdf");
```
### API reference
diff --git a/docs/src/multi-pages.md b/docs/src/multi-pages.md
index b10c9f396a..c45d1acaea 100644
--- a/docs/src/multi-pages.md
+++ b/docs/src/multi-pages.md
@@ -209,11 +209,12 @@ print(new_page.title())
```csharp
// Get page after a specific action (e.g. clicking a link)
-var newPage = await Task.WhenAll(
- context.WaitForPageAsync(),
+var waitForPageTask = context.WaitForPageAsync();
+await Task.WhenAll(
+ waitForPageTask,
page.ClickAsync("a[target='_blank']")
);
-await newPage.WaitForLoadStateAsync();
+await waitForPageTask.Result.WaitForLoadStateAsync();
Console.WriteLine(await newPage.TitleAsync());
```
diff --git a/docs/src/navigations.md b/docs/src/navigations.md
index f296d4c4a1..4428c645f9 100644
--- a/docs/src/navigations.md
+++ b/docs/src/navigations.md
@@ -434,11 +434,12 @@ popup.wait_for_load_state("load")
```
```csharp
-var popup = await Task.WhenAll(
- page.WaitForPopupAsync(),
+var waitForPopupTask = page.WaitForPopupAsync();
+await Task.WhenAll(
+ waitForPopupTask,
page.ClickAsync("a[target='_blank']") // Opens popup
);
-await popup.WaitForLoadStateAsync(LoadState.Load);
+await waitForPopupTask.Result.WaitForLoadStateAsync(LoadState.Load);
```
### API reference
diff --git a/docs/src/network.md b/docs/src/network.md
index c721b8d5eb..d15f721bb3 100644
--- a/docs/src/network.md
+++ b/docs/src/network.md
@@ -281,10 +281,13 @@ response = response_info.value
```csharp
// Use a glob URL pattern
-var response = await Task.WhenAll(
- page.WaitForResponseAsync("**/api/fetch_data"),
+var waitForResponseTask = page.WaitForResponseAsync(
+ "**/api/fetch_data");
+await Task.WhenAll(
+ waitForResponseTask,
page.ClickAsync("button#update")
);
+var response = waitForResponseTask.Result;
```
#### Variations
@@ -341,16 +344,22 @@ response = response_info.value
```csharp
// Use a regular expression
-var response = await Task.WhenAll(
- page.WaitForResponseAsync(new Regex("\\.jpeg$")),
+var waitForResponseTask = page.WaitForResponseAsync(
+ new Regex("\\.jpeg$"));
+await Task.WhenAll(
+ waitForResponseTask,
page.ClickAsync("button#update")
);
+var response = waitForResponseTask.Result;
// Use a predicate taking a Response object
-var response = await Task.WhenAll(
- page.WaitForResponseAsync(r => r.Url.Contains(token)),
+var waitForResponseTask = page.WaitForResponseAsync(
+ r => r.Url.Contains(token));
+await Task.WhenAll(
+ waitForResponseTask,
page.ClickAsync("button#update")
);
+var response = waitForResponseTask.Result;
```
### API reference
diff --git a/docs/src/verification.md b/docs/src/verification.md
index d3aadcc613..b1c2502935 100644
--- a/docs/src/verification.md
+++ b/docs/src/verification.md
@@ -90,6 +90,28 @@ msg.args[0].json_value() # hello
msg.args[1].json_value() # 42
```
+```csharp
+// Listen for all System.out.printlns
+page.Console += (_, msg) => Console.WriteLine(msg.Text);
+
+// Listen for all console events and handle errors
+page.Console += (_, msg) => {
+ if ("error".Equals(msg.Type))
+ Console.WriteLine("Error text: " + msg.Text);
+};
+
+// Get the next System.out.println
+var waitForMessageTask = page.WaitForConsoleMessageAsync();
+await Task.WhenAll(
+ waitForMessageTask,
+ page.EvaluateAsync("console.log('hello', 42, { foo: 'bar' });")
+);
+
+// Deconstruct console.log arguments
+await waitForMessageTask.Result.Args.ElementAt(0).JsonValueAsync<string>(); // hello
+await waitForMessageTask.Result.Args.ElementAt(1).JsonValueAsync<int>(); // 42
+```
+
### API reference
- [ConsoleMessage]
- [Page]
@@ -137,6 +159,13 @@ page.on("pageerror", lambda exc: print(f"uncaught exception: {exc}"))
page.goto("data:text/html,<script>throw new Error('test')</script>")
```
+```csharp
+// Log all uncaught errors to the terminal
+page.PageError += (_, exception) => {
+ Console.WriteLine("Uncaught exception: " + exception);
+};
+```
+
### API reference
- [Page]
- [`event: Page.pageError`]
@@ -181,6 +210,12 @@ page.onDialog(dialog -> {
page.on("dialog", lambda dialog: dialog.accept())
```
+```csharp
+page.RequestFailed += (_, request) => {
+ Console.WriteLine(request.Url + " " + request.Failure);
+};
+```
+
#### `"popup"` - handle popup windows
```js
@@ -208,6 +243,12 @@ with page.expect_popup() as popup_info:
popup = popup_info.value
```
+```csharp
+var waitForPopupTask = page.WaitForPopupAsync();
+await Task.WhenAll(waitForPopupTask, page.ClickAsync("#open"));
+var popup = waitForPopupTask.Result;
+```
+
### API reference
- [Page]
- [`event: Page.requestFailed`]
diff --git a/docs/src/videos.md b/docs/src/videos.md
index 18da337dad..72586060ff 100644
--- a/docs/src/videos.md
+++ b/docs/src/videos.md
@@ -32,13 +32,19 @@ context = browser.new_context(record_video_dir="videos/")
context.close()
```
+```csharp
+var context = await browser.NewContextAsync(recordVideoDir: "videos/");
+// Make sure to close, so that videos are saved.
+await context.CloseAsync();
+```
+
You can also specify video size, it defaults to viewport size scaled down to fit 800x800.
```js
const context = await browser.newContext({
recordVideo: {
dir: 'videos/',
- size: { width: 1024, height: 768 },
+ size: { width: 640, height: 480 },
}
});
```
@@ -46,23 +52,32 @@ const context = await browser.newContext({
```java
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.setRecordVideoDir(Paths.get("videos/"))
- .setRecordVideoSize(1024, 768));
+ .setRecordVideoSize(640, 480));
```
```python async
context = await browser.new_context(
record_video_dir="videos/",
- record_video_size={"width": 1024, "height": 768}
+ record_video_size={"width": 640, "height": 480}
)
```
```python sync
context = browser.new_context(
record_video_dir="videos/",
- record_video_size={"width": 1024, "height": 768}
+ record_video_size={"width": 640, "height": 480}
)
```
+```csharp
+var context = await browser.NewContextAsync(
+ recordVideoDir: "videos/",
+ recordVideoSize: new RecordVideoSize() { Width = 640, Height = 480 }
+);
+// Make sure to close, so that videos are saved.
+await context.CloseAsync();
+```
+
Saved video files will appear in the specified folder. They all have generated unique names.
For the multi-page scenarios, you can access the video file associated with the page via the
[`method: Page.video`].
@@ -84,6 +99,10 @@ path = await page.video.path()
path = page.video.path()
```
+```csharp
+var path = await page.Video.PathAsync();
+```
+
:::note
Note that the video is only available after the page or browser context is closed.
:::