mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
docs(dotnet): examples for verification, video, fixes (#6597)
Will follow up...
This commit is contained in:
parent
bbc3ebd512
commit
9aa610063c
@ -750,11 +750,12 @@ file_chooser.set_files("myfile.pdf")
|
|||||||
```
|
```
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
var fileChooser = await Task.WhenAll(
|
var waitForChooserTask = page.WaitForFileChooserAsync();
|
||||||
page.WaitForFileChooserAsync(),
|
await Task.WhenAll(
|
||||||
|
waitForChooserTask,
|
||||||
page.ClickAsync("upload")
|
page.ClickAsync("upload")
|
||||||
);
|
);
|
||||||
await fileChooser.SetFilesAsync("myfile.pdf");
|
await waitForChooserTask.Result.SetFilesAsync("myfile.pdf");
|
||||||
```
|
```
|
||||||
|
|
||||||
### API reference
|
### API reference
|
||||||
|
@ -209,11 +209,12 @@ print(new_page.title())
|
|||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
// Get page after a specific action (e.g. clicking a link)
|
// Get page after a specific action (e.g. clicking a link)
|
||||||
var newPage = await Task.WhenAll(
|
var waitForPageTask = context.WaitForPageAsync();
|
||||||
context.WaitForPageAsync(),
|
await Task.WhenAll(
|
||||||
|
waitForPageTask,
|
||||||
page.ClickAsync("a[target='_blank']")
|
page.ClickAsync("a[target='_blank']")
|
||||||
);
|
);
|
||||||
await newPage.WaitForLoadStateAsync();
|
await waitForPageTask.Result.WaitForLoadStateAsync();
|
||||||
Console.WriteLine(await newPage.TitleAsync());
|
Console.WriteLine(await newPage.TitleAsync());
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -434,11 +434,12 @@ popup.wait_for_load_state("load")
|
|||||||
```
|
```
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
var popup = await Task.WhenAll(
|
var waitForPopupTask = page.WaitForPopupAsync();
|
||||||
page.WaitForPopupAsync(),
|
await Task.WhenAll(
|
||||||
|
waitForPopupTask,
|
||||||
page.ClickAsync("a[target='_blank']") // Opens popup
|
page.ClickAsync("a[target='_blank']") // Opens popup
|
||||||
);
|
);
|
||||||
await popup.WaitForLoadStateAsync(LoadState.Load);
|
await waitForPopupTask.Result.WaitForLoadStateAsync(LoadState.Load);
|
||||||
```
|
```
|
||||||
|
|
||||||
### API reference
|
### API reference
|
||||||
|
@ -281,10 +281,13 @@ response = response_info.value
|
|||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
// Use a glob URL pattern
|
// Use a glob URL pattern
|
||||||
var response = await Task.WhenAll(
|
var waitForResponseTask = page.WaitForResponseAsync(
|
||||||
page.WaitForResponseAsync("**/api/fetch_data"),
|
"**/api/fetch_data");
|
||||||
|
await Task.WhenAll(
|
||||||
|
waitForResponseTask,
|
||||||
page.ClickAsync("button#update")
|
page.ClickAsync("button#update")
|
||||||
);
|
);
|
||||||
|
var response = waitForResponseTask.Result;
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Variations
|
#### Variations
|
||||||
@ -341,16 +344,22 @@ response = response_info.value
|
|||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
// Use a regular expression
|
// Use a regular expression
|
||||||
var response = await Task.WhenAll(
|
var waitForResponseTask = page.WaitForResponseAsync(
|
||||||
page.WaitForResponseAsync(new Regex("\\.jpeg$")),
|
new Regex("\\.jpeg$"));
|
||||||
|
await Task.WhenAll(
|
||||||
|
waitForResponseTask,
|
||||||
page.ClickAsync("button#update")
|
page.ClickAsync("button#update")
|
||||||
);
|
);
|
||||||
|
var response = waitForResponseTask.Result;
|
||||||
|
|
||||||
// Use a predicate taking a Response object
|
// Use a predicate taking a Response object
|
||||||
var response = await Task.WhenAll(
|
var waitForResponseTask = page.WaitForResponseAsync(
|
||||||
page.WaitForResponseAsync(r => r.Url.Contains(token)),
|
r => r.Url.Contains(token));
|
||||||
|
await Task.WhenAll(
|
||||||
|
waitForResponseTask,
|
||||||
page.ClickAsync("button#update")
|
page.ClickAsync("button#update")
|
||||||
);
|
);
|
||||||
|
var response = waitForResponseTask.Result;
|
||||||
```
|
```
|
||||||
|
|
||||||
### API reference
|
### API reference
|
||||||
|
@ -90,6 +90,28 @@ msg.args[0].json_value() # hello
|
|||||||
msg.args[1].json_value() # 42
|
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
|
### API reference
|
||||||
- [ConsoleMessage]
|
- [ConsoleMessage]
|
||||||
- [Page]
|
- [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>")
|
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
|
### API reference
|
||||||
- [Page]
|
- [Page]
|
||||||
- [`event: Page.pageError`]
|
- [`event: Page.pageError`]
|
||||||
@ -181,6 +210,12 @@ page.onDialog(dialog -> {
|
|||||||
page.on("dialog", lambda dialog: dialog.accept())
|
page.on("dialog", lambda dialog: dialog.accept())
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
page.RequestFailed += (_, request) => {
|
||||||
|
Console.WriteLine(request.Url + " " + request.Failure);
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
#### `"popup"` - handle popup windows
|
#### `"popup"` - handle popup windows
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -208,6 +243,12 @@ with page.expect_popup() as popup_info:
|
|||||||
popup = popup_info.value
|
popup = popup_info.value
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
var waitForPopupTask = page.WaitForPopupAsync();
|
||||||
|
await Task.WhenAll(waitForPopupTask, page.ClickAsync("#open"));
|
||||||
|
var popup = waitForPopupTask.Result;
|
||||||
|
```
|
||||||
|
|
||||||
### API reference
|
### API reference
|
||||||
- [Page]
|
- [Page]
|
||||||
- [`event: Page.requestFailed`]
|
- [`event: Page.requestFailed`]
|
||||||
|
@ -32,13 +32,19 @@ context = browser.new_context(record_video_dir="videos/")
|
|||||||
context.close()
|
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.
|
You can also specify video size, it defaults to viewport size scaled down to fit 800x800.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const context = await browser.newContext({
|
const context = await browser.newContext({
|
||||||
recordVideo: {
|
recordVideo: {
|
||||||
dir: 'videos/',
|
dir: 'videos/',
|
||||||
size: { width: 1024, height: 768 },
|
size: { width: 640, height: 480 },
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
@ -46,23 +52,32 @@ const context = await browser.newContext({
|
|||||||
```java
|
```java
|
||||||
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
|
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
|
||||||
.setRecordVideoDir(Paths.get("videos/"))
|
.setRecordVideoDir(Paths.get("videos/"))
|
||||||
.setRecordVideoSize(1024, 768));
|
.setRecordVideoSize(640, 480));
|
||||||
```
|
```
|
||||||
|
|
||||||
```python async
|
```python async
|
||||||
context = await browser.new_context(
|
context = await browser.new_context(
|
||||||
record_video_dir="videos/",
|
record_video_dir="videos/",
|
||||||
record_video_size={"width": 1024, "height": 768}
|
record_video_size={"width": 640, "height": 480}
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
```python sync
|
```python sync
|
||||||
context = browser.new_context(
|
context = browser.new_context(
|
||||||
record_video_dir="videos/",
|
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.
|
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
|
For the multi-page scenarios, you can access the video file associated with the page via the
|
||||||
[`method: Page.video`].
|
[`method: Page.video`].
|
||||||
@ -84,6 +99,10 @@ path = await page.video.path()
|
|||||||
path = page.video.path()
|
path = page.video.path()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
var path = await page.Video.PathAsync();
|
||||||
|
```
|
||||||
|
|
||||||
:::note
|
:::note
|
||||||
Note that the video is only available after the page or browser context is closed.
|
Note that the video is only available after the page or browser context is closed.
|
||||||
:::
|
:::
|
||||||
|
Loading…
x
Reference in New Issue
Block a user