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

Will follow up...
This commit is contained in:
Pavel Feldman 2021-05-15 11:37:21 -07:00 committed by GitHub
parent bbc3ebd512
commit 9aa610063c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 91 additions and 19 deletions

View File

@ -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

View File

@ -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());
```

View File

@ -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

View File

@ -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

View File

@ -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`]

View File

@ -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.
:::