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
doc(dotnet): add a self-contained example (#6702)
Pavel Feldmanf9357531f53e
diff --git a/docs/src/api/class-browser.md b/docs/src/api/class-browser.mdindex 4d8aba5d23..2d802dafa4 100644--- a/docs/src/api/class-browser.md+++ b/docs/src/api/class-browser.md@@ -65,7 +65,7 @@ with sync_playwright() as playwright:using Microsoft.Playwright;using System.Threading.Tasks;-class Example+class Program{public static async Task Main(){diff --git a/docs/src/api/class-frame.md b/docs/src/api/class-frame.mdindex 4e3c860f67..50527c4dbf 100644--- a/docs/src/api/class-frame.md+++ b/docs/src/api/class-frame.md@@ -1534,19 +1534,19 @@ using System.Threading.Tasks;class FrameExamples{- public static async Task Main()- {- using var playwright = await Playwright.CreateAsync();- await using var browser = await playwright.Chromium.LaunchAsync();- var page = await browser.NewPageAsync();-- foreach (var currentUrl in new[] { "https://www.google.com", "https://bbc.com" })+ public static async Task Main(){- await page.GotoAsync(currentUrl);- element = await page.MainFrame.WaitForSelectorAsync("img");- Console.WriteLine($"Loaded image: {await element.GetAttributeAsync("src")}");+ using var playwright = await Playwright.CreateAsync();+ await using var browser = await playwright.Chromium.LaunchAsync();+ var page = await browser.NewPageAsync();++ foreach (var currentUrl in new[] { "https://www.google.com", "https://bbc.com" })+ {+ await page.GotoAsync(currentUrl);+ element = await page.MainFrame.WaitForSelectorAsync("img");+ Console.WriteLine($"Loaded image: {await element.GetAttributeAsync("src")}");+ }}- }}```diff --git a/docs/src/api/class-page.md b/docs/src/api/class-page.mdindex df63f1cc1a..ec912ccad4 100644--- a/docs/src/api/class-page.md+++ b/docs/src/api/class-page.md@@ -1406,26 +1406,26 @@ using System.Threading.Tasks;class PageExamples{- public static async Task Main()- {- using var playwright = await Playwright.CreateAsync();- await using var browser = await playwright.Webkit.LaunchAsync(new BrowserTypeLaunchOptions- {- Headless: false- });- var page = await browser.NewPageAsync();+ public static async Task Main()+ {+ using var playwright = await Playwright.CreateAsync();+ await using var browser = await playwright.Webkit.LaunchAsync(new BrowserTypeLaunchOptions+ {+ Headless: false+ });+ var page = await browser.NewPageAsync();- await page.ExposeBindingAsync("pageUrl", (source) => source.Page.Url);- 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.ExposeBindingAsync("pageUrl", (source) => source.Page.Url);+ 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 page.ClickAsync("button");+ }}```@@ -1664,34 +1664,34 @@ using System.Threading.Tasks;class PageExamples{- public static async Task Main()- {- using var playwright = await Playwright.CreateAsync();- await using var browser = await playwright.Webkit.LaunchAsync(new BrowserTypeLaunchOptions- {- Headless: false- });- var page = await browser.NewPageAsync();+ public static async Task Main()+ {+ using var playwright = await Playwright.CreateAsync();+ await using var browser = await playwright.Webkit.LaunchAsync(new BrowserTypeLaunchOptions+ {+ Headless: false+ });+ var page = await browser.NewPageAsync();- // NOTE: md5 is inherently insecure, and we strongly discourage using- // this in production in any shape or form- await page.ExposeFunctionAsync("sha1", (string input) =>- {- return Convert.ToBase64String(- MD5.Create().ComputeHash(System.Text.Encoding.UTF8.GetBytes(input)));- });+ // NOTE: md5 is inherently insecure, and we strongly discourage using+ // this in production in any shape or form+ await page.ExposeFunctionAsync("sha1", (string input) =>+ {+ return Convert.ToBase64String(+ MD5.Create().ComputeHash(System.Text.Encoding.UTF8.GetBytes(input)));+ });- await page.SetContentAsync("<script>\n" +- " async function onClick() {\n" +- " document.querySelector('div').textContent = await window.sha1('PLAYWRIGHT');\n" +- " }\n" +- "</script>\n" +- "<button onclick=\"onClick()\">Click me</button>\n" +- "<div></div>");+ await page.SetContentAsync("<script>\n" ++ " async function onClick() {\n" ++ " document.querySelector('div').textContent = await window.sha1('PLAYWRIGHT');\n" ++ " }\n" ++ "</script>\n" ++ "<button onclick=\"onClick()\">Click me</button>\n" ++ "<div></div>");- await page.ClickAsync("button");- Console.WriteLine(await page.TextContentAsync("div"));- }+ await page.ClickAsync("button");+ Console.WriteLine(await page.TextContentAsync("div"));+ }}```diff --git a/docs/src/auth.md b/docs/src/auth.mdindex 90bcee4016..4500105b1b 100644--- a/docs/src/auth.md+++ b/docs/src/auth.md@@ -332,17 +332,17 @@ with sync_playwright() as p:```csharpusing Microsoft.Playwright;-class Example+class Program{- public async void Main()- {- using var playwright = await Playwright.CreateAsync();- var chromium = playwright.Chromium;- var context = chromium.LaunchPersistentContextAsync(@"C:\path\to\directory\", new BrowserTypeLaunchPersistentContextOptions- {- Headless = false- });- }+ public static async Task Main()+ {+ using var playwright = await Playwright.CreateAsync();+ var chromium = playwright.Chromium;+ var context = chromium.LaunchPersistentContextAsync(@"C:\path\to\directory\", new BrowserTypeLaunchPersistentContextOptions+ {+ Headless = false+ });+ }}```diff --git a/docs/src/browsers.md b/docs/src/browsers.mdindex a10ce559ac..b0ff44aaa8 100644--- a/docs/src/browsers.md+++ b/docs/src/browsers.md@@ -71,7 +71,7 @@ browser = playwright.chromium.launch(channel="chrome")using Microsoft.Playwright;using System.Threading.Tasks;-class Example+class Program{public static async Task Main(){diff --git a/docs/src/ci.md b/docs/src/ci.mdindex ac99cee0cf..29f7f5dc9c 100644--- a/docs/src/ci.md+++ b/docs/src/ci.md@@ -270,9 +270,9 @@ browser = playwright.chromium.launch(chromiumSandbox=False)using Microsoft.Playwright;using System.Threading.Tasks;-class Example+class Program{- public async void Main()+ public static async Task Main(){using var playwright = await Playwright.CreateAsync();await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions@@ -399,9 +399,9 @@ with sync_playwright() as p:using Microsoft.Playwright;using System.Threading.Tasks;-class Example+class Program{- public async void Main()+ public static async Task Main(){using var playwright = await Playwright.CreateAsync();await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptionsdiff --git a/docs/src/cli.md b/docs/src/cli.mdindex 8953d33f8a..24eb9fd7ac 100644--- a/docs/src/cli.md+++ b/docs/src/cli.md@@ -172,7 +172,7 @@ with sync_playwright() as p:using Microsoft.Playwright;using System.Threading.Tasks;-class Example+class Program{public static async Task Main(){diff --git a/docs/src/core-concepts.md b/docs/src/core-concepts.mdindex 621af0842e..cab03431f1 100644--- a/docs/src/core-concepts.md+++ b/docs/src/core-concepts.md@@ -67,7 +67,7 @@ with sync_playwright() as p:using Microsoft.Playwright;using System.Threading.Tasks;-class Example+class Program{public static async Task Main(){diff --git a/docs/src/emulation.md b/docs/src/emulation.mdindex c3b81c1e76..f67d3187f3 100644--- a/docs/src/emulation.md+++ b/docs/src/emulation.md@@ -67,18 +67,18 @@ with sync_playwright() as playwright:using Microsoft.Playwright;using System.Threading.Tasks;-class Example+class Program{- public async void Main()- {- using var playwright = await Playwright.CreateAsync();- await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions- {- Headless: False- });- var pixel2 = playwright.Devices["Pixel 2"];- await using var context = await browser.NewContextAsync(pixel2);- }+ public static async Task Main()+ {+ using var playwright = await Playwright.CreateAsync();+ await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions+ {+ Headless: False+ });+ var pixel2 = playwright.Devices["Pixel 2"];+ await using var context = await browser.NewContextAsync(pixel2);+ }}```diff --git a/docs/src/intro-csharp.md b/docs/src/intro-csharp.mdindex ff8d06e7a0..9e3c2bae30 100644--- a/docs/src/intro-csharp.md+++ b/docs/src/intro-csharp.md@@ -20,7 +20,7 @@ dotnet add package Microsoft.Playwrightusing Microsoft.Playwright;using System.Threading.Tasks;-class Example+class Program{public static async Task Main(){@@ -33,26 +33,41 @@ class Example## First script-In our first script, we will navigate to `whatsmyuseragent.org` and take a screenshot in WebKit.+Create a console project and add the Playwright dependency.++```sh+dotnet new console -n pw_demo+cd pw_demo+dotnet add package Microsoft.Playwright --prerelease+```++Create a Program.cs that will navigate to `https://playwright.dev/dotnet` and take a screenshot in Chromium.```csharpusing Microsoft.Playwright;using System.Threading.Tasks;-class Example+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("whatsmyuseragent.org");+ await page.GotoAsync("https://playwright.dev/dotnet");await page.ScreenshotAsync(new PageScreenshotOptions { Path = "screenshot.png" });}}```-By default, Playwright runs the browsers in headless mode. To see the browser UI, pass the `headless: false` flag while launching the browser. You can also use [`option: slowMo`] to slow down execution. Learn more in the debugging tools [section](./debug.md).+Now build it and run it.++```ssh+dotnet build+dotnet run+```++By default, Playwright runs the browsers in headless mode. To see the browser UI, pass the `Headless = false` flag while launching the browser. You can also use [`option: slowMo`] to slow down execution. Learn more in the debugging tools [section](./debug.md).```csharpawait playwright.Firefox.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false, SlowMo = 50 });diff --git a/docs/src/multi-pages.md b/docs/src/multi-pages.mdindex b6b34bc165..15955baefe 100644--- a/docs/src/multi-pages.md+++ b/docs/src/multi-pages.md@@ -87,7 +87,7 @@ with sync_playwright() as playwright:using Microsoft.Playwright;using System.Threading.Tasks;-class Example+class Program{public static async Task Main(){diff --git a/docs/src/network.md b/docs/src/network.mdindex 0b72e133dc..62473808bd 100644--- a/docs/src/network.md+++ b/docs/src/network.md@@ -243,17 +243,17 @@ with sync_playwright() as playwright:using Microsoft.Playwright;using System;-class Example+class Program{- public async void 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");- }+ 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");+ }}```