mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
doc(dotnet): add a self-contained example (#6702)
This commit is contained in:
parent
ba29e99ace
commit
f9357531f5
@ -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()
|
||||
{
|
||||
|
@ -332,9 +332,9 @@ with sync_playwright() as p:
|
||||
```csharp
|
||||
using Microsoft.Playwright;
|
||||
|
||||
class Example
|
||||
class Program
|
||||
{
|
||||
public async void Main()
|
||||
public static async Task Main()
|
||||
{
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
var chromium = playwright.Chromium;
|
||||
|
@ -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()
|
||||
{
|
||||
|
@ -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 BrowserTypeLaunchOptions
|
||||
|
@ -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()
|
||||
{
|
||||
|
@ -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()
|
||||
{
|
||||
|
@ -67,9 +67,9 @@ with sync_playwright() as playwright:
|
||||
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 using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
|
||||
|
@ -20,7 +20,7 @@ dotnet add package Microsoft.Playwright
|
||||
using 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.
|
||||
|
||||
```csharp
|
||||
using 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).
|
||||
|
||||
```csharp
|
||||
await playwright.Firefox.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false, SlowMo = 50 });
|
||||
|
@ -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()
|
||||
{
|
||||
|
@ -243,9 +243,9 @@ with sync_playwright() as playwright:
|
||||
using Microsoft.Playwright;
|
||||
using System;
|
||||
|
||||
class Example
|
||||
class Program
|
||||
{
|
||||
public async void Main()
|
||||
public static async Task Main()
|
||||
{
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
await using var browser = await playwright.Chromium.LaunchAsync();
|
||||
|
Loading…
x
Reference in New Issue
Block a user