2021-04-06 22:23:55 -07:00
---
id: intro
2022-07-18 23:39:01 +02:00
title: "Installation"
2021-04-06 22:23:55 -07:00
---
2023-10-06 15:08:51 +02:00
## Introduction
2022-07-18 23:39:01 +02:00
Playwright was created specifically to accommodate the needs of end-to-end testing. Playwright supports all modern rendering engines including Chromium, WebKit, and Firefox. Test on Windows, Linux, and macOS, locally or on CI, headless or headed with native mobile emulation.
2021-04-06 22:23:55 -07:00
2023-05-22 13:02:30 -05:00
You can choose to use [NUnit base classes ](./test-runners.md#nunit ) or [MSTest base classes ](./test-runners.md#mstest ) that Playwright provides to write end-to-end tests. These classes support running tests on multiple browser engines, parallelizing tests, adjusting launch/context options and getting a [Page]/[BrowserContext] instance per test out of the box. Alternatively you can use the [library ](./library.md ) to manually write the testing infrastructure.
2021-04-06 22:23:55 -07:00
2022-07-21 00:57:09 +02:00
1. Start by creating a new project with `dotnet new` . This will create the `PlaywrightTests` directory which includes a `UnitTest1.cs` file:
2022-07-18 23:39:01 +02:00
< Tabs
2022-08-03 13:39:18 +02:00
groupId="test-runners"
2022-07-18 23:39:01 +02:00
defaultValue="nunit"
values={[
{label: 'NUnit', value: 'nunit'},
{label: 'MSTest', value: 'mstest'}
]
}>
< TabItem value = "nunit" >
2021-05-22 07:55:53 -07:00
2021-06-02 09:23:06 -07:00
```bash
2022-07-18 23:39:01 +02:00
dotnet new nunit -n PlaywrightTests
cd PlaywrightTests
```
2021-05-22 07:55:53 -07:00
2022-07-18 23:39:01 +02:00
< / TabItem >
< TabItem value = "mstest" >
2022-01-18 00:28:53 +01:00
2022-07-18 23:39:01 +02:00
```bash
dotnet new mstest -n PlaywrightTests
cd PlaywrightTests
2021-06-04 20:52:35 -07:00
```
2022-07-18 23:39:01 +02:00
< / TabItem >
< / Tabs >
2021-04-06 22:23:55 -07:00
2022-07-21 00:57:09 +02:00
2. Install the necessary Playwright dependencies:
2021-04-06 22:23:55 -07:00
2022-07-18 23:39:01 +02:00
< Tabs
2022-08-03 13:39:18 +02:00
groupId="test-runners"
2022-07-18 23:39:01 +02:00
defaultValue="nunit"
values={[
{label: 'NUnit', value: 'nunit'},
{label: 'MSTest', value: 'mstest'}
]
}>
< TabItem value = "nunit" >
2021-05-22 07:55:53 -07:00
2021-06-02 09:23:06 -07:00
```bash
2022-07-18 23:39:01 +02:00
dotnet add package Microsoft.Playwright.NUnit
2021-05-22 07:55:53 -07:00
```
2022-07-18 23:39:01 +02:00
< / TabItem >
< TabItem value = "mstest" >
2021-04-06 22:23:55 -07:00
2022-07-18 23:39:01 +02:00
```bash
dotnet add package Microsoft.Playwright.MSTest
2021-04-06 22:23:55 -07:00
```
2022-07-18 23:39:01 +02:00
< / TabItem >
< / Tabs >
2021-06-04 20:52:35 -07:00
2022-07-21 00:57:09 +02:00
3. Build the project so the `playwright.ps1` is available inside the `bin` directory:
2021-06-04 20:52:35 -07:00
```bash
2022-07-18 23:39:01 +02:00
dotnet build
2021-06-07 11:54:50 -07:00
```
2021-06-05 14:33:36 -07:00
2024-05-02 12:06:39 +02:00
1. Install required browsers. This example uses `net8.0` , if you are using a different version of .NET you will need to adjust the command and change `net8.0` to your version.
2021-06-04 20:52:35 -07:00
2021-06-07 11:54:50 -07:00
```bash
2024-05-02 12:06:39 +02:00
pwsh bin/Debug/net8.0/playwright.ps1 install
2021-06-04 20:52:35 -07:00
```
2024-05-02 12:06:39 +02:00
If `pwsh` is not available, you will have to [install PowerShell ](https://docs.microsoft.com/powershell/scripting/install/installing-powershell ).
2022-08-14 20:01:00 +02:00
2022-07-18 23:39:01 +02:00
## Add Example Tests
Edit the `UnitTest1.cs` file with the code below to create an example end-to-end test:
< Tabs
2022-08-03 13:39:18 +02:00
groupId="test-runners"
2022-07-18 23:39:01 +02:00
defaultValue="nunit"
values={[
{label: 'NUnit', value: 'nunit'},
{label: 'MSTest', value: 'mstest'}
]
}>
< TabItem value = "nunit" >
2024-01-31 17:49:01 +01:00
```csharp title="UnitTest1.cs"
2022-07-22 17:45:18 +02:00
using System.Text.RegularExpressions;
2021-06-04 20:52:35 -07:00
using System.Threading.Tasks;
2022-12-19 23:28:17 +08:00
using Microsoft.Playwright;
2021-06-04 20:52:35 -07:00
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
2022-04-19 21:23:26 +03:00
namespace PlaywrightTests;
[Parallelizable(ParallelScope.Self)]
2022-09-08 01:44:58 +02:00
[TestFixture]
2024-05-02 12:06:39 +02:00
public class ExampleTest : PageTest
2021-06-04 20:52:35 -07:00
{
2022-09-14 22:44:38 +02:00
[Test]
2024-05-02 12:06:39 +02:00
public async Task HasTitle()
2022-04-19 21:23:26 +03:00
{
2022-07-13 11:50:18 +02:00
await Page.GotoAsync("https://playwright.dev");
2022-07-21 00:57:09 +02:00
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
2024-05-02 12:06:39 +02:00
}
2022-04-19 21:23:26 +03:00
2024-05-02 12:06:39 +02:00
[Test]
public async Task GetStartedLink()
{
await Page.GotoAsync("https://playwright.dev");
2022-07-21 00:57:09 +02:00
// Click the get started link.
2024-05-02 12:06:39 +02:00
await Page.GetByRole(AriaRole.Link, new() { Name = "Get started" }).ClickAsync();
2022-07-21 00:57:09 +02:00
2024-05-02 12:06:39 +02:00
// Expects page to have a heading with the name of Installation.
await Expect(Page.GetByRole(AriaRole.Heading, new() { Name = "Installation" })).ToBeVisibleAsync();
}
2021-06-04 20:52:35 -07:00
}
```
2022-07-18 23:39:01 +02:00
< / TabItem >
< TabItem value = "mstest" >
2021-04-06 22:23:55 -07:00
2024-01-31 17:49:01 +01:00
```csharp title="UnitTest1.cs"
2022-07-22 17:45:18 +02:00
using System.Text.RegularExpressions;
2022-12-19 23:28:17 +08:00
using System.Threading.Tasks;
using Microsoft.Playwright;
2022-07-18 23:39:01 +02:00
using Microsoft.Playwright.MSTest;
2022-12-19 23:28:17 +08:00
using Microsoft.VisualStudio.TestTools.UnitTesting;
2021-04-06 22:23:55 -07:00
2022-07-18 23:39:01 +02:00
namespace PlaywrightTests;
2021-04-06 22:23:55 -07:00
2022-08-19 14:19:34 +02:00
[TestClass]
2024-05-02 12:06:39 +02:00
public class ExampleTest : PageTest
2022-07-18 23:39:01 +02:00
{
2022-09-14 22:44:38 +02:00
[TestMethod]
2024-05-02 12:06:39 +02:00
public async Task HasTitle()
2022-07-18 23:39:01 +02:00
{
await Page.GotoAsync("https://playwright.dev");
2022-01-26 20:43:44 +01:00
2022-07-21 00:57:09 +02:00
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
2024-05-02 12:06:39 +02:00
}
2022-01-26 20:43:44 +01:00
2024-05-02 12:06:39 +02:00
[TestMethod]
public async Task GetStartedLink()
{
await Page.GotoAsync("https://playwright.dev");
2022-07-21 00:57:09 +02:00
// Click the get started link.
2024-05-02 12:06:39 +02:00
await Page.GetByRole(AriaRole.Link, new() { Name = "Get started" }).ClickAsync();
2022-07-21 00:57:09 +02:00
2024-05-02 12:06:39 +02:00
// Expects page to have a heading with the name of Installation.
await Expect(Page.GetByRole(AriaRole.Heading, new() { Name = "Installation" })).ToBeVisibleAsync();
}
2022-01-26 20:43:44 +01:00
}
```
2022-07-18 23:39:01 +02:00
< / TabItem >
< / Tabs >
2022-01-26 21:26:51 +01:00
2022-07-18 23:39:01 +02:00
## Running the Example Tests
2022-01-26 21:26:51 +01:00
2024-05-02 12:06:39 +02:00
By default tests will be run on Chromium. This can be configured via the `BROWSER` environment variable, or by adjusting the [launch configuration options ](./running-tests.md ). Tests are run in headless mode meaning no browser will open up when running the tests. Results of the tests and test logs will be shown in the terminal.
2021-04-30 16:44:30 +02:00
2022-07-18 23:39:01 +02:00
```bash
2024-05-02 12:06:39 +02:00
dotnet test
2022-07-18 23:39:01 +02:00
```
2021-04-30 16:44:30 +02:00
2024-05-02 12:06:39 +02:00
See our doc on [Running and Debugging Tests ](./running-tests.md ) to learn more about running tests in headed mode, running multiple tests, running specific configurations etc.
2021-04-30 16:44:30 +02:00
2023-06-02 14:13:02 -07:00
## System requirements
2023-12-18 18:38:51 +01:00
- Playwright is distributed as a .NET Standard 2.0 library. We recommend .NET 8.
2023-06-02 14:13:02 -07:00
- Windows 10+, Windows Server 2016+ or Windows Subsystem for Linux (WSL).
2023-12-18 18:38:51 +01:00
- MacOS 12 Monterey, MacOS 13 Ventura, or MacOS 14 Sonoma.
2023-07-13 12:56:44 +02:00
- Debian 11, Debian 12, Ubuntu 20.04 or Ubuntu 22.04.
2023-06-02 14:13:02 -07:00
2022-07-18 23:39:01 +02:00
## What's next
2021-04-30 16:44:30 +02:00
2022-07-18 23:39:01 +02:00
- [Write tests using web first assertions, page fixtures and locators ](./writing-tests.md )
2022-10-21 09:52:36 -07:00
- [Run single test, multiple tests, headed mode ](./running-tests.md )
2024-05-08 22:55:44 +02:00
- [Generate tests with Codegen ](./codegen-intro.md )
2022-08-10 14:34:27 +02:00
- [See a trace of your tests ](./trace-viewer-intro.md )
2024-05-02 12:06:39 +02:00
- [Run tests on CI ](./ci-intro.md )
- [Learn more about the NUnit and MSTest base classes ](./test-runners.md )