docs(dotnet): fix code snippets

This commit is contained in:
amaechi hope 2024-11-08 17:35:36 +01:00
parent 2a5f9a2e7b
commit e5833c2622
14 changed files with 33 additions and 33 deletions

View file

@ -256,7 +256,7 @@ print(page.evaluate("location.href"))
```csharp ```csharp
var popup = await context.RunAndWaitForPageAsync(async => var popup = await context.RunAndWaitForPageAsync(async =>
{ {
await Page.GetByText("open new page").ClickAsync(); await page.GetByText("open new page").ClickAsync();
}); });
Console.WriteLine(await popup.EvaluateAsync<string>("location.href")); Console.WriteLine(await popup.EvaluateAsync<string>("location.href"));
``` ```

View file

@ -54,7 +54,7 @@ download.save_as("/path/to/save/at/" + download.suggested_filename)
```csharp ```csharp
// Start the task of waiting for the download before clicking // Start the task of waiting for the download before clicking
var waitForDownloadTask = page.WaitForDownloadAsync(); var waitForDownloadTask = page.WaitForDownloadAsync();
await Page.GetByText("Download file").ClickAsync(); await page.GetByText("Download file").ClickAsync();
var download = await waitForDownloadTask; var download = await waitForDownloadTask;
// Wait for the download process to complete and save the downloaded file somewhere // Wait for the download process to complete and save the downloaded file somewhere

View file

@ -101,7 +101,7 @@ locator.click()
``` ```
```csharp ```csharp
var locator = Page.GetByText("Submit"); var locator = page.GetByText("Submit");
await locator.HoverAsync(); await locator.HoverAsync();
await locator.ClickAsync(); await locator.ClickAsync();
``` ```

View file

@ -33,7 +33,7 @@ file_chooser.set_files("myfile.pdf")
```csharp ```csharp
var fileChooser = await page.RunAndWaitForFileChooserAsync(async () => var fileChooser = await page.RunAndWaitForFileChooserAsync(async () =>
{ {
await Page.GetByText("Upload file").ClickAsync(); await page.GetByText("Upload file").ClickAsync();
}); });
await fileChooser.SetFilesAsync("temp.txt"); await fileChooser.SetFilesAsync("temp.txt");
``` ```

View file

@ -290,7 +290,7 @@ browser.close()
``` ```
```csharp ```csharp
await Page.GotoAsync("https://keycode.info"); await page.GotoAsync("https://keycode.info");
await page.Keyboard.PressAsync("A"); await page.Keyboard.PressAsync("A");
await page.ScreenshotAsync(new() { Path = "A.png" }); await page.ScreenshotAsync(new() { Path = "A.png" });
await page.Keyboard.PressAsync("ArrowLeft"); await page.Keyboard.PressAsync("ArrowLeft");

View file

@ -1757,7 +1757,7 @@ new_email.click()
```csharp ```csharp
var newEmail = page.GetByRole(AriaRole.Button, new() { Name = "New" }); var newEmail = page.GetByRole(AriaRole.Button, new() { Name = "New" });
var dialog = Page.GetByText("Confirm security settings"); var dialog = page.GetByText("Confirm security settings");
await Expect(newEmail.Or(dialog)).ToBeVisibleAsync(); await Expect(newEmail.Or(dialog)).ToBeVisibleAsync();
if (await dialog.IsVisibleAsync()) if (await dialog.IsVisibleAsync())
await page.GetByRole(AriaRole.Button, new() { Name = "Dismiss" }).ClickAsync(); await page.GetByRole(AriaRole.Button, new() { Name = "Dismiss" }).ClickAsync();

View file

@ -2138,7 +2138,7 @@ expect(page.locator('body')).to_match_aria_snapshot('''
``` ```
```csharp ```csharp
await Page.GotoAsync("https://demo.playwright.dev/todomvc/"); await page.GotoAsync("https://demo.playwright.dev/todomvc/");
await Expect(page.Locator("body")).ToMatchAriaSnapshotAsync(@" await Expect(page.Locator("body")).ToMatchAriaSnapshotAsync(@"
- heading ""todos"" - heading ""todos""
- textbox ""What needs to be done?"" - textbox ""What needs to be done?""

View file

@ -485,7 +485,7 @@ print(popup.evaluate("location.href"))
```csharp ```csharp
var popup = await page.RunAndWaitForPopupAsync(async () => var popup = await page.RunAndWaitForPopupAsync(async () =>
{ {
await Page.GetByText("open the popup").ClickAsync(); await page.GetByText("open the popup").ClickAsync();
}); });
Console.WriteLine(await popup.EvaluateAsync<string>("location.href")); Console.WriteLine(await popup.EvaluateAsync<string>("location.href"));
``` ```
@ -3186,12 +3186,12 @@ await page.get_by_role("button", name="Start here").click()
```csharp ```csharp
// Setup the handler. // Setup the handler.
await page.AddLocatorHandlerAsync(Page.GetByText("Sign up to the newsletter"), async () => { await page.AddLocatorHandlerAsync(page.GetByText("Sign up to the newsletter"), async () => {
await page.GetByRole(AriaRole.Button, new() { Name = "No thanks" }).ClickAsync(); await page.GetByRole(AriaRole.Button, new() { Name = "No thanks" }).ClickAsync();
}); });
// Write the test as usual. // Write the test as usual.
await Page.GotoAsync("https://example.com"); await page.GotoAsync("https://example.com");
await page.GetByRole("button", new() { Name = "Start here" }).ClickAsync(); await page.GetByRole("button", new() { Name = "Start here" }).ClickAsync();
``` ```
@ -3243,12 +3243,12 @@ await page.get_by_role("button", name="Start here").click()
```csharp ```csharp
// Setup the handler. // Setup the handler.
await page.AddLocatorHandlerAsync(Page.GetByText("Confirm your security details"), async () => { await page.AddLocatorHandlerAsync(page.GetByText("Confirm your security details"), async () => {
await page.GetByRole(AriaRole.Button, new() { Name = "Remind me later" }).ClickAsync(); await page.GetByRole(AriaRole.Button, new() { Name = "Remind me later" }).ClickAsync();
}); });
// Write the test as usual. // Write the test as usual.
await Page.GotoAsync("https://example.com"); await page.GotoAsync("https://example.com");
await page.GetByRole("button", new() { Name = "Start here" }).ClickAsync(); await page.GetByRole("button", new() { Name = "Start here" }).ClickAsync();
``` ```
@ -3305,7 +3305,7 @@ await page.AddLocatorHandlerAsync(page.Locator("body"), async () => {
}, new() { NoWaitAfter = true }); }, new() { NoWaitAfter = true });
// Write the test as usual. // Write the test as usual.
await Page.GotoAsync("https://example.com"); await page.GotoAsync("https://example.com");
await page.GetByRole("button", new() { Name = "Start here" }).ClickAsync(); await page.GetByRole("button", new() { Name = "Start here" }).ClickAsync();
``` ```
@ -3336,7 +3336,7 @@ await page.add_locator_handler(page.get_by_label("Close"), handler, times=1)
``` ```
```csharp ```csharp
await page.AddLocatorHandlerAsync(Page.GetByText("Sign up to the newsletter"), async locator => { await page.AddLocatorHandlerAsync(page.GetByText("Sign up to the newsletter"), async locator => {
await locator.ClickAsync(); await locator.ClickAsync();
}, new() { Times = 1 }); }, new() { Times = 1 });
``` ```
@ -4734,7 +4734,7 @@ with page.expect_navigation():
await page.RunAndWaitForNavigationAsync(async () => await page.RunAndWaitForNavigationAsync(async () =>
{ {
// This action triggers the navigation after a timeout. // This action triggers the navigation after a timeout.
await Page.GetByText("Navigate after timeout").ClickAsync(); await page.GetByText("Navigate after timeout").ClickAsync();
}); });
// The method continues after navigation has finished // The method continues after navigation has finished
@ -4865,13 +4865,13 @@ second_request = second.value
// Waits for the next request with the specified url. // Waits for the next request with the specified url.
await page.RunAndWaitForRequestAsync(async () => await page.RunAndWaitForRequestAsync(async () =>
{ {
await Page.GetByText("trigger request").ClickAsync(); await page.GetByText("trigger request").ClickAsync();
}, "http://example.com/resource"); }, "http://example.com/resource");
// Alternative way with a predicate. // Alternative way with a predicate.
await page.RunAndWaitForRequestAsync(async () => await page.RunAndWaitForRequestAsync(async () =>
{ {
await Page.GetByText("trigger request").ClickAsync(); await page.GetByText("trigger request").ClickAsync();
}, request => request.Url == "https://example.com" && request.Method == "GET"); }, request => request.Url == "https://example.com" && request.Method == "GET");
``` ```
@ -5009,13 +5009,13 @@ return response.ok
// Waits for the next response with the specified url. // Waits for the next response with the specified url.
await page.RunAndWaitForResponseAsync(async () => await page.RunAndWaitForResponseAsync(async () =>
{ {
await Page.GetByText("trigger response").ClickAsync(); await page.GetByText("trigger response").ClickAsync();
}, "http://example.com/resource"); }, "http://example.com/resource");
// Alternative way with a predicate. // Alternative way with a predicate.
await page.RunAndWaitForResponseAsync(async () => await page.RunAndWaitForResponseAsync(async () =>
{ {
await Page.GetByText("trigger response").ClickAsync(); await page.GetByText("trigger response").ClickAsync();
}, response => response.Url == "https://example.com" && response.Status == 200 && response.Request.Method == "GET"); }, response => response.Url == "https://example.com" && response.Status == 200 && response.Request.Method == "GET");
``` ```

View file

@ -214,7 +214,7 @@ print(response.request.redirected_from.url) # "http://example.com"
``` ```
```csharp ```csharp
var response = await Page.GotoAsync("http://www.microsoft.com"); var response = await page.GotoAsync("http://www.microsoft.com");
Console.WriteLine(response.Request.RedirectedFrom?.Url); // http://www.microsoft.com Console.WriteLine(response.Request.RedirectedFrom?.Url); // http://www.microsoft.com
``` ```
@ -241,7 +241,7 @@ print(response.request.redirected_from) # None
``` ```
```csharp ```csharp
var response = await Page.GotoAsync("https://www.google.com"); var response = await page.GotoAsync("https://www.google.com");
Console.WriteLine(response.Request.RedirectedFrom?.Url); // null Console.WriteLine(response.Request.RedirectedFrom?.Url); // null
``` ```
@ -368,7 +368,7 @@ print(request.timing)
```csharp ```csharp
var request = await page.RunAndWaitForRequestFinishedAsync(async () => var request = await page.RunAndWaitForRequestFinishedAsync(async () =>
{ {
await Page.GotoAsync("https://www.microsoft.com"); await page.GotoAsync("https://www.microsoft.com");
}); });
Console.WriteLine(request.Timing.ResponseEnd); Console.WriteLine(request.Timing.ResponseEnd);
``` ```

View file

@ -250,7 +250,7 @@ var page = await context.NewPageAsync();
await page.GotoAsync("https://playwright.dev"); await page.GotoAsync("https://playwright.dev");
await context.Tracing.StartChunkAsync(); await context.Tracing.StartChunkAsync();
await Page.GetByText("Get Started").ClickAsync(); await page.GetByText("Get Started").ClickAsync();
// Everything between StartChunkAsync and StopChunkAsync will be recorded in the trace. // Everything between StartChunkAsync and StopChunkAsync will be recorded in the trace.
await context.Tracing.StopChunkAsync(new() await context.Tracing.StopChunkAsync(new()
{ {

View file

@ -1505,19 +1505,19 @@ page.getByText(Pattern.compile("^hello$", Pattern.CASE_INSENSITIVE));
```csharp ```csharp
// Matches <span> // Matches <span>
Page.GetByText("world"); page.GetByText("world");
// Matches first <div> // Matches first <div>
Page.GetByText("Hello world"); page.GetByText("Hello world");
// Matches second <div> // Matches second <div>
Page.GetByText("Hello", new() { Exact = true }); page.GetByText("Hello", new() { Exact = true });
// Matches both <div>s // Matches both <div>s
Page.GetByText(new Regex("Hello")); page.GetByText(new Regex("Hello"));
// Matches second <div> // Matches second <div>
Page.GetByText(new Regex("^hello$", RegexOptions.IgnoreCase)); page.GetByText(new Regex("^hello$", RegexOptions.IgnoreCase));
``` ```
**Details** **Details**

View file

@ -152,11 +152,11 @@ void listener(object sender, IRequest request)
Console.WriteLine("Request finished: " + request.Url); Console.WriteLine("Request finished: " + request.Url);
}; };
page.RequestFinished += listener; page.RequestFinished += listener;
await Page.GotoAsync("https://wikipedia.org"); await page.GotoAsync("https://wikipedia.org");
// Remove previously added listener. // Remove previously added listener.
page.RequestFinished -= listener; page.RequestFinished -= listener;
await Page.GotoAsync("https://www.openstreetmap.org/"); await page.GotoAsync("https://www.openstreetmap.org/");
``` ```
## Adding one-off listeners ## Adding one-off listeners

View file

@ -476,7 +476,7 @@ page.goto("https://example.com")
await page.RouteAsync("**/api/fetch_data", async route => { await page.RouteAsync("**/api/fetch_data", async route => {
await route.FulfillAsync(new() { Status = 200, Body = testData }); await route.FulfillAsync(new() { Status = 200, Body = testData });
}); });
await Page.GotoAsync("https://example.com"); await page.GotoAsync("https://example.com");
``` ```
## Modify requests ## Modify requests

View file

@ -774,7 +774,7 @@ var page1 = await page.RunAndWaitForPopupAsync(async () =>
expect.soft(sources.get('C#')!.text).toContain(` expect.soft(sources.get('C#')!.text).toContain(`
await page.GotoAsync("about:blank"); await page.GotoAsync("about:blank");
await Page.GetByText("link").ClickAsync();`); await page.GetByText("link").ClickAsync();`);
expect(page.url()).toContain('about:blank#foo'); expect(page.url()).toContain('about:blank#foo');
}); });
@ -819,7 +819,7 @@ await Page.GetByText("link").ClickAsync();`);
.setButton(MouseButton.MIDDLE));`); .setButton(MouseButton.MIDDLE));`);
expect(sources.get('C#')!.text).toContain(` expect(sources.get('C#')!.text).toContain(`
await Page.GetByText("Click me").ClickAsync(new LocatorClickOptions await page.GetByText("Click me").ClickAsync(new LocatorClickOptions
{ {
Button = MouseButton.Middle, Button = MouseButton.Middle,
});`); });`);