Browse Source

docs(dotnet): examples for dialogs, fixes (#6599)

pull/6603/head
Pavel Feldman 1 month ago
committed by GitHub
parent
commit
fd1e62b8c5
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      docs/src/api/class-browsercontext.md
  2. 7
      docs/src/api/class-download.md
  3. 7
      docs/src/api/class-filechooser.md
  4. 6
      docs/src/api/class-frame.md
  5. 18
      docs/src/api/class-page.md
  6. 7
      docs/src/api/class-request.md
  7. 19
      docs/src/dialogs.md
  8. 17
      docs/src/events.md
  9. 12
      docs/src/handles.md
  10. 8
      docs/src/input.md
  11. 15
      docs/src/multi-pages.md
  12. 8
      docs/src/navigations.md
  13. 21
      docs/src/network.md
  14. 15
      docs/src/verification.md

19
docs/src/api/class-browsercontext.md

@ -25,7 +25,7 @@ BrowserContext context = browser.newContext();
// Create a new page inside context.
Page page = context.newPage();
page.navigate("https://example.com");
// Dispose context once it"s no longer needed.
// Dispose context once it is no longer needed.
context.close();
```
@ -35,7 +35,7 @@ context = await browser.new_context()
# create a new page inside context.
page = await context.new_page()
await page.goto("https://example.com")
# dispose context once it"s no longer needed.
# dispose context once it is no longer needed.
await context.close()
```
@ -45,7 +45,7 @@ context = browser.new_context()
# create a new page inside context.
page = context.new_page()
page.goto("https://example.com")
# dispose context once it"s no longer needed.
# dispose context once it is no longer needed.
context.close()
```
@ -57,7 +57,7 @@ var context = await browser.NewContextAsync();
// Create a new page inside context.
var page = await context.NewPageAsync();
await page.GotoAsync("https://bing.com");
// Dispose context once it"s no longer needed.
// Dispose context once it is no longer needed.
await context.CloseAsync();
```
@ -133,9 +133,9 @@ print(page.evaluate("location.href"))
```csharp
var popupTask = context.WaitForPageAsync();
await Task.WhenAll(popupTask, page.ClickAsync("a"));
Console.WriteLine(popupTask.Result.EvaluateAsync<string>("location.href"));
await page.ClickAsync("a");
var popup = await popupTask;
Console.WriteLine(await popup.EvaluateAsync<string>("location.href"));
```
:::note
@ -1226,8 +1226,9 @@ page = event_info.value
```
```csharp
var t = page.WaitForEventAsync("page");
await Task.WhenAll(t, page.ClickAsync("button"));
var waitForPageEvent = context.WaitForPageAsync();
await page.ClickAsync("button");
var page = await waitForPageEvent;
```
### param: BrowserContext.waitForEvent.event

7
docs/src/api/class-download.md

@ -49,9 +49,10 @@ path = download.path()
```
```csharp
var downloadTask = page.WaitForDownloadAsync();
await Task.WhenAll(downloadTask, page.ClickAsync("#downloadButton"));
Console.WriteLine(await downloadTask.Result.PathAsync());
var waitForDownloadTask = page.WaitForDownloadAsync();
await page.ClickAsync("#downloadButton");
var download = await waitForDownloadTask;
Console.WriteLine(await download.PathAsync());
```
:::note

7
docs/src/api/class-filechooser.md

@ -30,9 +30,10 @@ file_chooser.set_files("myfile.pdf")
```
```csharp
var fileChooserTask = page.WaitForFileChooserAsync();
await Task.WhenAll(fileChooserTask, page.ClickAsync("upload"));
await fileChooserTask.Result.SetFilesAsync("temp.txt");
var waitForFileChooserTask = page.WaitForFileChooserAsync();
await page.ClickAsync("upload");
var fileChooser = await waitForFileChooserTask;
await fileChooser.SetFilesAsync("temp.txt");
```
## method: FileChooser.element

6
docs/src/api/class-frame.md

@ -1424,8 +1424,10 @@ with frame.expect_navigation():
```
```csharp
Task.WaitAll(frame.WaitForNavigationAsync(),
frame.ClickAsync("a.delayed-navigation")); // clicking the link will indirectly cause a navigation
await Task.WhenAll(
frame.WaitForNavigationAsync(),
// clicking the link will indirectly cause a navigation
frame.ClickAsync("a.delayed-navigation"));
// Resolves after navigation has finished
```

18
docs/src/api/class-page.md

@ -397,9 +397,10 @@ print(popup.evaluate("location.href"))
```
```csharp
var popupTask = page.WaitForPopupAsync();
await Task.WhenAll(popupTask, page.EvaluateAsync("() => window.open('https://microsoft.com')"));
Console.WriteLine(await popupTask.Result.EvaluateAsync<string>("location.href"));
var waitForPopupTask = page.WaitForPopupAsync();
await page.EvaluateAsync("() => window.open('https://microsoft.com')");
var popup = await waitForPopupTask;
Console.WriteLine(await popup.EvaluateAsync<string>("location.href"));
```
:::note
@ -2965,8 +2966,8 @@ frame = event_info.value
```csharp
var waitTask = page.WaitForEventAsync(PageEvent.FrameNavigated);
await Task.WhenAll(waitTask, page.ClickAsync("button"));
var frame = waitTask.Result;
await page.ClickAsync("button");
var frame = await waitTask;
```
### param: Page.waitForEvent.event = %%-wait-for-event-event-%%
@ -3194,9 +3195,10 @@ print(popup.title()) # popup is ready to use.
```csharp
var popupTask = page.WaitForPopupAsync();
await Task.WhenAll(popupTask, page.ClickAsync("button")); // click triggers the popup/
await popupTask.Result.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
Console.WriteLine(await popupTask.Result.TitleAsync()); // popup is ready to use.
await page.ClickAsync("button"); // click triggers the popup/
var popup = await popupTask;
await popup.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
Console.WriteLine(await popup.TitleAsync()); // popup is ready to use.
```
Shortcut for main frame's [`method: Frame.waitForLoadState`].

7
docs/src/api/class-request.md

@ -239,9 +239,10 @@ print(request.timing)
```
```csharp
var requestFinishedTask = page.WaitForEventAsync(PageEvent.RequestFinished);
await Task.WhenAll(requestFinishedTask, page.GotoAsync("https://www.microsoft.com"));
Console.WriteLine(requestFinishedTask.Result.Timing.ResponseEnd);
var waitForEventTask = page.WaitForEventAsync(PageEvent.RequestFinished);
await page.GotoAsync("https://www.microsoft.com");
var request = await waitForEventTask;
Console.WriteLine(request.Timing.ResponseEnd);
```
## method: Request.url

19
docs/src/dialogs.md

@ -31,6 +31,11 @@ page.on("dialog", lambda dialog: dialog.accept())
page.click("button")
```
```csharp
page.Dialog += (_, dialog) => dialog.AcceptAsync();
await page.ClickAsync("button");
```
:::note
[`event: Page.dialog`] listener **must handle** the dialog. Otherwise your action will stall, be it [`method: Page.click`], [`method: Page.evaluate`] or any other. That's because dialogs in Web are modal and block further page execution until they are handled.
:::
@ -61,6 +66,11 @@ page.on("dialog", lambda dialog: print(dialog.message))
page.click("button") # Will hang here
```
```csharp
page.Dialog += (_, dialog) => Console.WriteLine(dialog.Message);
await page.ClickAsync("button"); // Will hang here
```
:::note
If there is no listener for [`event: Page.dialog`], all dialogs are automatically dismissed.
:::
@ -110,3 +120,12 @@ def handle_dialog(dialog):
page.on('dialog', lambda: handle_dialog)
page.close(run_before_unload=True)
```
```csharp
page.Dialog += (_, dialog) =>
{
Assert.Equal("beforeunload", dialog.Type);
dialog.DismissAsync();
};
await page.CloseAsync(runBeforeUnload: true);
```

17
docs/src/events.md

@ -50,8 +50,6 @@ print(first.value.url)
```
```csharp
// The callback lambda defines scope of the code that is expected to
// trigger request.
var waitForRequestTask = page.WaitForRequestAsync("**/*logo*.png");
await page.GotoAsync("https://wikipedia.org");
var request = await waitForRequestTask;
@ -94,8 +92,6 @@ popup.value.goto("https://wikipedia.org")
```
```csharp
// The callback lambda defines scope of the code that is expected to
// create popup window.
var waitForPopupTask = page.WaitForPopupAsync();
await page.EvaluateAsync("window.open()");
var popup = await waitForPopupTask;
@ -160,18 +156,20 @@ page.goto("https://www.openstreetmap.org/")
```csharp
page.Request += (_, request) => Console.WriteLine("Request sent: " + request.Url);
EventHandler<IRequest> listener = (_, request) => {
Console.WriteLine("Request finished: " + request.Url);
void listener(object sender, IRequest request)
{
Console.WriteLine("Request finished: " + request.Url);
};
page.RequestFinished += listener;
await page.GotoAsync("https://wikipedia.org");
// Remove previously added listener, each on* method has corresponding off*
// Remove previously added listener.
page.RequestFinished -= listener;
await page.GotoAsync("https://www.openstreetmap.org/");
```
## Adding one-off listeners
* langs: js, python, java
If certain event needs to be handled once, there is a convenience API for that:
@ -195,11 +193,6 @@ page.once("dialog", lambda dialog: dialog.accept("2021"))
page.evaluate("prompt('Enter a number:')")
```
```csharp
page.Dialog += (_, dialog) => dialog.AcceptAsync("2021");
await page.EvaluateAsync("prompt('Enter a number:')");
```
### API reference
- [Browser]

12
docs/src/handles.md

@ -193,7 +193,7 @@ arg.put("myArray", myArrayHandle);
arg.put("newElement", 2);
page.evaluate("arg => arg.myArray.add(arg.newElement)", arg);
// Release the object when it"s no longer needed.
// Release the object when it is no longer needed.
myArrayHandle.dispose();
```
@ -239,10 +239,10 @@ my_array_handle.dispose()
```csharp
// Create new array in page.
var myArrayHandle = await page.EvaluateHandleAsync("() => {\n" +
" window.myArray = [1];\n" +
" return myArray;\n" +
"}");
var myArrayHandle = await page.EvaluateHandleAsync(@"() => {
window.myArray = [1];
return myArray;
}");
// Get the length of the array.
var length = await page.EvaluateAsync<int>("a => a.length", myArrayHandle);
@ -251,7 +251,7 @@ var length = await page.EvaluateAsync<int>("a => a.length", myArrayHandle);
await page.EvaluateAsync("arg => arg.myArray.add(arg.newElement)",
new { myArray = myArrayHandle, newElement = 2 });
// Release the object when it"s no longer needed.
// Release the object when it is no longer needed.
await myArrayHandle.DisposeAsync();
```

8
docs/src/input.md

@ -751,11 +751,9 @@ file_chooser.set_files("myfile.pdf")
```csharp
var waitForChooserTask = page.WaitForFileChooserAsync();
await Task.WhenAll(
waitForChooserTask,
page.ClickAsync("upload")
);
await waitForChooserTask.Result.SetFilesAsync("myfile.pdf");
await page.ClickAsync("upload");
var fileChooser = await waitForChooserTask;
await fileChooser.SetFilesAsync("myfile.pdf");
```
### API reference

15
docs/src/multi-pages.md

@ -210,11 +210,9 @@ print(new_page.title())
```csharp
// Get page after a specific action (e.g. clicking a link)
var waitForPageTask = context.WaitForPageAsync();
await Task.WhenAll(
waitForPageTask,
page.ClickAsync("a[target='_blank']")
);
await waitForPageTask.Result.WaitForLoadStateAsync();
await page.ClickAsync("a[target='_blank']");
var newPage = await waitForPageTask;
await page.WaitForLoadStateAsync();
Console.WriteLine(await newPage.TitleAsync());
```
@ -312,10 +310,9 @@ print(popup.title())
```csharp
// Get popup after a specific action (e.g., click)
var newPage = await Task.WhenAll(
page.WaitForPopupAsync(),
page.ClickAsync("#open")
);
var waitForPopupTask = page.WaitForPopupAsync();
await page.ClickAsync("#open");
var newPage = await waitForPopupTask;
await newPage.WaitForLoadStateAsync();
Console.WriteLine(await newPage.TitleAsync());
```

8
docs/src/navigations.md

@ -435,11 +435,9 @@ popup.wait_for_load_state("load")
```csharp
var waitForPopupTask = page.WaitForPopupAsync();
await Task.WhenAll(
waitForPopupTask,
page.ClickAsync("a[target='_blank']") // Opens popup
);
await waitForPopupTask.Result.WaitForLoadStateAsync(LoadState.Load);
await page.ClickAsync("a[target='_blank']"); // Opens popup
var popup = await waitForPopupTask;
popup.WaitForLoadStateAsync(LoadState.Load);
```
### API reference

21
docs/src/network.md

@ -282,11 +282,8 @@ response = response_info.value
```csharp
// Use a glob URL pattern
var waitForResponseTask = page.WaitForResponseAsync("**/api/fetch_data");
await Task.WhenAll(
waitForResponseTask,
page.ClickAsync("button#update")
);
var response = waitForResponseTask.Result;
await page.ClickAsync("button#update");
var response = await waitForResponseTask;
```
#### Variations
@ -344,19 +341,13 @@ response = response_info.value
```csharp
// Use a regular expression
var waitForResponseTask = page.WaitForResponseAsync(new Regex("\\.jpeg$"));
await Task.WhenAll(
waitForResponseTask,
page.ClickAsync("button#update")
);
var response = waitForResponseTask.Result;
await page.ClickAsync("button#update");
var response = await waitForResponseTask;
// Use a predicate taking a Response object
var waitForResponseTask = page.WaitForResponseAsync(r => r.Url.Contains(token));
await Task.WhenAll(
waitForResponseTask,
page.ClickAsync("button#update")
);
var response = waitForResponseTask.Result;
await page.ClickAsync("button#update");
var response = await waitForResponseTask;
```
### API reference

15
docs/src/verification.md

@ -103,14 +103,11 @@ page.Console += (_, msg) =>
// Get the next System.out.println
var waitForMessageTask = page.WaitForConsoleMessageAsync();
await Task.WhenAll(
waitForMessageTask,
page.EvaluateAsync("console.log('hello', 42, { foo: 'bar' });")
);
await page.EvaluateAsync("console.log('hello', 42, { foo: 'bar' });");
var message = await waitForMessageTask;
// Deconstruct console.log arguments
await waitForMessageTask.Result.Args.ElementAt(0).JsonValueAsync<string>(); // hello
await waitForMessageTask.Result.Args.ElementAt(1).JsonValueAsync<int>(); // 42
await message.Args.ElementAt(0).JsonValueAsync<string>(); // hello
await message.Args.ElementAt(1).JsonValueAsync<int>(); // 42
```
### API reference
@ -248,8 +245,8 @@ popup = popup_info.value
```csharp
var waitForPopupTask = page.WaitForPopupAsync();
await Task.WhenAll(waitForPopupTask, page.ClickAsync("#open"));
var popup = waitForPopupTask.Result;
await page.ClickAsync("#open");
var popup = await waitForPopupTask;
```
### API reference

Loading…
Cancel
Save