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
|
||||
using Microsoft.Playwright;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
public static async Task Main()
|
||||
{
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
var firefox = playwright.Firefox;
|
||||
var browser = await firefox.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });
|
||||
var page = await browser.NewPageAsync();
|
||||
await page.GotoAsync("https://www.bing.com");
|
||||
await browser.CloseAsync();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## event: Browser.disconnected
|
||||
|
@ -493,12 +493,7 @@ with sync_playwright() as playwright:
|
||||
|
||||
```csharp
|
||||
using Microsoft.Playwright;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
public static async Task Main()
|
||||
{
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
var browser = await playwright.Webkit.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });
|
||||
var context = await browser.NewContextAsync();
|
||||
@ -513,8 +508,6 @@ class Program
|
||||
"<button onclick=\"onClick()\">Click me</button>\n" +
|
||||
"<div></div>");
|
||||
await page.ClickAsync("button");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
An example of passing an element handle:
|
||||
|
@ -51,8 +51,8 @@ using System.Threading.Tasks;
|
||||
using Microsoft.Playwright.NUnit;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace PlaywrightTests
|
||||
{
|
||||
namespace PlaywrightTests;
|
||||
|
||||
public class ExampleTests : PageTest
|
||||
{
|
||||
[Test]
|
||||
@ -63,7 +63,6 @@ namespace PlaywrightTests
|
||||
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## property: LocatorAssertions.not
|
||||
|
@ -53,8 +53,8 @@ using System.Threading.Tasks;
|
||||
using Microsoft.Playwright.NUnit;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace PlaywrightTests
|
||||
{
|
||||
namespace PlaywrightTests;
|
||||
|
||||
public class ExampleTests : PageTest
|
||||
{
|
||||
[Test]
|
||||
@ -65,7 +65,6 @@ namespace PlaywrightTests
|
||||
await Expect(Page.Locator("div#foobar")).ToHaveURL(new Regex(".*/login"));
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## property: PageAssertions.not
|
||||
|
@ -52,8 +52,8 @@ using System.Threading.Tasks;
|
||||
using Microsoft.Playwright.NUnit;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Playwright.TestingHarnessTest.NUnit
|
||||
{
|
||||
namespace Playwright.TestingHarnessTest.NUnit;
|
||||
|
||||
public class ExampleTests : PageTest
|
||||
{
|
||||
[Test]
|
||||
@ -62,7 +62,6 @@ namespace Playwright.TestingHarnessTest.NUnit
|
||||
await Expect(Page.Locator(".status")).ToHaveTextAsync("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
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Playwright;
|
||||
using System;
|
||||
|
||||
class Program
|
||||
{
|
||||
public static async Task Main()
|
||||
{
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
await using var browser = await playwright.Chromium.LaunchAsync();
|
||||
var page = await browser.NewPageAsync();
|
||||
@ -97,6 +91,4 @@ class Program
|
||||
{
|
||||
Console.WriteLine("Timeout!");
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -90,18 +90,11 @@ browser = playwright.chromium.launch(channel="chrome")
|
||||
|
||||
```csharp
|
||||
using Microsoft.Playwright;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
public static async Task Main()
|
||||
{
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
var chromium = playwright.Chromium;
|
||||
// Can be "msedge", "chrome-beta", "msedge-beta", "msedge-dev", etc.
|
||||
var browser = await chromium.LaunchAsync(new BrowserTypeLaunchOptions { Channel = "chrome" });
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### When to use Google Chrome & Microsoft Edge and when not to?
|
||||
|
@ -313,19 +313,12 @@ with sync_playwright() as p:
|
||||
|
||||
```csharp
|
||||
using Microsoft.Playwright;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
public static async Task Main()
|
||||
{
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
|
||||
{
|
||||
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.
|
||||
|
@ -255,12 +255,7 @@ with sync_playwright() as p:
|
||||
|
||||
```csharp
|
||||
using Microsoft.Playwright;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
public static async Task Main()
|
||||
{
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
var chromium = playwright.Chromium;
|
||||
// Make sure to run headed.
|
||||
@ -273,8 +268,6 @@ class Program
|
||||
// Pause the page, and start recording manually.
|
||||
var page = await context.NewPageAsync();
|
||||
await page.PauseAsync();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Open pages
|
||||
|
@ -163,12 +163,7 @@ with sync_playwright() as p:
|
||||
|
||||
```csharp
|
||||
using Microsoft.Playwright;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
public static async Task Main()
|
||||
{
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
var chromium = playwright.Chromium;
|
||||
// Make sure to run headed.
|
||||
@ -181,8 +176,6 @@ class Program
|
||||
// Pause the page, and start recording manually.
|
||||
var page = await context.NewPageAsync();
|
||||
await page.PauseAsync();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Emulate devices
|
||||
|
@ -30,19 +30,12 @@ Create a `Program.cs` that will navigate to `https://playwright.dev/dotnet` and
|
||||
|
||||
```csharp
|
||||
using Microsoft.Playwright;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class Program
|
||||
{
|
||||
public static async Task Main()
|
||||
{
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
await using var browser = await playwright.Chromium.LaunchAsync();
|
||||
var page = await browser.NewPageAsync();
|
||||
await page.GotoAsync("https://playwright.dev/dotnet");
|
||||
await page.ScreenshotAsync(new PageScreenshotOptions { Path = "screenshot.png" });
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Now run it.
|
||||
@ -78,7 +71,7 @@ Install dependencies, build project and download necessary browsers. This is onl
|
||||
dotnet add package Microsoft.Playwright.NUnit
|
||||
# Build the project
|
||||
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
|
||||
```
|
||||
|
||||
@ -88,8 +81,8 @@ using System.Threading.Tasks;
|
||||
using Microsoft.Playwright.NUnit;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace PlaywrightTests
|
||||
{
|
||||
namespace PlaywrightTests;
|
||||
|
||||
[Parallelizable(ParallelScope.Self)]
|
||||
public class Tests : PageTest
|
||||
{
|
||||
@ -107,7 +100,6 @@ namespace PlaywrightTests
|
||||
Assert.AreEqual(21, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```bash
|
||||
|
@ -244,20 +244,13 @@ with sync_playwright() as playwright:
|
||||
|
||||
```csharp
|
||||
using Microsoft.Playwright;
|
||||
using System;
|
||||
|
||||
class Program
|
||||
{
|
||||
public static async Task Main()
|
||||
{
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
await using var browser = await playwright.Chromium.LaunchAsync();
|
||||
var page = await browser.NewPageAsync();
|
||||
page.Request += (_, request) => Console.WriteLine(">> " + request.Method + " " + request.Url);
|
||||
page.Response += (_, response) => Console.WriteLine("<< " + response.Status + " " + response.Url);
|
||||
await page.GotoAsync("https://example.com");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Or wait for a network response after the button click:
|
||||
|
@ -103,8 +103,8 @@ class SearchPage:
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Playwright;
|
||||
|
||||
namespace BigEcommerceApp.Tests.Models
|
||||
{
|
||||
namespace BigEcommerceApp.Tests.Models;
|
||||
|
||||
public class SearchPage
|
||||
{
|
||||
private readonly IPage _page;
|
||||
@ -127,7 +127,6 @@ namespace BigEcommerceApp.Tests.Models
|
||||
await _searchTermInput.PressAsync("Enter");
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Page objects can then be used inside a test.
|
||||
|
@ -52,8 +52,8 @@ using System.Threading.Tasks;
|
||||
using Microsoft.Playwright.NUnit;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Playwright.TestingHarnessTest.NUnit
|
||||
{
|
||||
namespace Playwright.TestingHarnessTest.NUnit;
|
||||
|
||||
public class ExampleTests : PageTest
|
||||
{
|
||||
[Test]
|
||||
@ -62,7 +62,6 @@ namespace Playwright.TestingHarnessTest.NUnit
|
||||
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
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 NUnit.Framework;
|
||||
|
||||
namespace PlaywrightTests
|
||||
{
|
||||
namespace PlaywrightTests;
|
||||
|
||||
[Parallelizable(ParallelScope.Self)]
|
||||
public class MyTest : PageTest
|
||||
{
|
||||
@ -53,7 +53,6 @@ namespace PlaywrightTests
|
||||
Assert.AreEqual(21, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Run your tests against Chromium
|
||||
|
Loading…
x
Reference in New Issue
Block a user