chore: enhance .NET onboarding story (#15755)
This commit is contained in:
Родитель
ef84df9fc4
Коммит
9fda46822c
|
@ -157,6 +157,19 @@ See all supported browsers:
|
|||
pwsh bin\Debug\netX\playwright.ps1 install --help
|
||||
```
|
||||
|
||||
## Install browsers via API
|
||||
* langs: csharp
|
||||
|
||||
It's possible to run [Command line tools](./cli.md) commands via the .NET API:
|
||||
|
||||
```csharp
|
||||
var exitCode = Microsoft.Playwright.Program.Main(new[] {"install"});
|
||||
if (exitCode != 0)
|
||||
{
|
||||
throw new Exception($"Playwright exited with code {exitCode}");
|
||||
}
|
||||
```
|
||||
|
||||
## Managing browser binaries
|
||||
|
||||
Playwright downloads Chromium, WebKit and Firefox browsers into the OS-specific cache folders:
|
||||
|
|
|
@ -1,81 +1,89 @@
|
|||
---
|
||||
id: intro
|
||||
title: "Getting started"
|
||||
title: "Installation"
|
||||
---
|
||||
|
||||
<!-- TOC -->
|
||||
- [Release notes](./release-notes.md)
|
||||
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.
|
||||
|
||||
## First project
|
||||
You can choose to use [NUnit base classes](./test-runners.md#nunit) or [MSTest base classes](./test-runners.md#nunit) 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.
|
||||
|
||||
Create a console project and add the Playwright dependency.
|
||||
Start by creating a new project with `dotnet new`. This will create the `PlaywrightTests` directory which includes a `UnitTest1.cs` file:
|
||||
|
||||
<Tabs
|
||||
defaultValue="nunit"
|
||||
values={[
|
||||
{label: 'NUnit', value: 'nunit'},
|
||||
{label: 'MSTest', value: 'mstest'}
|
||||
]
|
||||
}>
|
||||
<TabItem value="nunit">
|
||||
|
||||
```bash
|
||||
# Create project
|
||||
dotnet new console -n PlaywrightDemo
|
||||
cd PlaywrightDemo
|
||||
|
||||
# Add project dependency
|
||||
dotnet add package Microsoft.Playwright
|
||||
# Build the project
|
||||
dotnet build
|
||||
# Install required browsers - replace netX with actual output folder name, f.ex. net6.0.
|
||||
pwsh bin\Debug\netX\playwright.ps1 install
|
||||
|
||||
# If the pwsh command does not work (throws TypeNotFound), make sure to use an up-to-date version of PowerShell.
|
||||
dotnet tool update --global PowerShell
|
||||
```
|
||||
|
||||
Create a `Program.cs` that will navigate to `https://playwright.dev/dotnet` and take a screenshot in Chromium.
|
||||
|
||||
```csharp
|
||||
using Microsoft.Playwright;
|
||||
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
await using var browser = await playwright.Chromium.LaunchAsync();
|
||||
var page = await browser.NewPageAsync();
|
||||
await page.GotoAsync("https://playwright.dev/dotnet");
|
||||
await page.ScreenshotAsync(new PageScreenshotOptions { Path = "screenshot.png" });
|
||||
```
|
||||
|
||||
Now run it.
|
||||
|
||||
```bash
|
||||
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()
|
||||
{
|
||||
Headless = false,
|
||||
SlowMo = 50,
|
||||
});
|
||||
```
|
||||
|
||||
## First test
|
||||
|
||||
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).
|
||||
|
||||
```bash
|
||||
# Create new project.
|
||||
dotnet new nunit -n PlaywrightTests
|
||||
cd PlaywrightTests
|
||||
```
|
||||
|
||||
Install dependencies, build project and download necessary browsers. This is only done once per project.
|
||||
</TabItem>
|
||||
<TabItem value="mstest">
|
||||
|
||||
```bash
|
||||
dotnet new mstest -n PlaywrightTests
|
||||
cd PlaywrightTests
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
Install the necessary Playwright dependencies:
|
||||
|
||||
<Tabs
|
||||
defaultValue="nunit"
|
||||
values={[
|
||||
{label: 'NUnit', value: 'nunit'},
|
||||
{label: 'MSTest', value: 'mstest'}
|
||||
]
|
||||
}>
|
||||
<TabItem value="nunit">
|
||||
|
||||
```bash
|
||||
# Add project dependency
|
||||
dotnet add package Microsoft.Playwright.NUnit
|
||||
# Build the project
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
<TabItem value="mstest">
|
||||
|
||||
```bash
|
||||
dotnet add package Microsoft.Playwright.MSTest
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
Build the project so the `playwright.ps1` is available inside the `bin` directory:
|
||||
|
||||
```bash
|
||||
dotnet build
|
||||
# Install required browsers - replace netX with actual output folder name, f.ex. net6.0.
|
||||
```
|
||||
|
||||
Install required browsers by replacing `netX` with the actual output folder name, e.g. `net6.0`:
|
||||
|
||||
```bash
|
||||
pwsh bin\Debug\netX\playwright.ps1 install
|
||||
```
|
||||
|
||||
Edit UnitTest1.cs file.
|
||||
## Add Example Tests
|
||||
|
||||
Edit the `UnitTest1.cs` file with the code below to create an example end-to-end test:
|
||||
|
||||
<Tabs
|
||||
defaultValue="nunit"
|
||||
values={[
|
||||
{label: 'NUnit', value: 'nunit'},
|
||||
{label: 'MSTest', value: 'mstest'}
|
||||
]
|
||||
}>
|
||||
<TabItem value="nunit">
|
||||
|
||||
```csharp
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Playwright.NUnit;
|
||||
|
@ -110,70 +118,78 @@ public class Tests : PageTest
|
|||
}
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
<TabItem value="mstest">
|
||||
|
||||
```csharp
|
||||
using Microsoft.Playwright.MSTest;
|
||||
|
||||
namespace PlaywrightTests;
|
||||
|
||||
public class UnitTest1 : PageTest
|
||||
{
|
||||
[TestMethod]
|
||||
async public Task ShouldHaveTheCorrectSlogan()
|
||||
{
|
||||
await Page.GotoAsync("https://playwright.dev");
|
||||
await Expect(Page.Locator("text=enables reliable end-to-end testing for modern web apps")).ToBeVisibleAsync();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task ShouldHaveTheCorrectTitle()
|
||||
{
|
||||
await Page.GotoAsync("https://playwright.dev");
|
||||
var title = Page.Locator(".navbar__inner .navbar__title");
|
||||
await Expect(title).ToHaveTextAsync("Playwright");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task ShouldAdd()
|
||||
{
|
||||
var result = await Page.EvaluateAsync<int>("() => 7 + 3");
|
||||
Assert.AreEqual(10, result);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
## Running the Example Tests
|
||||
|
||||
By default tests will be run on Chromium. This can be configured via the `BROWSER` environment variable, or by adjusting the [launch configuration options](./test-runners.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.
|
||||
|
||||
<Tabs
|
||||
defaultValue="nunit"
|
||||
values={[
|
||||
{label: 'NUnit', value: 'nunit'},
|
||||
{label: 'MSTest', value: 'mstest'}
|
||||
]
|
||||
}>
|
||||
<TabItem value="nunit">
|
||||
|
||||
```bash
|
||||
dotnet test -- NUnit.NumberOfTestWorkers=5
|
||||
```
|
||||
|
||||
## Record scripts
|
||||
|
||||
[Command line tools](./cli.md) can be used to record user interactions and generate C# code.
|
||||
</TabItem>
|
||||
<TabItem value="mstest">
|
||||
|
||||
```bash
|
||||
pwsh bin\Debug\netX\playwright.ps1 codegen
|
||||
dotnet test -- MSTest.Parallelize.Workers=5
|
||||
```
|
||||
|
||||
## Install browsers via API
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
It's possible to run [Command line tools](./cli.md) commands via the .NET API:
|
||||
See our doc on [Test Runners](./test-runners.md) to learn more about running tests in headed mode, running multiple tests, running specific configurations etc.
|
||||
|
||||
```csharp
|
||||
var exitCode = Microsoft.Playwright.Program.Main(new[] {"install"});
|
||||
if (exitCode != 0)
|
||||
{
|
||||
throw new Exception($"Playwright exited with code {exitCode}");
|
||||
}
|
||||
```
|
||||
## What's next
|
||||
|
||||
## Bundle drivers for different platforms
|
||||
|
||||
Playwright by default does bundle only the driver for the .NET publish target runtime. If you want to bundle for additional platforms, you can
|
||||
override this behavior by using either `all`, `none` or `linux`, `win`, `osx` in your project file.
|
||||
|
||||
```xml
|
||||
<PropertyGroup>
|
||||
<PlaywrightPlatform>all</PlaywrightPlatform>
|
||||
</PropertyGroup>
|
||||
```
|
||||
|
||||
or:
|
||||
|
||||
```xml
|
||||
<PropertyGroup>
|
||||
<PlaywrightPlatform>osx;linux</PlaywrightPlatform>
|
||||
</PropertyGroup>
|
||||
```
|
||||
|
||||
## System requirements
|
||||
|
||||
The browser binaries for Chromium, Firefox and WebKit work across the 3 platforms (Windows, macOS, Linux):
|
||||
|
||||
### Windows
|
||||
|
||||
Works with Windows and Windows Subsystem for Linux (WSL).
|
||||
|
||||
### macOS
|
||||
|
||||
Requires 11 (Big Sur) or above.
|
||||
|
||||
### Linux
|
||||
|
||||
Depending on your Linux distribution, you might need to install additional
|
||||
dependencies to run the browsers.
|
||||
|
||||
:::note
|
||||
Only Ubuntu 18.04, 20.04, and 22.04 are officially supported.
|
||||
:::
|
||||
|
||||
See also in the [Command line tools](./cli.md#install-system-dependencies)
|
||||
which has a command to install all necessary dependencies automatically for Ubuntu
|
||||
LTS releases.
|
||||
- [Write tests using web first assertions, page fixtures and locators](./writing-tests.md)
|
||||
- [Run single tests, multiple tests, headed mode](./running-tests.md)
|
||||
- [Learn more about the NUnit and MSTest base classes](./test-runners.md)
|
||||
- [Debug tests with the Playwright Debugger](./debug.md)
|
||||
- [Generate tests with Codegen](./codegen.md)
|
||||
- [See a trace of your tests](./trace-viewer.md)
|
||||
- [Using Playwright as library](./library.md)
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
---
|
||||
id: library
|
||||
title: "Getting started - Library"
|
||||
---
|
||||
|
||||
Playwright can either be used with the [NUnit](./test-runners.md#nunit) or [MSTest](./test-runners.md#mstest), or as a Playwright Library (this guide). If you are working on an application that utilizes Playwright capabilities or you are using Playwright with another test runner, read on.
|
||||
|
||||
## Usage
|
||||
|
||||
Create a console project and add the Playwright dependency.
|
||||
|
||||
```bash
|
||||
# Create project
|
||||
dotnet new console -n PlaywrightDemo
|
||||
cd PlaywrightDemo
|
||||
|
||||
# Add project dependency
|
||||
dotnet add package Microsoft.Playwright
|
||||
# Build the project
|
||||
dotnet build
|
||||
# Install required browsers - replace netX with actual output folder name, e.g. net6.0.
|
||||
pwsh bin\Debug\netX\playwright.ps1 install
|
||||
|
||||
# If the pwsh command does not work (throws TypeNotFound), make sure to use an up-to-date version of PowerShell.
|
||||
dotnet tool update --global PowerShell
|
||||
```
|
||||
|
||||
Create a `Program.cs` that will navigate to `https://playwright.dev/dotnet` and take a screenshot in Chromium.
|
||||
|
||||
```csharp
|
||||
using Microsoft.Playwright;
|
||||
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
await using var browser = await playwright.Chromium.LaunchAsync();
|
||||
var page = await browser.NewPageAsync();
|
||||
await page.GotoAsync("https://playwright.dev/dotnet");
|
||||
await page.ScreenshotAsync(new PageScreenshotOptions { Path = "screenshot.png" });
|
||||
```
|
||||
|
||||
Now run it.
|
||||
|
||||
```bash
|
||||
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 using var browser = await playwright.Firefox.LaunchAsync(new()
|
||||
{
|
||||
Headless = false,
|
||||
SlowMo = 50,
|
||||
});
|
||||
```
|
||||
|
||||
## Bundle drivers for different platforms
|
||||
|
||||
Playwright by default does bundle only the driver for the .NET publish target runtime. If you want to bundle for additional platforms, you can
|
||||
override this behavior by using either `all`, `none` or `linux`, `win`, `osx` in your project file.
|
||||
|
||||
```xml
|
||||
<PropertyGroup>
|
||||
<PlaywrightPlatform>all</PlaywrightPlatform>
|
||||
</PropertyGroup>
|
||||
```
|
||||
|
||||
or:
|
||||
|
||||
```xml
|
||||
<PropertyGroup>
|
||||
<PlaywrightPlatform>osx;linux</PlaywrightPlatform>
|
||||
</PropertyGroup>
|
||||
```
|
|
@ -0,0 +1,74 @@
|
|||
---
|
||||
id: running-tests
|
||||
title: "Running Tests"
|
||||
---
|
||||
|
||||
You can run a single test, a set of tests or all tests. Tests can be run on different browsers. By default tests are run in a headless manner meaning no browser window will be opened while running the tests and results will be seen in the terminal. If you prefer you can run your tests in headed mode by using the `headless` test run parameter.
|
||||
|
||||
- Running all tests
|
||||
|
||||
```bash
|
||||
dotnet test
|
||||
```
|
||||
|
||||
- Running a single test file
|
||||
|
||||
```bash
|
||||
dotnet test --filter "MyClassName"
|
||||
```
|
||||
|
||||
- Run a set of test files
|
||||
|
||||
```bash
|
||||
dotnet test --filter "MyClassName1|MyClassName2"
|
||||
```
|
||||
|
||||
- Run the test with the title
|
||||
|
||||
```bash
|
||||
dotnet test --filter "Name~TestMethod1"
|
||||
```
|
||||
|
||||
- Running Tests on specific browsers
|
||||
|
||||
```bash tab=bash-bash
|
||||
BROWSER=webkit dotnet test
|
||||
```
|
||||
|
||||
```batch tab=bash-batch
|
||||
set BROWSER=webkit
|
||||
dotnet test
|
||||
```
|
||||
|
||||
```powershell tab=bash-powershell
|
||||
$env:BROWSER="webkit"
|
||||
dotnet test
|
||||
```
|
||||
|
||||
- Running Tests on multiple browsers
|
||||
|
||||
To run your test on multiple browsers or configurations you need to invoke the `dotnet test` command multiple times. There you can then either specify the `BROWSER` environment variable (like the previous) or pass the `browser` via the runsettings file:
|
||||
|
||||
```bash
|
||||
dotnet test --settings:chromium.runsettings
|
||||
dotnet test --settings:firefox.runsettings
|
||||
dotnet test --settings:webkit.runsettings
|
||||
```
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RunSettings>
|
||||
<TestRunParameters>
|
||||
<Parameter name="browser" value="chromium" />
|
||||
<Parameter name="headless" value="false" />
|
||||
</TestRunParameters>
|
||||
</RunSettings>
|
||||
```
|
||||
|
||||
For more information see [selective unit tests](https://docs.microsoft.com/en-us/dotnet/core/testing/selective-unit-tests?pivots=mstest) in the Microsoft docs.
|
||||
|
||||
## What's Next
|
||||
|
||||
- [Debug tests with the Playwright Debugger](./debug.md)
|
||||
- [Generate tests with Codegen](./codegen.md)
|
||||
- [See a trace of your tests](./trace-viewer.md)
|
|
@ -108,6 +108,69 @@ By default NUnit will run all test files in parallel, while running tests inside
|
|||
|
||||
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.
|
||||
|
||||
### Customizing [BrowserContext] options
|
||||
|
||||
To customize context options, you can override the `ContextOptions` method of your test class derived from `Microsoft.Playwright.MSTest.PageTest` or `Microsoft.Playwright.MSTest.ContextTest`. See the following example:
|
||||
|
||||
```csharp
|
||||
using Microsoft.Playwright.NUnit;
|
||||
|
||||
namespace PlaywrightTests;
|
||||
|
||||
[Parallelizable(ParallelScope.Self)]
|
||||
public class MyTest : PageTest
|
||||
{
|
||||
[Test]
|
||||
public async Task TestWithCustomContextOptions()
|
||||
{
|
||||
// The following Page (and BrowserContext) instance has the custom colorScheme, viewport and baseURL set:
|
||||
await Page.GotoAsync("/login");
|
||||
}
|
||||
|
||||
public override BrowserNewContextOptions ContextOptions()
|
||||
{
|
||||
return new BrowserNewContextOptions()
|
||||
{
|
||||
ColorScheme = ColorScheme.Light,
|
||||
ViewportSize = new()
|
||||
{
|
||||
Width = 1920,
|
||||
Height = 1080
|
||||
},
|
||||
BaseURL = "https://github.com",
|
||||
};
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Customizing [Browser]/launch options
|
||||
|
||||
[Browser]/launch options can be override either using a run settings file or by setting the run settings options directly via the
|
||||
CLI. See the following example:
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RunSettings>
|
||||
<TestRunParameters>
|
||||
<Parameter name="browser" value="chromium" />
|
||||
<Parameter name="headless" value="false" />
|
||||
<Parameter name="channel" value="msedge" />
|
||||
</TestRunParameters>
|
||||
</RunSettings>
|
||||
```
|
||||
|
||||
```bash tab=bash-bash
|
||||
dotnet test -- TestRunParameters.Parameter\(name=\"browser\", value=\"chromium\"\) TestRunParameters.Parameter\(name=\"headless\", value=\"false\"\) TestRunParameters.Parameter\(name=\"channel\", value=\"msedge\"\)
|
||||
```
|
||||
|
||||
```batch tab=bash-batch
|
||||
dotnet test -- TestRunParameters.Parameter(name=\"browser\", value=\"chromium\") TestRunParameters.Parameter(name=\"headless\", value=\"false\") TestRunParameters.Parameter(name=\"channel\", value=\"msedge\")
|
||||
```
|
||||
|
||||
```powershell tab=bash-powershell
|
||||
dotnet test -- TestRunParameters.Parameter(name=\"browser\", value=\"chromium\") TestRunParameters.Parameter(name=\"headless\", value=\"false\") TestRunParameters.Parameter(name=\"channel\", value=\"msedge\")
|
||||
```
|
||||
|
||||
### Using Verbose API Logs
|
||||
|
||||
When you have enabled the [verbose API log](./debug.md#verbose-api-logs), via the `DEBUG` environment variable, you will see the messages in the standard error stream. In NUnit, within Visual Studio, that will be the `Tests` pane of the `Output` window. It will also be displayed in the `Test Log` for each test.
|
||||
|
@ -247,6 +310,73 @@ By default MSTest will run all classes in parallel, while running tests inside e
|
|||
dotnet test --settings:.runsettings -- MSTest.Parallelize.Workers=4
|
||||
```
|
||||
|
||||
### Customizing [BrowserContext] options
|
||||
|
||||
To customize context options, you can override the `ContextOptions` method of your test class derived from `Microsoft.Playwright.MSTest.PageTest` or `Microsoft.Playwright.MSTest.ContextTest`. See the following example:
|
||||
|
||||
```csharp
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Playwright;
|
||||
using Microsoft.Playwright.MSTest;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace PlaywrightTests;
|
||||
|
||||
[TestClass]
|
||||
public class UnitTest1 : PageTest
|
||||
{
|
||||
[TestMethod]
|
||||
public async Task TestWithCustomContextOptions()
|
||||
{
|
||||
// The following Page (and BrowserContext) instance has the custom colorScheme, viewport and baseURL set:
|
||||
await Page.GotoAsync("/login");
|
||||
}
|
||||
|
||||
public override BrowserNewContextOptions ContextOptions()
|
||||
{
|
||||
return new BrowserNewContextOptions()
|
||||
{
|
||||
ColorScheme = ColorScheme.Light,
|
||||
ViewportSize = new()
|
||||
{
|
||||
Width = 1920,
|
||||
Height = 1080
|
||||
},
|
||||
BaseURL = "https://github.com",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Customizing [Browser]/launch options
|
||||
|
||||
[Browser]/launch options can be override either using a run settings file or by setting the run settings options directly via the
|
||||
CLI. See the following example:
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RunSettings>
|
||||
<TestRunParameters>
|
||||
<Parameter name="browser" value="chromium" />
|
||||
<Parameter name="headless" value="false" />
|
||||
<Parameter name="channel" value="msedge" />
|
||||
</TestRunParameters>
|
||||
</RunSettings>
|
||||
```
|
||||
|
||||
```bash tab=bash-bash
|
||||
dotnet test -- TestRunParameters.Parameter\(name=\"browser\", value=\"chromium\"\) TestRunParameters.Parameter\(name=\"headless\", value=\"false\"\) TestRunParameters.Parameter\(name=\"channel\", value=\"msedge\"\)
|
||||
```
|
||||
|
||||
```batch tab=bash-batch
|
||||
dotnet test -- TestRunParameters.Parameter(name=\"browser\", value=\"chromium\") TestRunParameters.Parameter(name=\"headless\", value=\"false\") TestRunParameters.Parameter(name=\"channel\", value=\"msedge\")
|
||||
```
|
||||
|
||||
```powershell tab=bash-powershell
|
||||
dotnet test -- TestRunParameters.Parameter(name=\"browser\", value=\"chromium\") TestRunParameters.Parameter(name=\"headless\", value=\"false\") TestRunParameters.Parameter(name=\"channel\", value=\"msedge\")
|
||||
```
|
||||
|
||||
### Using Verbose API Logs
|
||||
|
||||
When you have enabled the [verbose API log](./debug.md#verbose-api-logs), via the `DEBUG` environment variable, you will see the messages in the standard error stream. In MSTest, within Visual Studio, that will be the `Tests` pane of the `Output` window. It will also be displayed in the `Test Log` for each test.
|
||||
|
|
|
@ -0,0 +1,238 @@
|
|||
---
|
||||
id: writing-tests
|
||||
title: "Writing Tests"
|
||||
---
|
||||
|
||||
Playwright assertions are created specifically for the dynamic web. Checks are automatically retried until the necessary conditions are met. Playwright comes with auto-wait built in meaning it waits for elements to be actionable prior to performing actions. Playwright provides the [Expect](./test-assertions) function to write assertions.
|
||||
|
||||
Take a look at the example test to see how to write a test using web first assertions, locators and selectors.
|
||||
|
||||
<Tabs
|
||||
defaultValue="nunit"
|
||||
values={[
|
||||
{label: 'NUnit', value: 'nunit'},
|
||||
{label: 'MSTest', value: 'mstest'}
|
||||
]
|
||||
}>
|
||||
<TabItem value="nunit">
|
||||
|
||||
```csharp
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.Playwright.NUnit;
|
||||
|
||||
namespace PlaywrightTests;
|
||||
|
||||
[Parallelizable(ParallelScope.Self)]
|
||||
public class Tests : PageTest
|
||||
{
|
||||
[Test]
|
||||
async public Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage()
|
||||
{
|
||||
await Page.GotoAsync("https://playwright.dev");
|
||||
|
||||
// Expect a title "to contain" a substring.
|
||||
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
|
||||
|
||||
// create a locator
|
||||
var getStarted = Page.Locator("text=Get Started");
|
||||
|
||||
// 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;
|
||||
using Microsoft.Playwright.MSTest;
|
||||
|
||||
namespace PlaywrightTests;
|
||||
|
||||
public class UnitTest1 : PageTest
|
||||
{
|
||||
[TestMethod]
|
||||
async public Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage()
|
||||
{
|
||||
await Page.GotoAsync("https://playwright.dev");
|
||||
|
||||
// Expect a title "to contain" a substring.
|
||||
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
|
||||
|
||||
// create a locator
|
||||
var getStarted = Page.Locator("text=Get Started");
|
||||
|
||||
// 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
|
||||
|
||||
[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 .click .fill etc. Custom locators can be created with the [`method: Page.locator`] method.
|
||||
|
||||
```csharp
|
||||
var getStarted = Page.Locator("text=Get Started");
|
||||
|
||||
await Expect(getStarted).ToHaveAttributeAsync("href", "/docs/installation");
|
||||
await getStarted.ClickAsync();
|
||||
```
|
||||
|
||||
[Selectors](./selectors.md) are strings that are used to create Locators. Playwright supports many different selectors like [Text](./selectors.md#text-selector), [CSS](./selectors.md#css-selector), [XPath](./selectors.md#xpath-selectors) and many more.
|
||||
|
||||
```csharp
|
||||
await Expect(Page.Locator("text=Installation")).ToBeVisibleAsync();
|
||||
```
|
||||
|
||||
|
||||
### 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
|
||||
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)]
|
||||
public class Tests : PageTest
|
||||
{
|
||||
[Test]
|
||||
public async Task BasicTest()
|
||||
{
|
||||
await Page.GotoAsync("https://playwright.dev");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
<TabItem value="mstest">
|
||||
|
||||
```csharp
|
||||
using Microsoft.Playwright.MSTest;
|
||||
|
||||
namespace PlaywrightTests;
|
||||
|
||||
public class UnitTest1 : PageTest
|
||||
{
|
||||
[TestMethod]
|
||||
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
|
||||
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)]
|
||||
public class Tests : PageTest
|
||||
{
|
||||
[Test]
|
||||
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
|
||||
using Microsoft.Playwright.MSTest;
|
||||
|
||||
namespace PlaywrightTests;
|
||||
|
||||
public class UnitTest1 : PageTest
|
||||
{
|
||||
[TestMethod]
|
||||
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
|
||||
|
||||
- [Run single tests, multiple tests, headed mode](./running-tests.md)
|
||||
- [Debug tests with the Playwright Debugger](./debug.md)
|
||||
- [Generate tests with Codegen](./codegen.md)
|
||||
- [See a trace of your tests](./trace-viewer.md)
|
|
@ -69,7 +69,7 @@ await expect(page).toHaveTitle(/Playwright/);
|
|||
|
||||
### Locators
|
||||
|
||||
[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 .click. fill etc. Custom locators can be created with the [`method: Page.locator`] method.
|
||||
[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 .click .fill etc. Custom locators can be created with the [`method: Page.locator`] method.
|
||||
|
||||
```js
|
||||
const getStarted = page.locator('text=Get Started');
|
||||
|
|
Загрузка…
Ссылка в новой задаче