编写测试
简介
Playwright 测试很简单,它们
- 执行操作,以及
- 根据预期断言状态。
无需在执行操作之前等待任何内容:Playwright 会自动等待各种可操作性检查通过,然后才会执行每个操作。
在执行检查时,也无需处理竞争条件 - Playwright 断言的设计方式使其描述了最终需要满足的预期。
就是这样!这些设计选择使 Playwright 用户能够完全忘记测试中不稳定的超时和竞争检查。
您将学习
第一个测试
查看以下示例,了解如何编写测试。
- MSTest
- NUnit
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Playwright;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace PlaywrightTests;
[Parallelizable(ParallelScope.Self)]
[TestFixture]
public class ExampleTest : PageTest
{
[Test]
public async Task HasTitle()
{
await Page.GotoAsync("https://playwright.net.cn");
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
}
[Test]
public async Task GetStartedLink()
{
await Page.GotoAsync("https://playwright.net.cn");
// Click the get started link.
await Page.GetByRole(AriaRole.Link, new() { Name = "Get started" }).ClickAsync();
// Expects page to have a heading with the name of Installation.
await Expect(Page.GetByRole(AriaRole.Heading, new() { Name = "Installation" })).ToBeVisibleAsync();
}
}
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Playwright;
using Microsoft.Playwright.MSTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace PlaywrightTests;
[TestClass]
public class ExampleTest : PageTest
{
[TestMethod]
public async Task HasTitle()
{
await Page.GotoAsync("https://playwright.net.cn");
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
}
[TestMethod]
public async Task GetStartedLink()
{
await Page.GotoAsync("https://playwright.net.cn");
// Click the get started link.
await Page.GetByRole(AriaRole.Link, new() { Name = "Get started" }).ClickAsync();
// Expects page to have a heading with the name of Installation.
await Expect(Page.GetByRole(AriaRole.Heading, new() { Name = "Installation" })).ToBeVisibleAsync();
}
}
操作
导航
大多数测试将从导航页面到 URL 开始。之后,测试将能够与页面元素交互。
await Page.GotoAsync("https://playwright.net.cn");
在继续之前,Playwright 将等待页面达到加载状态。了解有关Page.GotoAsync()选项的更多信息。
交互
执行操作始于定位元素。Playwright 使用定位器 API 来实现此目的。定位器表示随时查找页面上元素(复数)的一种方法,了解有关可用不同类型定位器的更多信息。在执行操作之前,Playwright 将等待元素可操作,因此无需等待其变为可用。
// Create a locator.
var getStarted = Page.GetByRole(AriaRole.Link, new() { Name = "Get started" });
// Click it.
await getStarted.ClickAsync();
在大多数情况下,它将在一行中编写
await Page.GetByRole(AriaRole.Link, new() { Name = "Get started" }).ClickAsync();
基本操作
这是最流行的 Playwright 操作列表。请注意,还有更多操作,因此请务必查看Locator API部分以了解有关它们的更多信息。
操作 | 描述 |
---|---|
Locator.CheckAsync() | 选中输入复选框 |
Locator.ClickAsync() | 单击元素 |
Locator.UncheckAsync() | 取消选中输入复选框 |
Locator.HoverAsync() | 将鼠标悬停在元素上 |
Locator.FillAsync() | 填充表单字段,输入文本 |
Locator.FocusAsync() | 聚焦元素 |
Locator.PressAsync() | 按单个键 |
Locator.SetInputFilesAsync() | 选择要上传的文件 |
Locator.SelectOptionAsync() | 在下拉列表中选择选项 |
断言
Playwright 提供了一个名为Expect 的异步函数来断言并在满足预期条件之前等待。
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
以下是大多数流行的异步断言列表。请注意,还有更多断言需要熟悉
测试隔离
Playwright NUnit 和 MSTest 测试框架基类将通过提供单独的Page
实例来彼此隔离每个测试。由于浏览器上下文,页面在测试之间是隔离的,这相当于一个全新的浏览器配置文件,其中每个测试都获得一个全新的环境,即使多个测试在一个浏览器中运行也是如此。
- MSTest
- NUnit
using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace PlaywrightTests;
[Parallelizable(ParallelScope.Self)]
[TestFixture]
public class ExampleTest : PageTest
{
[Test]
public async Task BasicTest()
{
await Page.GotoAsync("https://playwright.net.cn");
}
}
using System.Threading.Tasks;
using Microsoft.Playwright.MSTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace PlaywrightTests;
[TestClass]
public class ExampleTest : PageTest
{
[TestMethod]
public async Task BasicTest()
{
await Page.GotoAsync("https://playwright.net.cn");
}
}
使用测试钩子
您可以在 NUnit 中使用SetUp
/TearDown
或在 MSTest 中使用TestInitialize
/TestCleanup
来准备和清理测试环境
- MSTest
- NUnit
using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
namespace PlaywrightTests;
[Parallelizable(ParallelScope.Self)]
[TestFixture]
public class ExampleTest : PageTest
{
[Test]
public async Task MainNavigation()
{
// Assertions use the expect API.
await Expect(Page).ToHaveURLAsync("https://playwright.net.cn/");
}
[SetUp]
public async Task SetUp()
{
await Page.GotoAsync("https://playwright.net.cn");
}
}
using System.Threading.Tasks;
using Microsoft.Playwright.MSTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace PlaywrightTests;
[TestClass]
public class ExampleTest : PageTest
{
[TestMethod]
public async Task MainNavigation()
{
// Assertions use the expect API.
await Expect(Page).ToHaveURLAsync("https://playwright.net.cn/");
}
[TestInitialize]
public async Task TestInitialize()
{
await Page.GotoAsync("https://playwright.net.cn");
}
}