撰寫測試
簡介
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.dev");
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
}
[Test]
public async Task GetStartedLink()
{
await Page.GotoAsync("https://playwright.dev");
// 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.dev");
// Expect a title "to contain" a substring.
await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));
}
[TestMethod]
public async Task GetStartedLink()
{
await Page.GotoAsync("https://playwright.dev");
// 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();
}
}
Actions
導覽
大多數測試將從導航頁面到 URL 開始。之後,測試將能夠與頁面元素互動。
await Page.GotoAsync("https://playwright.dev");
Playwright 會等待頁面達到載入狀態後再繼續前進。了解更多關於 Page.GotoAsync() 選項。
互動
執行操作從定位元素開始。Playwright 使用 Locators API 來實現這一點。Locators 代表了一種在任何時刻在頁面上找到元素的方法,了解更多關於可用的 不同類型 的 locators。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 部分以了解更多資訊。
Action | Description |
---|---|
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"));
以下是最受歡迎的非同步斷言清單。請注意,還有更多可以熟悉:
Assertion | Description |
---|---|
Expect(Locator).ToBeCheckedAsync() | Checkbox 已勾選 |
Expect(Locator).ToBeEnabledAsync() | 控制項已啟用 |
Expect(Locator).ToBeVisibleAsync() | 元素可見 |
Expect(Locator).ToContainTextAsync() | 元素包含文字 |
Expect(Locator).ToHaveAttributeAsync() | 元素具有屬性 |
Expect(Locator).ToHaveCountAsync() | 元素列表具有給定的長度 |
Expect(Locator).ToHaveTextAsync() | 元素匹配文字 |
Expect(Locator).ToHaveValueAsync() | 輸入元素具有值 |
Expect(Page).ToHaveTitleAsync() | 頁面具有標題 |
Expect(Page).ToHaveURLAsync() | 頁面具有 URL |
測試隔離
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.dev");
}
}
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.dev");
}
}
使用測試掛鉤
您可以在 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.dev/");
}
[SetUp]
public async Task SetUp()
{
await Page.GotoAsync("https://playwright.dev");
}
}
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.dev/");
}
[TestInitialize]
public async Task TestInitialize()
{
await Page.GotoAsync("https://playwright.dev");
}
}