从 Testing Library 迁移
迁移原则
本指南描述了从 DOM Testing Library、React Testing Library 和 Vue Testing Library 迁移到 Playwright 的组件测试。
INFO
如果你在浏览器中使用 DOM Testing Library(例如,你使用 webpack 打包端到端测试),你可以直接切换到 Playwright Test。下面的示例专注于组件测试,但对于端到端测试,你只需要将 await mount 替换为 await page.goto('http://localhost:3000/') 来打开被测页面。
Playwright 通过 story 渲染组件——一个将组件嵌入特定场景的小型包装器——由你自己的开发服务器从 gallery 页面提供服务。Testing Library 在测试中内联调用 render(),而 Playwright 将该设置移到 story 中,并在测试中通过 id 引用它。请参阅组件测试了解如何设置 gallery。
速查表
| Testing Library | Playwright |
|---|---|
| screen | page 和 component |
| queries | locators |
| async helpers | assertions |
| user events | actions |
await user.click(screen.getByText('Click me')) | await component.getByText('Click me').click() |
await user.click(await screen.findByText('Click me')) | await component.getByText('Click me').click() |
await user.type(screen.getByLabelText('Password'), 'secret') | await component.getByLabel('Password').fill('secret') |
expect(screen.getByLabelText('Password')).toHaveValue('secret') | await expect(component.getByLabel('Password')).toHaveValue('secret') |
screen.getByRole('button', { pressed: true }) | component.getByRole('button', { pressed: true }) |
screen.getByLabelText('...') | component.getByLabel('...') |
screen.queryByPlaceholderText('...') | component.getByPlaceholder('...') |
screen.findByText('...') | component.getByText('...') |
screen.getByTestId('...') | component.getByTestId('...') |
render(<Component />); | story 导出 + await mount('Component/Default'); |
const { unmount } = render(<Component />); | const component = await mount('...'); await component.unmount(); |
const { rerender } = render(<Component />); | const component = await mount('...'); await component.update(props); |
示例
Testing Library:
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
test('sign in', async () => {
// 设置页面。
const user = userEvent.setup();
render(<SignInPage />);
// 执行操作。
await user.type(screen.getByLabelText('Username'), 'John');
await user.type(screen.getByLabelText('Password'), 'secret');
await user.click(screen.getByRole('button', { name: 'Sign in' }));
// 通过等待"欢迎"消息出现来验证登录状态。
expect(await screen.findByText('Welcome, John')).toBeInTheDocument();
});逐行迁移到 Playwright Test。首先,将场景从测试移到组件旁边的 story 中:
import { SignInPage } from './SignInPage';
export const Default = () => <SignInPage />; // 1然后测试通过 id 挂载该 story:
const { test, expect } = require('@playwright/test'); // 2
test('sign in', async ({ mount }) => { // 3
// 设置页面。
const component = await mount('pages/SignInPage/Default'); // 4
// 执行操作。
await component.getByLabel('Username').fill('John'); // 5
await component.getByLabel('Password').fill('secret');
await component.getByRole('button', { name: 'Sign in' }).click();
// 通过等待"欢迎"消息出现来验证登录状态。
await expect(component.getByText('Welcome, John')).toBeVisible(); // 6
});迁移要点(请参阅 Playwright Test 代码片段中的内联注释):
- 无论
render()以前内联设置什么——props、providers、mock 数据——都变成 story 导出。Story 在浏览器中运行,因此活动对象不再需要传递到测试中。 - 从
@playwright/test导入所有内容,适用于组件测试和端到端测试。 - 测试函数会获得一个与其他测试隔离的
page,以及在此页面中渲染 story 的mount。这是 Playwright Test 中许多有用的 fixtures 中的两个。 - 将
render替换为fixtures.mount(),它接受一个 story id 并返回一个作用域为 gallery 根的组件定位器。 - 使用通过
locator.locator()或page.locator()创建的定位器来执行大多数操作。 - 使用断言来验证状态。
迁移查询
所有像 getBy...、findBy...、queryBy... 及其多元素对应方法的查询都被 component.getBy... 定位器替代。定位器始终自动等待并在需要时重试,因此你不必担心选择正确的方法。当你想执行列表操作时,例如断言文本列表,Playwright 会自动执行多元素操作。
替换 waitFor
Playwright 包含会自动等待条件满足的断言,因此你通常不需要显式的 waitFor/waitForElementToBeRemoved 调用。
// Testing Library
await waitFor(() => {
expect(getByText('the lion king')).toBeInTheDocument();
});
await waitForElementToBeRemoved(() => queryByText('the mummy'));
// Playwright
await expect(page.getByText('the lion king')).toBeVisible();
await expect(page.getByText('the mummy')).toBeHidden();当你找不到合适的断言时,使用 expect.poll 代替。
await expect.poll(async () => {
const response = await page.request.get('https://api.example.com');
return response.status();
}).toBe(200);替换 within
你可以使用 locator.locator() 方法在另一个定位器内部创建定位器。
// Testing Library
const messages = screen.getByTestId('messages');
const helloMessage = within(messages).getByText('hello');
// Playwright
const messages = component.getByTestId('messages');
const helloMessage = messages.getByText('hello');Playwright Test 的超强能力
一旦使用 Playwright Test,你将获得很多功能!
- 完全零配置的 TypeScript 支持
- 在任何流行操作系统(Windows、macOS、Ubuntu)上的所有 Web 引擎(Chrome、Firefox、Safari)上运行测试
- 完全支持多源、(i)frames、标签页和上下文
- 在多个浏览器上并行隔离运行测试
- 内置测试产物收集
你还将获得与 Playwright Test 捆绑在一起的所有这些精彩工具:
- Visual Studio Code 集成
- UI 模式用于调试测试,具有时间旅行体验和监视模式。
- Playwright Inspector
- Playwright Test 代码生成
- Playwright Tracing 用于事后调试
延伸阅读
了解更多关于 Playwright Test 运行器的信息: