docs(dotnet): fix syntactically incorrect code snippets (#23900)
This commit is contained in:
Родитель
e1c220a37b
Коммит
69ae8c1a28
|
@ -165,6 +165,8 @@ public class TestGitHubAPI : PlaywrightTest
|
|||
These tests assume that repository exists. You probably want to create a new one before running tests and delete it afterwards. Use `[SetUp]` and `[TearDown]` hooks for that.
|
||||
|
||||
```csharp
|
||||
public class TestGitHubAPI : PlaywrightTest
|
||||
{
|
||||
// ...
|
||||
|
||||
[SetUp]
|
||||
|
@ -198,6 +200,7 @@ These tests assume that repository exists. You probably want to create a new one
|
|||
var resp = await Request.DeleteAsync("/repos/" + USER + "/" + REPO);
|
||||
Assert.True(resp.Ok);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Complete test example
|
||||
|
@ -337,6 +340,8 @@ The following test creates a new issue via API and then navigates to the list of
|
|||
project to check that it appears at the top of the list. The check is performed using [LocatorAssertions].
|
||||
|
||||
```csharp
|
||||
class TestGitHubAPI : PageTest
|
||||
{
|
||||
[Test]
|
||||
public async Task LastCreatedIssueShouldBeFirstInTheList()
|
||||
{
|
||||
|
@ -352,6 +357,7 @@ project to check that it appears at the top of the list. The check is performed
|
|||
var firstIssue = Page.Locator("a[data-hovercard-type='issue']").First;
|
||||
await Expect(firstIssue).ToHaveTextAsync("[Feature] request 1");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Check the server state after running user actions
|
||||
|
@ -360,6 +366,8 @@ The following test creates a new issue via user interface in the browser and the
|
|||
it was created:
|
||||
|
||||
```csharp
|
||||
class GitHubTests : PageTest
|
||||
{
|
||||
[Test]
|
||||
public async Task LastCreatedIssueShouldBeOnTheServer()
|
||||
{
|
||||
|
@ -374,6 +382,7 @@ it was created:
|
|||
Assert.True(newIssue.Ok);
|
||||
StringAssert.Contains(await newIssue.TextAsync(), "Bug report 1");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Reuse authentication state
|
||||
|
|
|
@ -379,12 +379,12 @@ api_request_context.get("https://example.com/api/getText", params=query_params)
|
|||
```
|
||||
|
||||
```csharp
|
||||
var params = new Dictionary<string, object>()
|
||||
var queryParams = new Dictionary<string, object>()
|
||||
{
|
||||
{ "isbn", "1234" },
|
||||
{ "page", 23 },
|
||||
}
|
||||
await request.GetAsync("https://example.com/api/getText", new() { Params = params });
|
||||
};
|
||||
await request.GetAsync("https://example.com/api/getText", new() { Params = queryParams });
|
||||
```
|
||||
|
||||
### param: APIRequestContext.get.url = %%-fetch-param-url-%%
|
||||
|
|
|
@ -251,7 +251,7 @@ browser = playwright.chromium.launch( # or "firefox" or "webkit".
|
|||
```csharp
|
||||
var browser = await playwright.Chromium.LaunchAsync(new() {
|
||||
IgnoreDefaultArgs = new[] { "--mute-audio" }
|
||||
})
|
||||
});
|
||||
```
|
||||
|
||||
> **Chromium-only** Playwright can also be used to control the Google Chrome or Microsoft Edge browsers, but it works best with the version of
|
||||
|
|
|
@ -44,7 +44,7 @@ for (Locator li : page.getByRole('listitem').all())
|
|||
```
|
||||
|
||||
```csharp
|
||||
foreach (var li in await page.GetByRole('listitem').AllAsync())
|
||||
foreach (var li in await page.GetByRole("listitem").AllAsync())
|
||||
await li.ClickAsync();
|
||||
```
|
||||
|
||||
|
@ -1298,7 +1298,7 @@ checked = page.get_by_role("checkbox").is_checked()
|
|||
```
|
||||
|
||||
```csharp
|
||||
Boolean checked = await page.GetByRole(AriaRole.Checkbox).IsCheckedAsync();
|
||||
var isChecked = await page.GetByRole(AriaRole.Checkbox).IsCheckedAsync();
|
||||
```
|
||||
|
||||
### option: Locator.isChecked.timeout = %%-input-timeout-%%
|
||||
|
|
|
@ -1780,7 +1780,7 @@ expect(locator).to_have_values([re.compile(r"R"), re.compile(r"G")])
|
|||
|
||||
```csharp
|
||||
var locator = Page.Locator("id=favorite-colors");
|
||||
await locator.SelectOptionAsync(new string[] { "R", "G" })
|
||||
await locator.SelectOptionAsync(new string[] { "R", "G" });
|
||||
await Expect(locator).ToHaveValuesAsync(new Regex[] { new Regex("R"), new Regex("G") });
|
||||
```
|
||||
|
||||
|
|
|
@ -1782,7 +1782,7 @@ class PageExamples
|
|||
using var playwright = await Playwright.CreateAsync();
|
||||
await using var browser = await playwright.Webkit.LaunchAsync(new()
|
||||
{
|
||||
Headless: false
|
||||
Headless = false,
|
||||
});
|
||||
var page = await browser.NewPageAsync();
|
||||
|
||||
|
@ -2046,7 +2046,7 @@ class PageExamples
|
|||
using var playwright = await Playwright.CreateAsync();
|
||||
await using var browser = await playwright.Webkit.LaunchAsync(new()
|
||||
{
|
||||
Headless: false
|
||||
Headless = false
|
||||
});
|
||||
var page = await browser.NewPageAsync();
|
||||
|
||||
|
|
|
@ -591,8 +591,8 @@ await page.RouteAsync("**/*", route => route.FulfillAsync(new ()
|
|||
{
|
||||
Status = 404,
|
||||
ContentType = "text/plain",
|
||||
Body = "Not Found!")
|
||||
});
|
||||
Body = "Not Found!"
|
||||
}));
|
||||
```
|
||||
|
||||
An example of serving static file:
|
||||
|
|
|
@ -49,14 +49,14 @@ await using var browser = playwright.Chromium.LaunchAsync();
|
|||
await using var context = await browser.NewContextAsync();
|
||||
await context.Tracing.StartAsync(new()
|
||||
{
|
||||
Screenshots: true,
|
||||
Snapshots: true
|
||||
Screenshots = true,
|
||||
Snapshots = true
|
||||
});
|
||||
var page = context.NewPageAsync();
|
||||
await page.GotoAsync("https://playwright.dev");
|
||||
await context.Tracing.StopAsync(new()
|
||||
{
|
||||
Path: "trace.zip"
|
||||
Path = "trace.zip"
|
||||
});
|
||||
```
|
||||
|
||||
|
@ -103,14 +103,14 @@ await using var browser = playwright.Chromium.LaunchAsync();
|
|||
await using var context = await browser.NewContextAsync();
|
||||
await context.Tracing.StartAsync(new()
|
||||
{
|
||||
Screenshots: true,
|
||||
Snapshots: true
|
||||
Screenshots = true,
|
||||
Snapshots = true
|
||||
});
|
||||
var page = context.NewPageAsync();
|
||||
await page.GotoAsync("https://playwright.dev");
|
||||
await context.Tracing.StopAsync(new()
|
||||
{
|
||||
Path: "trace.zip"
|
||||
Path = "trace.zip"
|
||||
});
|
||||
```
|
||||
|
||||
|
@ -238,8 +238,8 @@ await using var browser = playwright.Chromium.LaunchAsync();
|
|||
await using var context = await browser.NewContextAsync();
|
||||
await context.Tracing.StartAsync(new()
|
||||
{
|
||||
Screenshots: true,
|
||||
Snapshots: true
|
||||
Screenshots = true,
|
||||
Snapshots = true
|
||||
});
|
||||
var page = context.NewPageAsync();
|
||||
await page.GotoAsync("https://playwright.dev");
|
||||
|
@ -249,7 +249,7 @@ await page.GetByText("Get Started").ClickAsync();
|
|||
// Everything between StartChunkAsync and StopChunkAsync will be recorded in the trace.
|
||||
await context.Tracing.StopChunkAsync(new()
|
||||
{
|
||||
Path: "trace1.zip"
|
||||
Path = "trace1.zip"
|
||||
});
|
||||
|
||||
await context.Tracing.StartChunkAsync();
|
||||
|
@ -257,7 +257,7 @@ await page.GotoAsync("http://example.com");
|
|||
// Save a second trace file with different actions.
|
||||
await context.Tracing.StopChunkAsync(new()
|
||||
{
|
||||
Path: "trace2.zip"
|
||||
Path = "trace2.zip"
|
||||
});
|
||||
```
|
||||
|
||||
|
|
|
@ -1395,19 +1395,19 @@ page.getByText(Pattern.compile("^hello$", Pattern.CASE_INSENSITIVE))
|
|||
|
||||
```csharp
|
||||
// Matches <span>
|
||||
page.GetByText("world")
|
||||
page.GetByText("world");
|
||||
|
||||
// Matches first <div>
|
||||
page.GetByText("Hello world")
|
||||
page.GetByText("Hello world");
|
||||
|
||||
// Matches second <div>
|
||||
page.GetByText("Hello", new() { Exact: true })
|
||||
page.GetByText("Hello", new() { Exact = true });
|
||||
|
||||
// Matches both <div>s
|
||||
page.GetByText(new Regex("Hello"))
|
||||
page.GetByText(new Regex("Hello"));
|
||||
|
||||
// Matches second <div>
|
||||
page.GetByText(new Regex("^hello$", RegexOptions.IgnoreCase))
|
||||
page.GetByText(new Regex("^hello$", RegexOptions.IgnoreCase));
|
||||
```
|
||||
|
||||
**Details**
|
||||
|
|
|
@ -395,7 +395,7 @@ pytest test_login.py --browser-channel msedge
|
|||
</RunSettings>
|
||||
```
|
||||
|
||||
```csharp
|
||||
```bash csharp
|
||||
dotnet test -- Playwright.BrowserName=chromium Playwright.LaunchOptions.Channel=msedge
|
||||
```
|
||||
|
||||
|
|
|
@ -76,19 +76,13 @@ with sync_playwright() as playwright:
|
|||
using Microsoft.Playwright;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
await using var browser = await playwright.Chromium.LaunchAsync(new()
|
||||
{
|
||||
public static async Task Main()
|
||||
{
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
await using var browser = await playwright.Chromium.LaunchAsync(new()
|
||||
{
|
||||
Headless: False
|
||||
});
|
||||
var iphone13 = playwright.Devices["iPhone 13"];
|
||||
await using var context = await browser.NewContextAsync(iphone13);
|
||||
}
|
||||
}
|
||||
Headless = false
|
||||
});
|
||||
var iphone13 = playwright.Devices["iPhone 13"];
|
||||
await using var context = await browser.NewContextAsync(iphone13);
|
||||
```
|
||||
|
||||
|
||||
|
|
|
@ -192,7 +192,7 @@ page.get_by_label('Choose multiple colors').select_option(['red', 'green', 'blue
|
|||
await page.GetByLabel("Choose a color").SelectOptionAsync("blue");
|
||||
|
||||
// Single selection matching the label
|
||||
await page.GetByLabel("Choose a color").SelectOptionAsync(new SelectOptionValue { Label = "blue" }));
|
||||
await page.GetByLabel("Choose a color").SelectOptionAsync(new SelectOptionValue { Label = "blue" });
|
||||
|
||||
// Multiple selected items
|
||||
await page.GetByLabel("Choose multiple colors").SelectOptionAsync(new[] { "blue", "green", "red" });
|
||||
|
|
|
@ -141,7 +141,7 @@ locator.click()
|
|||
```
|
||||
|
||||
```csharp
|
||||
var locator = page.GetByRole(AriaRole.Button, new() { Name = "Sign in" })
|
||||
var locator = page.GetByRole(AriaRole.Button, new() { Name = "Sign in" });
|
||||
|
||||
await locator.HoverAsync();
|
||||
await locator.ClickAsync();
|
||||
|
@ -180,7 +180,7 @@ locator.click()
|
|||
```csharp
|
||||
var locator = page
|
||||
.FrameLocator("#my-frame")
|
||||
.GetByRole(AriaRole.Button), new() { Name = "Sign in" });
|
||||
.GetByRole(AriaRole.Button, new() { Name = "Sign in" });
|
||||
|
||||
await locator.ClickAsync();
|
||||
```
|
||||
|
@ -644,11 +644,11 @@ page.locator("//button").click()
|
|||
```
|
||||
|
||||
```csharp
|
||||
await page.Locator('css=button').ClickAsync();
|
||||
await page.Locator('xpath=//button').ClickAsync();
|
||||
await page.Locator("css=button").ClickAsync();
|
||||
await page.Locator("xpath=//button").ClickAsync();
|
||||
|
||||
await page.Locator('button').ClickAsync();
|
||||
await page.Locator('//button').ClickAsync();
|
||||
await page.Locator("button").ClickAsync();
|
||||
await page.Locator("//button").ClickAsync();
|
||||
```
|
||||
|
||||
XPath and CSS selectors can be tied to the DOM structure or implementation. These selectors can break when the DOM structure changes. Long CSS or XPath chains below are an example of a **bad practice** that leads to unstable tests:
|
||||
|
@ -1010,8 +1010,8 @@ await Expect(page
|
|||
.GetByRole(AriaRole.Listitem)
|
||||
.Filter(new() {
|
||||
Has = page.GetByRole(AriaRole.Heading, new() { Name = "Product 2" })
|
||||
})
|
||||
.toHaveCountAsync(1);
|
||||
}))
|
||||
.ToHaveCountAsync(1);
|
||||
```
|
||||
|
||||
### Filter by not having child/descendant
|
||||
|
@ -1053,8 +1053,8 @@ await Expect(page
|
|||
.GetByRole(AriaRole.Listitem)
|
||||
.Filter(new() {
|
||||
HasNot = page.GetByRole(AriaRole.Heading, new() { Name = "Product 2" })
|
||||
})
|
||||
.toHaveCountAsync(1);
|
||||
}))
|
||||
.ToHaveCountAsync(1);
|
||||
```
|
||||
|
||||
Note that the inner locator is matched starting from the outer one, not from the document root.
|
||||
|
@ -1104,7 +1104,7 @@ var product = page
|
|||
.Filter(new() { HasText = "Product 2" });
|
||||
|
||||
await product
|
||||
.GetByRole(AriaRole.Button), new() { Name = "Add to cart" })
|
||||
.GetByRole(AriaRole.Button, new() { Name = "Add to cart" })
|
||||
.ClickAsync();
|
||||
```
|
||||
|
||||
|
@ -1574,7 +1574,7 @@ var rowLocator = page.GetByRole(AriaRole.Listitem);
|
|||
await rowLocator
|
||||
.Filter(new() { HasText = "Mary" })
|
||||
.Filter(new() {
|
||||
Has = page.GetByRole(AriaRole.Button), new() { Name = "Say goodbye" })
|
||||
Has = page.GetByRole(AriaRole.Button, new() { Name = "Say goodbye" })
|
||||
})
|
||||
.ScreenshotAsync(new() { Path = "screenshot.png" });
|
||||
```
|
||||
|
|
|
@ -41,8 +41,14 @@ page.route("https://dog.ceo/api/breeds/list/all", handle)
|
|||
```csharp
|
||||
await page.RouteAsync("https://dog.ceo/api/breeds/list/all", async route =>
|
||||
{
|
||||
var json = /* JsonElement with the test payload */;
|
||||
await route.FulfillAsync(new () { Json: json });
|
||||
var json = new
|
||||
{
|
||||
message = new
|
||||
{
|
||||
test_breed = new string[] { }
|
||||
}
|
||||
};
|
||||
await route.FulfillAsync(new () { Json = json });
|
||||
});
|
||||
```
|
||||
|
||||
|
|
|
@ -224,9 +224,9 @@ await using var browser = await BrowserType.LaunchAsync(new()
|
|||
// Browser proxy option is required for Chromium on Windows.
|
||||
Proxy = proxy
|
||||
});
|
||||
using var context = await Browser.NewContextAsync(new()
|
||||
await using var context = await browser.NewContextAsync(new()
|
||||
{
|
||||
Proxy = new Proxy { Server = "http://myproxy.com:3128" })
|
||||
Proxy = new Proxy { Server = "http://myproxy.com:3128" },
|
||||
});
|
||||
```
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче