Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
docs(dotnet): migrate to top-level style code snippets (#13559)
Guriy Samarincde7c5df4432
diff --git a/docs/src/api/class-browser.md b/docs/src/api/class-browser.mdindex 4b7d3141e8..4549da135e 100644--- a/docs/src/api/class-browser.md+++ b/docs/src/api/class-browser.md@@ -63,20 +63,13 @@ with sync_playwright() as playwright:```csharpusing 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.disconnecteddiff --git a/docs/src/api/class-browsercontext.md b/docs/src/api/class-browsercontext.mdindex 2d770f86d2..7d30bb6a69 100644--- a/docs/src/api/class-browsercontext.md+++ b/docs/src/api/class-browsercontext.md@@ -493,28 +493,21 @@ with sync_playwright() as playwright:```csharpusing 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:diff --git a/docs/src/api/class-locatorassertions.md b/docs/src/api/class-locatorassertions.mdindex 9282c34a1b..5592ae01c0 100644--- a/docs/src/api/class-locatorassertions.md+++ b/docs/src/api/class-locatorassertions.md@@ -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");}}```diff --git a/docs/src/api/class-pageassertions.md b/docs/src/api/class-pageassertions.mdindex 69d32d989c..4c3c63bb29 100644--- a/docs/src/api/class-pageassertions.md+++ b/docs/src/api/class-pageassertions.md@@ -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"));}}```diff --git a/docs/src/api/class-playwrightassertions.md b/docs/src/api/class-playwrightassertions.mdindex e7733ed70f..c4cffd3ef3 100644--- a/docs/src/api/class-playwrightassertions.md+++ b/docs/src/api/class-playwrightassertions.md@@ -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");}}```diff --git a/docs/src/api/class-timeouterror.md b/docs/src/api/class-timeouterror.mdindex 4416b7cfb9..f0d9b3e408 100644--- a/docs/src/api/class-timeouterror.md+++ b/docs/src/api/class-timeouterror.md@@ -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!");}```diff --git a/docs/src/browsers.md b/docs/src/browsers.mdindex 898048c929..1497ae1f4e 100644--- a/docs/src/browsers.md+++ b/docs/src/browsers.md@@ -90,18 +90,11 @@ browser = playwright.chromium.launch(channel="chrome")```csharpusing 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?diff --git a/docs/src/ci.md b/docs/src/ci.mdindex cab933a0f8..47a0532666 100644--- a/docs/src/ci.md+++ b/docs/src/ci.md@@ -313,19 +313,12 @@ with sync_playwright() as p:```csharpusing 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.diff --git a/docs/src/cli.md b/docs/src/cli.mdindex 4109cbf2c0..6cf4891ac1 100644--- a/docs/src/cli.md+++ b/docs/src/cli.md@@ -255,26 +255,19 @@ with sync_playwright() as p:```csharpusing 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 });-- // 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();- }-}+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());++// Pause the page, and start recording manually.+var page = await context.NewPageAsync();+await page.PauseAsync();```## Open pagesdiff --git a/docs/src/codegen.md b/docs/src/codegen.mdindex ea7fa6eb4a..5684326f0a 100644--- a/docs/src/codegen.md+++ b/docs/src/codegen.md@@ -163,26 +163,19 @@ with sync_playwright() as p:```csharpusing 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 });-- // 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();- }-}++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());++// Pause the page, and start recording manually.+var page = await context.NewPageAsync();+await page.PauseAsync();```## Emulate devicesdiff --git a/docs/src/intro-csharp.md b/docs/src/intro-csharp.mdindex c84b146be9..4fbb89b2aa 100644--- a/docs/src/intro-csharp.md+++ b/docs/src/intro-csharp.md@@ -30,19 +30,12 @@ Create a `Program.cs` that will navigate to `https://playwright.dev/dotnet` and```csharpusing 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 onldotnet add package Microsoft.Playwright.NUnit# Build the projectdotnet 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+namespace PlaywrightTests;++[Parallelizable(ParallelScope.Self)]+public class Tests : PageTest{- [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(){- [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);- }+ int result = await Page.EvaluateAsync<int>("() => 7 * 3");+ Assert.AreEqual(21, result);}}```diff --git a/docs/src/network.md b/docs/src/network.mdindex 8e3e5474fc..fef08d8b35 100644--- a/docs/src/network.md+++ b/docs/src/network.md@@ -244,20 +244,13 @@ with sync_playwright() as playwright:```csharpusing 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:diff --git a/docs/src/pom.md b/docs/src/pom.mdindex e3e55bc444..984cd8172d 100644--- a/docs/src/pom.md+++ b/docs/src/pom.md@@ -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)+ {+ _page = page;+ _searchTermInput = page.Locator("[aria-label='Enter your search term']");+ }++ public async Task Goto()+ {+ await _page.GotoAsync("https://bing.com");+ }++ public async Task Search(string text){- private readonly IPage _page;- private readonly ILocator _searchTermInput;-- 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 Search(string text)- {- await _searchTermInput.FillAsync(text);- await _searchTermInput.PressAsync("Enter");- }+ await _searchTermInput.FillAsync(text);+ await _searchTermInput.PressAsync("Enter");}}```diff --git a/docs/src/release-notes-csharp.md b/docs/src/release-notes-csharp.mdindex 7fe64a4820..369e4e5b01 100644--- a/docs/src/release-notes-csharp.md+++ b/docs/src/release-notes-csharp.md@@ -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");}}```diff --git a/docs/src/test-runners-csharp.md b/docs/src/test-runners-csharp.mdindex c11ef079fa..1059b29bb5 100644--- a/docs/src/test-runners-csharp.md+++ b/docs/src/test-runners-csharp.md@@ -34,24 +34,23 @@ using System.Threading.Tasks;using Microsoft.Playwright.NUnit;using NUnit.Framework;-namespace PlaywrightTests+namespace PlaywrightTests;++[Parallelizable(ParallelScope.Self)]+public class MyTest : PageTest{- [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(){- [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);- }+ 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 supportWhile 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).\ No newline at end of file+outlined by the maintainers across [several](https://github.com/xunit/xunit/issues/2003) [issues](https://github.com/xunit/xunit/issues/2111#issuecomment-650004247).