docs(dotnet): fix code snippets for GetByText

This commit is contained in:
amaechi hope 2024-10-30 16:25:59 +01:00
parent 183720b56a
commit a8ee6d0017
19 changed files with 49 additions and 49 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

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

@ -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,7 +3186,7 @@ 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();
}); });
@ -3243,7 +3243,7 @@ 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();
}); });
@ -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

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

@ -61,7 +61,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

@ -85,7 +85,7 @@ popup.value.goto("https://wikipedia.org")
```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();
}); });
await popup.GotoAsync("https://wikipedia.org"); await popup.GotoAsync("https://wikipedia.org");
``` ```

View file

@ -327,7 +327,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

@ -301,23 +301,23 @@ page.get_by_text("Item").click(position={ "x": 0, "y": 0})
await page.GetByRole(AriaRole.Button).ClickAsync(); await page.GetByRole(AriaRole.Button).ClickAsync();
// Double click // Double click
await page.GetByText("Item").DblClickAsync(); await Page.GetByText("Item").DblClickAsync();
// Right click // Right click
await page.GetByText("Item").ClickAsync(new() { Button = MouseButton.Right }); await Page.GetByText("Item").ClickAsync(new() { Button = MouseButton.Right });
// Shift + click // Shift + click
await page.GetByText("Item").ClickAsync(new() { Modifiers = new[] { KeyboardModifier.Shift } }); await Page.GetByText("Item").ClickAsync(new() { Modifiers = new[] { KeyboardModifier.Shift } });
// Ctrl + click or Windows and Linux // Ctrl + click or Windows and Linux
// Meta + click on macOS // Meta + click on macOS
await page.GetByText("Item").ClickAsync(new() { Modifiers = new[] { KeyboardModifier.ControlOrMeta } }); await Page.GetByText("Item").ClickAsync(new() { Modifiers = new[] { KeyboardModifier.ControlOrMeta } });
// Hover over element // Hover over element
await page.GetByText("Item").HoverAsync(); await Page.GetByText("Item").HoverAsync();
// Click the top left corner // Click the top left corner
await page.GetByText("Item").ClickAsync(new() { position = new Position { X = 0, Y = 0 } }); await Page.GetByText("Item").ClickAsync(new() { position = new Position { X = 0, Y = 0 } });
``` ```
Under the hood, this and other pointer-related methods: Under the hood, this and other pointer-related methods:
@ -460,7 +460,7 @@ page.get_by_role("textbox").press("$")
```csharp ```csharp
// Hit Enter // Hit Enter
await page.GetByText("Submit").PressAsync("Enter"); await Page.GetByText("Submit").PressAsync("Enter");
// Dispatch Control+Right // Dispatch Control+Right
await page.GetByRole(AriaRole.Textbox).PressAsync("Control+ArrowRight"); await page.GetByRole(AriaRole.Textbox).PressAsync("Control+ArrowRight");
@ -826,7 +826,7 @@ page.get_by_text("Footer text").scroll_into_view_if_needed()
```csharp ```csharp
// Scroll the footer into view, forcing an "infinite list" to load more content // Scroll the footer into view, forcing an "infinite list" to load more content
await page.GetByText("Footer text").ScrollIntoViewIfNeededAsync(); await Page.GetByText("Footer text").ScrollIntoViewIfNeededAsync();
``` ```
If you would like to control the scrolling more precisely, use [`method: Mouse.wheel`] or [`method: Locator.evaluate`]: If you would like to control the scrolling more precisely, use [`method: Mouse.wheel`] or [`method: Locator.evaluate`]:

View file

@ -730,7 +730,7 @@ await page.get_by_text("Details").click()
page.get_by_text("Details").click() page.get_by_text("Details").click()
``` ```
```csharp ```csharp
await page.GetByText("Details").ClickAsync(); await Page.GetByText("Details").ClickAsync();
``` ```
```html ```html
@ -1265,7 +1265,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).First).ToBeVisibleAsync(); await Expect(newEmail.Or(dialog).First).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();
@ -1441,7 +1441,7 @@ page.getByText("orange").click();
``` ```
```csharp ```csharp
await page.GetByText("orange").ClickAsync(); await Page.GetByText("orange").ClickAsync();
``` ```
#### Filter by text #### Filter by text

View file

@ -76,7 +76,7 @@ await page.RouteAsync("*/**/api/v1/fruits", async route => {
await page.GotoAsync("https://demo.playwright.dev/api-mocking"); await page.GotoAsync("https://demo.playwright.dev/api-mocking");
// Assert that the Strawberry fruit is visible // Assert that the Strawberry fruit is visible
await Expect(page.GetByTextAsync("Strawberry")).ToBeVisibleAsync(); await Expect(Page.GetByTextAsync("Strawberry")).ToBeVisibleAsync();
``` ```
```java ```java
@ -188,7 +188,7 @@ await page.RouteAsync("*/**/api/v1/fruits", async (route) => {
await page.GotoAsync("https://demo.playwright.dev/api-mocking"); await page.GotoAsync("https://demo.playwright.dev/api-mocking");
// Assert that the Loquat fruit is visible // Assert that the Loquat fruit is visible
await Expect(page.GetByTextAsync("Loquat", new () { Exact = true })).ToBeVisibleAsync(); await Expect(Page.GetByTextAsync("Loquat", new () { Exact = true })).ToBeVisibleAsync();
``` ```
```java ```java
@ -283,7 +283,7 @@ await page.RouteFromHARAsync("./hars/fruit.har", new () {
await page.GotoAsync("https://demo.playwright.dev/api-mocking"); await page.GotoAsync("https://demo.playwright.dev/api-mocking");
// Assert that the fruit is visible // Assert that the fruit is visible
await Expect(page.GetByText("Strawberry")).ToBeVisibleAsync(); await Expect(Page.GetByText("Strawberry")).ToBeVisibleAsync();
``` ```
```java ```java

View file

@ -89,7 +89,7 @@ page.get_by_text("example domain").click()
// Navigate and click element // Navigate and click element
// Click will auto-wait for the element // Click will auto-wait for the element
await page.GotoAsync("https://example.com"); await page.GotoAsync("https://example.com");
await page.GetByText("Example Domain").ClickAsync(); await Page.GetByText("Example Domain").ClickAsync();
``` ```
For the scenario above, Playwright will wait for the text to become visible, For the scenario above, Playwright will wait for the text to become visible,
@ -146,7 +146,7 @@ page.wait_for_url("**/login")
``` ```
```csharp ```csharp
await page.GetByText("Click me").ClickAsync(); await Page.GetByText("Click me").ClickAsync();
await page.WaitForURL("**/login"); await page.WaitForURL("**/login");
``` ```

View file

@ -338,7 +338,7 @@ response = response_info.value
```csharp ```csharp
// Use a glob URL pattern // Use a glob URL pattern
var waitForResponseTask = page.WaitForResponseAsync("**/api/fetch_data"); var waitForResponseTask = page.WaitForResponseAsync("**/api/fetch_data");
await page.GetByText("Update").ClickAsync(); await Page.GetByText("Update").ClickAsync();
var response = await waitForResponseTask; var response = await waitForResponseTask;
``` ```
@ -397,12 +397,12 @@ response = response_info.value
```csharp ```csharp
// Use a regular expression // Use a regular expression
var waitForResponseTask = page.WaitForResponseAsync(new Regex("\\.jpeg$")); var waitForResponseTask = page.WaitForResponseAsync(new Regex("\\.jpeg$"));
await page.GetByText("Update").ClickAsync(); await Page.GetByText("Update").ClickAsync();
var response = await waitForResponseTask; var response = await waitForResponseTask;
// Use a predicate taking a Response object // Use a predicate taking a Response object
var waitForResponseTask = page.WaitForResponseAsync(r => r.Url.Contains(token)); var waitForResponseTask = page.WaitForResponseAsync(r => r.Url.Contains(token));
await page.GetByText("Update").ClickAsync(); await Page.GetByText("Update").ClickAsync();
var response = await waitForResponseTask; var response = await waitForResponseTask;
``` ```

View file

@ -464,7 +464,7 @@ parent = page.get_by_role("listitem").filter(has=child)
``` ```
```csharp ```csharp
var child = page.GetByText("Hello"); var child = Page.GetByText("Hello");
var parent = page.GetByRole(AriaRole.Listitem).Filter(new () { Has = child }); var parent = page.GetByRole(AriaRole.Listitem).Filter(new () { Has = child });
``` ```
@ -487,7 +487,7 @@ parent = page.get_by_text("Hello").locator('xpath=..')
``` ```
```csharp ```csharp
var parent = page.GetByText("Hello").Locator("xpath=.."); var parent = Page.GetByText("Hello").Locator("xpath=..");
``` ```
## React locator ## React locator
@ -698,7 +698,7 @@ page.get_by_text("Password").fill("secret")
```csharp ```csharp
// Fill the input by targeting the label. // Fill the input by targeting the label.
await page.GetByText("Password").FillAsync("secret"); await Page.GetByText("Password").FillAsync("secret");
``` ```
However, other methods will target the label itself, for example [`method: LocatorAssertions.toHaveText`] will assert the text content of the label, not the input field. However, other methods will target the label itself, for example [`method: LocatorAssertions.toHaveText`] will assert the text content of the label, not the input field.

View file

@ -184,7 +184,7 @@ print(new_page.title())
// 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 context.RunAndWaitForPageAsync(async () => var newPage = await context.RunAndWaitForPageAsync(async () =>
{ {
await page.GetByText("open new tab").ClickAsync(); await Page.GetByText("open new tab").ClickAsync();
}); });
// Interact with the new page normally // Interact with the new page normally
await newPage.GetByRole(AriaRole.Button).ClickAsync(); await newPage.GetByRole(AriaRole.Button).ClickAsync();
@ -288,7 +288,7 @@ print(popup.title())
// Get popup after a specific action (e.g., click) // Get popup after a specific action (e.g., click)
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();
}); });
// Interact with the popup normally // Interact with the popup normally
await popup.GetByRole(AriaRole.Button).ClickAsync(); await popup.GetByRole(AriaRole.Button).ClickAsync();

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,
});`); });`);