从 Testing Library 迁移
迁移原则
本指南介绍了从 DOM Testing Library、React Testing Library、Vue Testing Library 和 Svelte Testing Library 迁移到 Playwright 的实验性组件测试。
如果您在浏览器中使用 DOM Testing Library(例如,使用 webpack 打包端到端测试),您可以直接切换到 Playwright Test。下面的示例重点介绍组件测试,但对于端到端测试,您只需将 await mount
替换为 await page.goto('http://localhost:3000/')
来打开待测页面。
速查表
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 />); | mount(<Component />); |
const { unmount } = render(<Component />); | const { unmount } = await mount(<Component />); |
const { rerender } = render(<Component />); | const { update } = await mount(<Component />); |
示例
Testing Library
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
test('sign in', async () => {
// Setup the page.
const user = userEvent.setup();
render(<SignInPage />);
// Perform actions.
await user.type(screen.getByLabelText('Username'), 'John');
await user.type(screen.getByLabelText('Password'), 'secret');
await user.click(screen.getByRole('button', { name: 'Sign in' }));
// Verify signed in state by waiting until "Welcome" message appears.
expect(await screen.findByText('Welcome, John')).toBeInTheDocument();
});
逐行迁移到 Playwright Test
const { test, expect } = require('@playwright/experimental-ct-react'); // 1
test('sign in', async ({ mount }) => { // 2
// Setup the page.
const component = await mount(<SignInPage />); // 3
// Perform actions.
await component.getByLabel('Username').fill('John'); // 4
await component.getByLabel('Password').fill('secret');
await component.getByRole('button', { name: 'Sign in' }).click();
// Verify signed in state by waiting until "Welcome" message appears.
await expect(component.getByText('Welcome, John')).toBeVisible(); // 5
});
迁移要点 (参见 Playwright Test 代码片段中的内联注释)
- 对于组件测试,从
@playwright/experimental-ct-react
(或 -vue, -svelte) 导入所有内容;对于端到端测试,从@playwright/test
导入所有内容。 - 测试函数会接收一个与其他测试隔离的
page
,以及一个在此页面中渲染组件的mount
。这些是 Playwright Test 中两个有用的夹具。 - 将
render
替换为返回组件定位器的mount
。 - 使用通过 locator.locator() 或 page.locator() 创建的定位器来执行大部分动作。
- 使用断言来验证状态。
迁移 queries
所有类似 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 = document.getElementById('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)帧、选项卡和上下文
- 在多个浏览器中并行独立运行测试
- 内置的测试产物收集
您还将获得所有这些与 Playwright Test 捆绑在一起的 ✨ 超棒工具 ✨
- Visual Studio Code 集成
- UI 模式,用于调试测试,提供时间旅行体验和观察模式。
- Playwright Inspector
- Playwright Test 代码生成
- Playwright Tracing 用于事后调试
进一步阅读
了解更多关于 Playwright Test 测试运行器