2022-07-19 00:39:01 +03:00
---
id: writing-tests
title: "Writing Tests"
---
2022-07-21 01:57:09 +03:00
Playwright assertions are created specifically for the dynamic web. Checks are automatically retried until the necessary conditions are met. Playwright comes with [auto-wait ](./actionability.md ) built in meaning it waits for elements to be actionable prior to performing actions. Playwright provides the [Expect ](./test-assertions ) function to write assertions.
2022-07-19 00:39:01 +03:00
2022-10-28 18:54:21 +03:00
Take a look at the example test below to see how to write a test using using [locators ](/locators.md ) and web first assertions.
2022-07-19 00:39:01 +03:00
< Tabs
2022-08-03 14:39:18 +03:00
groupId="test-runners"
2022-07-19 00:39:01 +03:00
defaultValue="nunit"
values={[
{label: 'NUnit', value: 'nunit'},
{label: 'MSTest', value: 'mstest'}
]
}>
< TabItem value = "nunit" >
```csharp
using System.Text.RegularExpressions;
2022-12-19 18:28:17 +03:00
using System.Threading.Tasks;
using Microsoft.Playwright;
2022-07-19 00:39:01 +03:00
using Microsoft.Playwright.NUnit;
2022-12-19 18:28:17 +03:00
using NUnit.Framework;
2022-07-19 00:39:01 +03:00
namespace PlaywrightTests;
[Parallelizable(ParallelScope.Self)]
2022-09-08 02:44:58 +03:00
[TestFixture]
2022-07-19 00:39:01 +03:00
public class Tests : PageTest
{
2022-09-14 23:44:38 +03:00
[Test]
2022-08-07 16:04:08 +03:00
public async Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage()
2022-07-19 00:39:01 +03:00
{
await Page.GotoAsync("https://playwright.dev");
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
// create a locator
2022-11-30 07:56:18 +03:00
var getStarted = Page.GetByRole(AriaRole.Link, new() { Name = "Get started" });
2022-07-19 00:39:01 +03:00
// Expect an attribute "to be strictly equal" to the value.
await Expect(getStarted).ToHaveAttributeAsync("href", "/docs/intro");
// Click the get started link.
await getStarted.ClickAsync();
// Expects the URL to contain intro.
await Expect(Page).ToHaveURLAsync(new Regex(".*intro"));
}
}
```
< / TabItem >
< TabItem value = "mstest" >
```csharp
using System.Text.RegularExpressions;
2022-12-19 18:28:17 +03:00
using System.Threading.Tasks;
using Microsoft.Playwright;
2022-07-19 00:39:01 +03:00
using Microsoft.Playwright.MSTest;
2022-12-19 18:28:17 +03:00
using Microsoft.VisualStudio.TestTools.UnitTesting;
2022-07-19 00:39:01 +03:00
namespace PlaywrightTests;
2022-08-19 15:19:34 +03:00
[TestClass]
2022-07-19 00:39:01 +03:00
public class UnitTest1 : PageTest
{
2022-09-14 23:44:38 +03:00
[TestMethod]
2022-08-07 16:04:08 +03:00
public async Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage()
2022-07-19 00:39:01 +03:00
{
await Page.GotoAsync("https://playwright.dev");
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
// create a locator
2022-11-30 07:56:18 +03:00
var getStarted = Page.GetByRole(AriaRole.Link, new() { Name = "Get started" });
2022-07-19 00:39:01 +03:00
// Expect an attribute "to be strictly equal" to the value.
await Expect(getStarted).ToHaveAttributeAsync("href", "/docs/intro");
// Click the get started link.
await getStarted.ClickAsync();
// Expects the URL to contain intro.
await Expect(Page).ToHaveURLAsync(new Regex(".*intro"));
}
}
```
< / TabItem >
< / Tabs >
### Assertions
Playwright provides an async function called [Expect ](./test-assertions ) to assert and wait until the expected condition is met.
```csharp
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
```
### Locators
2022-10-28 18:54:21 +03:00
[Locators ](./locators.md ) are the central piece of Playwright's auto-waiting and retry-ability. Locators represent a way to find element(s) on the page at any moment and are used to perform actions on elements such as `.ClickAsync` `.FillAsync` etc.
2022-07-19 00:39:01 +03:00
```csharp
2022-11-30 07:56:18 +03:00
var getStarted = Page.GetByRole(AriaRole.Link, new() { Name = "Get started" });
2022-07-19 00:39:01 +03:00
await Expect(getStarted).ToHaveAttributeAsync("href", "/docs/installation");
await getStarted.ClickAsync();
```
### Test Isolation
The Playwright NUnit and MSTest test framework base classes will isolate each test from each other by providing a separate `Page` instance. Pages are isolated between tests due to the Browser Context, which is equivalent to a brand new browser profile, where every test gets a fresh environment, even when multiple tests run in a single Browser.
< Tabs
2022-08-03 14:39:18 +03:00
groupId="test-runners"
2022-07-19 00:39:01 +03:00
defaultValue="nunit"
values={[
{label: 'NUnit', value: 'nunit'},
{label: 'MSTest', value: 'mstest'}
]
}>
< TabItem value = "nunit" >
```csharp
using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace PlaywrightTests;
[Parallelizable(ParallelScope.Self)]
2022-09-08 02:44:58 +03:00
[TestFixture]
2022-07-19 00:39:01 +03:00
public class Tests : PageTest
{
2022-09-14 23:44:38 +03:00
[Test]
2022-07-19 00:39:01 +03:00
public async Task BasicTest()
{
await Page.GotoAsync("https://playwright.dev");
}
}
```
< / TabItem >
< TabItem value = "mstest" >
```csharp
2022-12-19 18:28:17 +03:00
using System.Threading.Tasks;
2022-07-19 00:39:01 +03:00
using Microsoft.Playwright.MSTest;
2022-12-19 18:28:17 +03:00
using Microsoft.VisualStudio.TestTools.UnitTesting;
2022-07-19 00:39:01 +03:00
namespace PlaywrightTests;
2022-08-19 15:19:34 +03:00
[TestClass]
2022-07-19 00:39:01 +03:00
public class UnitTest1 : PageTest
{
2022-09-14 23:44:38 +03:00
[TestMethod]
2022-07-19 00:39:01 +03:00
public async Task BasicTest()
{
await Page.GotoAsync("https://playwright.dev");
}
}
```
< / TabItem >
< / Tabs >
### Using Test Hooks
You can use `SetUp` /`TearDown` in NUnit or `TestInitialize` /`TestCleanup` in MSTest to prepare and clean up your test environment:
< Tabs
2022-08-03 14:39:18 +03:00
groupId="test-runners"
2022-07-19 00:39:01 +03:00
defaultValue="nunit"
values={[
{label: 'NUnit', value: 'nunit'},
{label: 'MSTest', value: 'mstest'}
]
}>
< TabItem value = "nunit" >
```csharp
using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace PlaywrightTests;
[Parallelizable(ParallelScope.Self)]
2022-09-08 02:44:58 +03:00
[TestFixture]
2022-07-19 00:39:01 +03:00
public class Tests : PageTest
{
2022-09-14 23:44:38 +03:00
[Test]
2022-07-19 00:39:01 +03:00
public async Task MainNavigation()
{
// Assertions use the expect API.
await Expect(Page).ToHaveURLAsync("https://playwright.dev/");
}
[SetUp]
public async Task SetUp()
{
await Page.GotoAsync("https://playwright.dev");
}
}
```
< / TabItem >
< TabItem value = "mstest" >
```csharp
2022-12-19 18:28:17 +03:00
using System.Threading.Tasks;
2022-07-19 00:39:01 +03:00
using Microsoft.Playwright.MSTest;
2022-12-19 18:28:17 +03:00
using Microsoft.VisualStudio.TestTools.UnitTesting;
2022-07-19 00:39:01 +03:00
namespace PlaywrightTests;
2022-08-19 15:19:34 +03:00
[TestClass]
2022-07-19 00:39:01 +03:00
public class UnitTest1 : PageTest
{
2022-09-14 23:44:38 +03:00
[TestMethod]
2022-07-19 00:39:01 +03:00
public async Task MainNavigation()
{
// Assertions use the expect API.
await Expect(Page).ToHaveURLAsync("https://playwright.dev/");
}
[TestInitialize]
public async Task TestInitialize()
{
await Page.GotoAsync("https://playwright.dev");
}
}
```
< / TabItem >
< / Tabs >
## What's Next
2022-10-21 19:52:36 +03:00
- [Run single test, multiple tests, headed mode ](./running-tests.md )
2022-07-19 00:39:01 +03:00
- [Generate tests with Codegen ](./codegen.md )
2022-08-10 15:34:27 +03:00
- [See a trace of your tests ](./trace-viewer-intro.md )