Skip to content

Commit f441755

Browse files
authored
docs(dotnet): add test runner docs (#6919)
1 parent 69b7346 commit f441755

File tree

3 files changed

+189
-34
lines changed

3 files changed

+189
-34
lines changed

docs/src/cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ playwright
2323

2424
```bash csharp
2525
# Install the CLI once.
26-
dotnet add package Microsoft.Playwright.CLI
26+
dotnet tool install --global Microsoft.Playwright.CLI
2727
# Use the tools.
2828
playwright
2929
```

docs/src/intro-csharp.md

Lines changed: 76 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,13 @@ title: "Getting Started"
88

99
## Installation
1010

11-
Install Microsoft.Playwright package from NuGet in Visual Studio or from the CLI in your project root directory:
11+
Start with installing `playwright` dotnet tool globally. This only needs to be done once. Learn more about [Playwright CLI](./cli.md) tool.
1212

1313
```bash
14-
dotnet add package Microsoft.Playwright
14+
dotnet tool install --global Microsoft.Playwright.CLI
1515
```
1616

17-
## Usage
18-
19-
```csharp
20-
using Microsoft.Playwright;
21-
using System.Threading.Tasks;
22-
23-
class Program
24-
{
25-
public static async Task Main()
26-
{
27-
using var playwright = await Playwright.CreateAsync();
28-
await using var browser = await playwright.Chromium.LaunchAsync();
29-
// Create pages, interact with UI elements, assert values
30-
}
31-
}
32-
```
33-
34-
## First script
17+
## First project
3518

3619
Create a console project and add the Playwright dependency.
3720

@@ -41,29 +24,45 @@ cd pw_demo
4124
dotnet add package Microsoft.Playwright --prerelease
4225
```
4326

44-
Create a Program.cs that will navigate to `https://playwright.dev/dotnet` and take a screenshot in Chromium.
27+
Ensure browsers necessary for testing are installed.
28+
29+
```bash
30+
playwright install
31+
```
32+
33+
Create a `Program.cs` that will navigate to `https://playwright.dev/dotnet` and take a screenshot in Chromium.
4534

4635
```csharp
47-
using Microsoft.Playwright;
36+
using System;
4837
using System.Threading.Tasks;
38+
using Microsoft.Playwright.NUnit;
39+
using NUnit.Framework;
4940

50-
class Program
41+
namespace PlaywrightTests
5142
{
52-
public static async Task Main()
43+
[Parallelizable(ParallelScope.Self)]
44+
public class MyTest : PageTest
5345
{
54-
using var playwright = await Playwright.CreateAsync();
55-
await using var browser = await playwright.Chromium.LaunchAsync();
56-
var page = await browser.NewPageAsync();
57-
await page.GotoAsync("https://playwright.dev/dotnet");
58-
await page.ScreenshotAsync(new PageScreenshotOptions { Path = "screenshot.png" });
46+
[Test]
47+
public async Task ShouldAdd()
48+
{
49+
int result = await Page.EvaluateAsync<int>("() => 7 + 3");
50+
Assert.AreEqual(10, result);
51+
}
52+
53+
[Test]
54+
public async Task ShouldMultiply()
55+
{
56+
int result = await Page.EvaluateAsync<int>("() => 7 * 3");
57+
Assert.AreEqual(21, result);
58+
}
5959
}
6060
}
6161
```
6262

63-
Now build it and run it.
63+
Now run it.
6464

6565
```bash
66-
dotnet build
6766
dotnet run
6867
```
6968

@@ -73,12 +72,56 @@ By default, Playwright runs the browsers in headless mode. To see the browser UI
7372
await playwright.Firefox.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false, SlowMo = 50 });
7473
```
7574

75+
## First test
76+
77+
You can choose to use NUnit test fixtures that come bundled with Playwright. These fixtures support running tests on multiple browser engines in parallel, out of the box. Learn more about [Playwright with NUnit](./test-runners.md).
78+
79+
```bash
80+
dotnet new console -n pw_test
81+
cd pw_test
82+
dotnet add package Microsoft.Playwright --prerelease
83+
dotnet add package Microsoft.Playwright.NUnit --prerelease
84+
```
85+
86+
Ensure browsers necessary for testing are installed.
87+
88+
```bash
89+
playwright install
90+
```
91+
92+
Create a PageTests.cs file.
93+
```csharp
94+
using System;
95+
using System.Threading.Tasks;
96+
using Microsoft.Playwright.NUnit;
97+
using NUnit.Framework;
98+
99+
namespace ExampleTest
100+
{
101+
[Parallelizable(ParallelScope.Self)]
102+
public class PageTests : PageTest
103+
{
104+
[Test]
105+
public async Task ShouldMultiply()
106+
{
107+
int result = await Page.EvaluateAsync<int>("() => 7 * 3");
108+
Assert.AreEqual(21, result);
109+
}
110+
}
111+
}
112+
```
113+
114+
```bash
115+
dotnet build
116+
dotnet test -- NUnit.NumberOfTestWorkers=5
117+
```
118+
76119
## Record scripts
77120

78-
Command Line Interface [CLI](./cli.md) can be used to record user interactions and generate C# code.
121+
[Command Line Interface](./cli.md) can be used to record user interactions and generate C# code.
79122

80123
```bash
81-
# FIXME:
124+
playwright codegen
82125
```
83126

84127
## System requirements

docs/src/test-runners-csharp.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
id: test-runners
3+
title: "Test Runners"
4+
---
5+
6+
With a few lines of code, you can hook up Playwright to your favorite .NET test runner.
7+
8+
Playwright and Browser instances can be reused between tests for better performance. We
9+
recommend running each test case in a new BrowserContext, this way browser state will be
10+
isolated between the tests.
11+
12+
<!-- TOC -->
13+
14+
## Creating NUnit project
15+
16+
```bash
17+
dotnet new console -n pw_test
18+
cd pw_test
19+
dotnet add package Microsoft.Playwright --prerelease
20+
dotnet add package Microsoft.Playwright.NUnit --prerelease
21+
```
22+
23+
Ensure browsers necessary for testing are installed.
24+
25+
```bash
26+
playwright install
27+
```
28+
29+
Create a PageTests.cs file.
30+
31+
```csharp
32+
using System;
33+
using System.Threading.Tasks;
34+
using Microsoft.Playwright.NUnit;
35+
using NUnit.Framework;
36+
37+
namespace PlaywrightTests
38+
{
39+
[Parallelizable(ParallelScope.Self)]
40+
public class MyTest : PageTest
41+
{
42+
[Test]
43+
public async Task ShouldAdd()
44+
{
45+
int result = await Page.EvaluateAsync<int>("() => 7 + 3");
46+
Assert.AreEqual(10, result);
47+
}
48+
49+
[Test]
50+
public async Task ShouldMultiply()
51+
{
52+
int result = await Page.EvaluateAsync<int>("() => 7 * 3");
53+
Assert.AreEqual(21, result);
54+
}
55+
}
56+
}
57+
```
58+
59+
Run your tests against Chromium
60+
61+
```bash
62+
dotnet build
63+
dotnet test
64+
```
65+
66+
Run your tests against WebKit
67+
68+
Windows
69+
70+
```bash
71+
set BROWSER=webkit
72+
dotnet test
73+
```
74+
75+
Linux & Mac
76+
77+
```bash
78+
BROWSER=webkit dotnet test
79+
```
80+
81+
Run your tests with GUI
82+
83+
Window
84+
85+
```bash
86+
set HEADED=1
87+
dotnet test
88+
```
89+
90+
Linux & Mac
91+
92+
```bash
93+
HEADED=1 dotnet test
94+
```
95+
96+
## Running NUnit tests in Parallel
97+
98+
By default NUnit will run all test files in parallel, while running tests inside each file sequentially. It will create as many processes as there are cores on the host system. You can adjust this behavior using the NUnit.NumberOfTestWorkers parameter.
99+
100+
For CPU-bound tests, we recommend using as many workers as there are cores on your system, divided by 2. For IO-bound tests you can use as many workers as you have cores.
101+
102+
## Base NUnit classes for Playwright
103+
104+
There are few base classes available to you in Microsoft.Playwright.NUnit namespace:
105+
106+
|Test |Description|
107+
|--------------|-----------|
108+
|PageTest |Each test gets a fresh copy of a web [Page] created in its own unique [BrowserContext]. Extending this class is the simplest way of writing a fully-functional Playwright test.<br></br><br></br>Note: You can override the `ContextOptions` method in each test file to control context options, the ones typically passed into the [`method: Browser.newContext`] method. That way you can specify all kinds of emulation options for your test file individually.|
109+
|ContextTest |Each test will get a fresh copy of a [BrowserContext]. You can create as many pages in this context as you'd like. Using this test is the easiest way to test multi-page scenarios where you need more than one tab.<br></br><br></br>Note: You can override the `ContextOptions` method in each test file to control context options, the ones typically passed into the [`method: Browser.newContext`] method. That way you can specify all kinds of emulation options for your test file individually.|
110+
|BrowserTest |Each test will get a browser and can create as many contexts as it likes. Each test is responsible for cleaning up all the contexts it created.|
111+
|PlaywrightTest|This gives each test a Playwright object so that the test could start and stop as many browsers as it likes.|
112+

0 commit comments

Comments
 (0)