docs(dotnet): migrate to top-level style code snippets (#13559)

This commit is contained in:
Guriy Samarin 2022-04-19 21:23:26 +03:00 committed by GitHub
parent 0a401b2d86
commit cde7c5df44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 147 additions and 218 deletions

View File

@ -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();
}
}
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

View File

@ -493,28 +493,21 @@ 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();
using var playwright = await Playwright.CreateAsync();
var browser = await playwright.Webkit.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });
var context = await browser.NewContextAsync();
await context.ExposeBindingAsync("pageURL", source => source.Page.Url);
var page = await context.NewPageAsync();
await page.SetContentAsync("<script>\n" +
" async function onClick() {\n" +
" document.querySelector('div').textContent = await window.pageURL();\n" +
" }\n" +
"</script>\n" +
"<button onclick=\"onClick()\">Click me</button>\n" +
"<div></div>");
await page.ClickAsync("button");
}
}
await context.ExposeBindingAsync("pageURL", source => source.Page.Url);
var page = await context.NewPageAsync();
await page.SetContentAsync("<script>\n" +
" async function onClick() {\n" +
" document.querySelector('div').textContent = await window.pageURL();\n" +
" }\n" +
"</script>\n" +
"<button onclick=\"onClick()\">Click me</button>\n" +
"<div></div>");
await page.ClickAsync("button");
```
An example of passing an element handle:

View File

@ -51,17 +51,16 @@ using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace PlaywrightTests
namespace PlaywrightTests;
public class ExampleTests : PageTest
{
public class ExampleTests : PageTest
[Test]
public async Task StatusBecomesSubmitted()
{
[Test]
public async Task StatusBecomesSubmitted()
{
// ..
await Page.ClickAsync("#submit-button");
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
}
// ..
await Page.ClickAsync("#submit-button");
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
}
}
```

View File

@ -53,17 +53,16 @@ using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace PlaywrightTests
namespace PlaywrightTests;
public class ExampleTests : PageTest
{
public class ExampleTests : PageTest
[Test]
public async Task NavigatetoLoginPage()
{
[Test]
public async Task NavigatetoLoginPage()
{
// ..
await Page.ClickAsync("#login");
await Expect(Page.Locator("div#foobar")).ToHaveURL(new Regex(".*/login"));
}
// ..
await Page.ClickAsync("#login");
await Expect(Page.Locator("div#foobar")).ToHaveURL(new Regex(".*/login"));
}
}
```

View File

@ -52,15 +52,14 @@ using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace Playwright.TestingHarnessTest.NUnit
namespace Playwright.TestingHarnessTest.NUnit;
public class ExampleTests : PageTest
{
public class ExampleTests : PageTest
[Test]
public async Task StatusBecomesSubmitted()
{
[Test]
public async Task StatusBecomesSubmitted()
{
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
}
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
}
}
```

View File

@ -78,25 +78,17 @@ public class TimeoutErrorExample {
```
```csharp
using System.Threading.Tasks;
using Microsoft.Playwright;
using System;
class Program
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();
try
{
public static async Task Main()
{
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();
try
{
await page.ClickAsync("text=Example", new() { Timeout = 100 });
}
catch (TimeoutException)
{
Console.WriteLine("Timeout!");
}
}
await page.ClickAsync("text=Example", new() { Timeout = 100 });
}
catch (TimeoutException)
{
Console.WriteLine("Timeout!");
}
```

View File

@ -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" });
}
}
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?

View File

@ -313,19 +313,12 @@ with sync_playwright() as p:
```csharp
using Microsoft.Playwright;
using System.Threading.Tasks;
class Program
using var playwright = await Playwright.CreateAsync();
await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
public static async Task Main()
{
using var playwright = await Playwright.CreateAsync();
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.

View File

@ -255,26 +255,19 @@ 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.
var browser = await chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });
using var playwright = await Playwright.CreateAsync();
var chromium = playwright.Chromium;
// Make sure to run headed.
var browser = await chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });
// Setup context however you like.
var context = await browser.NewContextAsync(); // Pass any options
await context.RouteAsync('**/*', route => route.ContinueAsync());
// Setup context however you like.
var context = await browser.NewContextAsync(); // Pass any options
await context.RouteAsync('**/*', route => route.ContinueAsync());
// Pause the page, and start recording manually.
var page = await context.NewPageAsync();
await page.PauseAsync();
}
}
// Pause the page, and start recording manually.
var page = await context.NewPageAsync();
await page.PauseAsync();
```
## Open pages

View File

@ -163,26 +163,19 @@ 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.
var browser = await chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });
using var playwright = await Playwright.CreateAsync();
var chromium = playwright.Chromium;
// Make sure to run headed.
var browser = await chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });
// Setup context however you like.
var context = await browser.NewContextAsync(); // Pass any options
await context.RouteAsync('**/*', route => route.ContinueAsync());
// Setup context however you like.
var context = await browser.NewContextAsync(); // Pass any options
await context.RouteAsync('**/*', route => route.ContinueAsync());
// Pause the page, and start recording manually.
var page = await context.NewPageAsync();
await page.PauseAsync();
}
}
// Pause the page, and start recording manually.
var page = await context.NewPageAsync();
await page.PauseAsync();
```
## Emulate devices

View File

@ -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" });
}
}
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,24 +81,23 @@ using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace PlaywrightTests
{
[Parallelizable(ParallelScope.Self)]
public class Tests : PageTest
{
[Test]
public async Task ShouldAdd()
{
int result = await Page.EvaluateAsync<int>("() => 7 + 3");
Assert.AreEqual(10, result);
}
namespace PlaywrightTests;
[Test]
public async Task ShouldMultiply()
{
int result = await Page.EvaluateAsync<int>("() => 7 * 3");
Assert.AreEqual(21, result);
}
[Parallelizable(ParallelScope.Self)]
public class Tests : PageTest
{
[Test]
public async Task ShouldAdd()
{
int result = await Page.EvaluateAsync<int>("() => 7 + 3");
Assert.AreEqual(10, result);
}
[Test]
public async Task ShouldMultiply()
{
int result = await Page.EvaluateAsync<int>("() => 7 * 3");
Assert.AreEqual(21, result);
}
}
```

View File

@ -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");
}
}
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:

View File

@ -103,29 +103,28 @@ class SearchPage:
using System.Threading.Tasks;
using Microsoft.Playwright;
namespace BigEcommerceApp.Tests.Models
namespace BigEcommerceApp.Tests.Models;
public class SearchPage
{
public class SearchPage
private readonly IPage _page;
private readonly ILocator _searchTermInput;
public SearchPage(IPage page)
{
private readonly IPage _page;
private readonly ILocator _searchTermInput;
_page = page;
_searchTermInput = page.Locator("[aria-label='Enter your search term']");
}
public SearchPage(IPage page)
{
_page = page;
_searchTermInput = page.Locator("[aria-label='Enter your search term']");
}
public async Task Goto()
{
await _page.GotoAsync("https://bing.com");
}
public async Task Goto()
{
await _page.GotoAsync("https://bing.com");
}
public async Task Search(string text)
{
await _searchTermInput.FillAsync(text);
await _searchTermInput.PressAsync("Enter");
}
public async Task Search(string text)
{
await _searchTermInput.FillAsync(text);
await _searchTermInput.PressAsync("Enter");
}
}
```

View File

@ -52,15 +52,14 @@ using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace Playwright.TestingHarnessTest.NUnit
namespace Playwright.TestingHarnessTest.NUnit;
public class ExampleTests : PageTest
{
public class ExampleTests : PageTest
[Test]
public async Task StatusBecomesSubmitted()
{
[Test]
public async Task StatusBecomesSubmitted()
{
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
}
await Expect(Page.Locator(".status")).ToHaveTextAsync("Submitted");
}
}
```

View File

@ -34,24 +34,23 @@ using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace PlaywrightTests
{
[Parallelizable(ParallelScope.Self)]
public class MyTest : PageTest
{
[Test]
public async Task ShouldAdd()
{
int result = await Page.EvaluateAsync<int>("() => 7 + 3");
Assert.AreEqual(10, result);
}
namespace PlaywrightTests;
[Test]
public async Task ShouldMultiply()
{
int result = await Page.EvaluateAsync<int>("() => 7 * 3");
Assert.AreEqual(21, result);
}
[Parallelizable(ParallelScope.Self)]
public class MyTest : PageTest
{
[Test]
public async Task ShouldAdd()
{
int result = await Page.EvaluateAsync<int>("() => 7 + 3");
Assert.AreEqual(10, result);
}
[Test]
public async Task ShouldMultiply()
{
int result = await Page.EvaluateAsync<int>("() => 7 * 3");
Assert.AreEqual(21, result);
}
}
```
@ -150,4 +149,4 @@ There are few base classes available to you in Microsoft.Playwright.NUnit namesp
## xUnit support
While using xUnit is also supported, we do not support running parallel tests. This is a well known problem/design limitation
outlined by the maintainers across [several](https://github.com/xunit/xunit/issues/2003) [issues](https://github.com/xunit/xunit/issues/2111#issuecomment-650004247).
outlined by the maintainers across [several](https://github.com/xunit/xunit/issues/2003) [issues](https://github.com/xunit/xunit/issues/2111#issuecomment-650004247).