mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
docs(dotnet): migrate to top-level style code snippets (#13559)
This commit is contained in:
parent
0a401b2d86
commit
cde7c5df44
@ -63,20 +63,13 @@ with sync_playwright() as playwright:
|
|||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
class Program
|
|
||||||
{
|
|
||||||
public static async Task Main()
|
|
||||||
{
|
|
||||||
using var playwright = await Playwright.CreateAsync();
|
using var playwright = await Playwright.CreateAsync();
|
||||||
var firefox = playwright.Firefox;
|
var firefox = playwright.Firefox;
|
||||||
var browser = await firefox.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });
|
var browser = await firefox.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });
|
||||||
var page = await browser.NewPageAsync();
|
var page = await browser.NewPageAsync();
|
||||||
await page.GotoAsync("https://www.bing.com");
|
await page.GotoAsync("https://www.bing.com");
|
||||||
await browser.CloseAsync();
|
await browser.CloseAsync();
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## event: Browser.disconnected
|
## event: Browser.disconnected
|
||||||
|
@ -493,12 +493,7 @@ with sync_playwright() as playwright:
|
|||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
class Program
|
|
||||||
{
|
|
||||||
public static async Task Main()
|
|
||||||
{
|
|
||||||
using var playwright = await Playwright.CreateAsync();
|
using var playwright = await Playwright.CreateAsync();
|
||||||
var browser = await playwright.Webkit.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });
|
var browser = await playwright.Webkit.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });
|
||||||
var context = await browser.NewContextAsync();
|
var context = await browser.NewContextAsync();
|
||||||
@ -513,8 +508,6 @@ class Program
|
|||||||
"<button onclick=\"onClick()\">Click me</button>\n" +
|
"<button onclick=\"onClick()\">Click me</button>\n" +
|
||||||
"<div></div>");
|
"<div></div>");
|
||||||
await page.ClickAsync("button");
|
await page.ClickAsync("button");
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
An example of passing an element handle:
|
An example of passing an element handle:
|
||||||
|
@ -51,8 +51,8 @@ using System.Threading.Tasks;
|
|||||||
using Microsoft.Playwright.NUnit;
|
using Microsoft.Playwright.NUnit;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
namespace PlaywrightTests
|
namespace PlaywrightTests;
|
||||||
{
|
|
||||||
public class ExampleTests : PageTest
|
public class ExampleTests : PageTest
|
||||||
{
|
{
|
||||||
[Test]
|
[Test]
|
||||||
@ -63,7 +63,6 @@ namespace PlaywrightTests
|
|||||||
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
|
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## property: LocatorAssertions.not
|
## property: LocatorAssertions.not
|
||||||
|
@ -53,8 +53,8 @@ using System.Threading.Tasks;
|
|||||||
using Microsoft.Playwright.NUnit;
|
using Microsoft.Playwright.NUnit;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
namespace PlaywrightTests
|
namespace PlaywrightTests;
|
||||||
{
|
|
||||||
public class ExampleTests : PageTest
|
public class ExampleTests : PageTest
|
||||||
{
|
{
|
||||||
[Test]
|
[Test]
|
||||||
@ -65,7 +65,6 @@ namespace PlaywrightTests
|
|||||||
await Expect(Page.Locator("div#foobar")).ToHaveURL(new Regex(".*/login"));
|
await Expect(Page.Locator("div#foobar")).ToHaveURL(new Regex(".*/login"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## property: PageAssertions.not
|
## property: PageAssertions.not
|
||||||
|
@ -52,8 +52,8 @@ using System.Threading.Tasks;
|
|||||||
using Microsoft.Playwright.NUnit;
|
using Microsoft.Playwright.NUnit;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
namespace Playwright.TestingHarnessTest.NUnit
|
namespace Playwright.TestingHarnessTest.NUnit;
|
||||||
{
|
|
||||||
public class ExampleTests : PageTest
|
public class ExampleTests : PageTest
|
||||||
{
|
{
|
||||||
[Test]
|
[Test]
|
||||||
@ -62,7 +62,6 @@ namespace Playwright.TestingHarnessTest.NUnit
|
|||||||
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
|
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Playwright will be re-testing the node with the selector `.status` until fetched Node has the `"Submitted"`
|
Playwright will be re-testing the node with the selector `.status` until fetched Node has the `"Submitted"`
|
||||||
|
@ -78,14 +78,8 @@ public class TimeoutErrorExample {
|
|||||||
```
|
```
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System;
|
|
||||||
|
|
||||||
class Program
|
|
||||||
{
|
|
||||||
public static async Task Main()
|
|
||||||
{
|
|
||||||
using var playwright = await Playwright.CreateAsync();
|
using var playwright = await Playwright.CreateAsync();
|
||||||
await using var browser = await playwright.Chromium.LaunchAsync();
|
await using var browser = await playwright.Chromium.LaunchAsync();
|
||||||
var page = await browser.NewPageAsync();
|
var page = await browser.NewPageAsync();
|
||||||
@ -97,6 +91,4 @@ class Program
|
|||||||
{
|
{
|
||||||
Console.WriteLine("Timeout!");
|
Console.WriteLine("Timeout!");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
@ -90,18 +90,11 @@ browser = playwright.chromium.launch(channel="chrome")
|
|||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
class Program
|
|
||||||
{
|
|
||||||
public static async Task Main()
|
|
||||||
{
|
|
||||||
using var playwright = await Playwright.CreateAsync();
|
using var playwright = await Playwright.CreateAsync();
|
||||||
var chromium = playwright.Chromium;
|
var chromium = playwright.Chromium;
|
||||||
// Can be "msedge", "chrome-beta", "msedge-beta", "msedge-dev", etc.
|
// Can be "msedge", "chrome-beta", "msedge-beta", "msedge-dev", etc.
|
||||||
var browser = await chromium.LaunchAsync(new BrowserTypeLaunchOptions { Channel = "chrome" });
|
var browser = await chromium.LaunchAsync(new BrowserTypeLaunchOptions { Channel = "chrome" });
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### When to use Google Chrome & Microsoft Edge and when not to?
|
### When to use Google Chrome & Microsoft Edge and when not to?
|
||||||
|
@ -313,19 +313,12 @@ with sync_playwright() as p:
|
|||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
class Program
|
|
||||||
{
|
|
||||||
public static async Task Main()
|
|
||||||
{
|
|
||||||
using var playwright = await Playwright.CreateAsync();
|
using var playwright = await Playwright.CreateAsync();
|
||||||
await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
|
await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
|
||||||
{
|
{
|
||||||
Headless = false
|
Headless = false
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
On Linux agents, headed execution requires [Xvfb](https://en.wikipedia.org/wiki/Xvfb) to be installed. Our [Docker image](./docker.md) and GitHub Action have Xvfb pre-installed. To run browsers in headed mode with Xvfb, add `xvfb-run` before the Node.js command.
|
On Linux agents, headed execution requires [Xvfb](https://en.wikipedia.org/wiki/Xvfb) to be installed. Our [Docker image](./docker.md) and GitHub Action have Xvfb pre-installed. To run browsers in headed mode with Xvfb, add `xvfb-run` before the Node.js command.
|
||||||
|
@ -255,12 +255,7 @@ with sync_playwright() as p:
|
|||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
class Program
|
|
||||||
{
|
|
||||||
public static async Task Main()
|
|
||||||
{
|
|
||||||
using var playwright = await Playwright.CreateAsync();
|
using var playwright = await Playwright.CreateAsync();
|
||||||
var chromium = playwright.Chromium;
|
var chromium = playwright.Chromium;
|
||||||
// Make sure to run headed.
|
// Make sure to run headed.
|
||||||
@ -273,8 +268,6 @@ class Program
|
|||||||
// Pause the page, and start recording manually.
|
// Pause the page, and start recording manually.
|
||||||
var page = await context.NewPageAsync();
|
var page = await context.NewPageAsync();
|
||||||
await page.PauseAsync();
|
await page.PauseAsync();
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Open pages
|
## Open pages
|
||||||
|
@ -163,12 +163,7 @@ with sync_playwright() as p:
|
|||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
class Program
|
|
||||||
{
|
|
||||||
public static async Task Main()
|
|
||||||
{
|
|
||||||
using var playwright = await Playwright.CreateAsync();
|
using var playwright = await Playwright.CreateAsync();
|
||||||
var chromium = playwright.Chromium;
|
var chromium = playwright.Chromium;
|
||||||
// Make sure to run headed.
|
// Make sure to run headed.
|
||||||
@ -181,8 +176,6 @@ class Program
|
|||||||
// Pause the page, and start recording manually.
|
// Pause the page, and start recording manually.
|
||||||
var page = await context.NewPageAsync();
|
var page = await context.NewPageAsync();
|
||||||
await page.PauseAsync();
|
await page.PauseAsync();
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Emulate devices
|
## Emulate devices
|
||||||
|
@ -30,19 +30,12 @@ Create a `Program.cs` that will navigate to `https://playwright.dev/dotnet` and
|
|||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
class Program
|
|
||||||
{
|
|
||||||
public static async Task Main()
|
|
||||||
{
|
|
||||||
using var playwright = await Playwright.CreateAsync();
|
using var playwright = await Playwright.CreateAsync();
|
||||||
await using var browser = await playwright.Chromium.LaunchAsync();
|
await using var browser = await playwright.Chromium.LaunchAsync();
|
||||||
var page = await browser.NewPageAsync();
|
var page = await browser.NewPageAsync();
|
||||||
await page.GotoAsync("https://playwright.dev/dotnet");
|
await page.GotoAsync("https://playwright.dev/dotnet");
|
||||||
await page.ScreenshotAsync(new PageScreenshotOptions { Path = "screenshot.png" });
|
await page.ScreenshotAsync(new PageScreenshotOptions { Path = "screenshot.png" });
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Now run it.
|
Now run it.
|
||||||
@ -78,7 +71,7 @@ Install dependencies, build project and download necessary browsers. This is onl
|
|||||||
dotnet add package Microsoft.Playwright.NUnit
|
dotnet add package Microsoft.Playwright.NUnit
|
||||||
# Build the project
|
# Build the project
|
||||||
dotnet build
|
dotnet build
|
||||||
# Install required browsers
|
# Install required browsers - replace netX with actual output folder name, f.ex. net6.0.
|
||||||
pwsh bin\Debug\netX\playwright.ps1 install
|
pwsh bin\Debug\netX\playwright.ps1 install
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -88,8 +81,8 @@ using System.Threading.Tasks;
|
|||||||
using Microsoft.Playwright.NUnit;
|
using Microsoft.Playwright.NUnit;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
namespace PlaywrightTests
|
namespace PlaywrightTests;
|
||||||
{
|
|
||||||
[Parallelizable(ParallelScope.Self)]
|
[Parallelizable(ParallelScope.Self)]
|
||||||
public class Tests : PageTest
|
public class Tests : PageTest
|
||||||
{
|
{
|
||||||
@ -107,7 +100,6 @@ namespace PlaywrightTests
|
|||||||
Assert.AreEqual(21, result);
|
Assert.AreEqual(21, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
@ -244,20 +244,13 @@ with sync_playwright() as playwright:
|
|||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using System;
|
|
||||||
|
|
||||||
class Program
|
|
||||||
{
|
|
||||||
public static async Task Main()
|
|
||||||
{
|
|
||||||
using var playwright = await Playwright.CreateAsync();
|
using var playwright = await Playwright.CreateAsync();
|
||||||
await using var browser = await playwright.Chromium.LaunchAsync();
|
await using var browser = await playwright.Chromium.LaunchAsync();
|
||||||
var page = await browser.NewPageAsync();
|
var page = await browser.NewPageAsync();
|
||||||
page.Request += (_, request) => Console.WriteLine(">> " + request.Method + " " + request.Url);
|
page.Request += (_, request) => Console.WriteLine(">> " + request.Method + " " + request.Url);
|
||||||
page.Response += (_, response) => Console.WriteLine("<< " + response.Status + " " + response.Url);
|
page.Response += (_, response) => Console.WriteLine("<< " + response.Status + " " + response.Url);
|
||||||
await page.GotoAsync("https://example.com");
|
await page.GotoAsync("https://example.com");
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Or wait for a network response after the button click:
|
Or wait for a network response after the button click:
|
||||||
|
@ -103,8 +103,8 @@ class SearchPage:
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
|
|
||||||
namespace BigEcommerceApp.Tests.Models
|
namespace BigEcommerceApp.Tests.Models;
|
||||||
{
|
|
||||||
public class SearchPage
|
public class SearchPage
|
||||||
{
|
{
|
||||||
private readonly IPage _page;
|
private readonly IPage _page;
|
||||||
@ -127,7 +127,6 @@ namespace BigEcommerceApp.Tests.Models
|
|||||||
await _searchTermInput.PressAsync("Enter");
|
await _searchTermInput.PressAsync("Enter");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Page objects can then be used inside a test.
|
Page objects can then be used inside a test.
|
||||||
|
@ -52,8 +52,8 @@ using System.Threading.Tasks;
|
|||||||
using Microsoft.Playwright.NUnit;
|
using Microsoft.Playwright.NUnit;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
namespace Playwright.TestingHarnessTest.NUnit
|
namespace Playwright.TestingHarnessTest.NUnit;
|
||||||
{
|
|
||||||
public class ExampleTests : PageTest
|
public class ExampleTests : PageTest
|
||||||
{
|
{
|
||||||
[Test]
|
[Test]
|
||||||
@ -62,7 +62,6 @@ namespace Playwright.TestingHarnessTest.NUnit
|
|||||||
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
|
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Playwright will be re-testing the node with the selector `.status` until
|
Playwright will be re-testing the node with the selector `.status` until
|
||||||
|
@ -34,8 +34,8 @@ using System.Threading.Tasks;
|
|||||||
using Microsoft.Playwright.NUnit;
|
using Microsoft.Playwright.NUnit;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
namespace PlaywrightTests
|
namespace PlaywrightTests;
|
||||||
{
|
|
||||||
[Parallelizable(ParallelScope.Self)]
|
[Parallelizable(ParallelScope.Self)]
|
||||||
public class MyTest : PageTest
|
public class MyTest : PageTest
|
||||||
{
|
{
|
||||||
@ -53,7 +53,6 @@ namespace PlaywrightTests
|
|||||||
Assert.AreEqual(21, result);
|
Assert.AreEqual(21, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Run your tests against Chromium
|
Run your tests against Chromium
|
||||||
|
Loading…
x
Reference in New Issue
Block a user