docs(dotnet): examples for dialog, download and filechooser (#6526)

This commit is contained in:
Anže Vodovnik 2021-05-13 19:24:15 +02:00 committed by GitHub
parent 8b6b894dd8
commit d6b98effc3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 1 deletions

View file

@ -80,6 +80,29 @@ with sync_playwright() as playwright:
run(playwright)
```
```csharp
using Microsoft.Playwright;
using System.Threading.Tasks;
class DialogExample
{
public static async Task Run()
{
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();
page.Dialog += async (_, dialog) =>
{
System.Console.WriteLine(dialog.Message);
await dialog.DismissAsync();
};
await page.EvaluateAsync("alert('1');");
}
}
```
:::note
Dialogs are dismissed automatically, unless there is a [`event: Page.dialog`] listener.
When listener is present, it **must** either [`method: Dialog.accept`] or [`method: Dialog.dismiss`] the dialog - otherwise the page will [freeze](https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#never_blocking) waiting for the dialog, and actions like click will never finish.

View file

@ -48,6 +48,12 @@ download = download_info.value
path = download.path()
```
```csharp
var downloadTask = page.WaitForDownloadAsync();
await Task.WhenAll(downloadTask, page.ClickAsync("#downloadButton"));
Console.WriteLine(await downloadTask.Result.PathAsync());
```
:::note
Browser context **must** be created with the [`option: acceptDownloads`] set to `true` when user needs access to the
downloaded content. If [`option: acceptDownloads`] is not set, download events are emitted, but the actual download is
@ -96,4 +102,4 @@ browsers can use different logic for computing it.
## method: Download.url
- returns: <[string]>
Returns downloaded url.
Returns downloaded url.

View file

@ -29,6 +29,12 @@ file_chooser = fc_info.value
file_chooser.set_files("myfile.pdf")
```
```csharp
var fileChooserTask = page.WaitForFileChooserAsync();
await Task.WhenAll(fileChooserTask, page.ClickAsync("upload"));
await fileChooserTask.Result.SetFilesAsync("temp.txt");
```
## method: FileChooser.element
- returns: <[ElementHandle]>