从 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('https://:3000/')
即可打开被测页面。
速查表
Testing Library | Playwright |
---|---|
screen | page 和 component |
查询 | 定位器 |
异步辅助函数 | 断言 |
用户事件 | 操作 |
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() 创建的定位器来执行大多数操作。
- 使用 断言 来验证状态。
迁移查询
所有像 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 支持
- 在所有 Web 引擎(Chrome、Firefox、Safari)和任何流行操作系统(Windows、macOS、Ubuntu)上运行测试
- 完全支持多源、(i)帧、选项卡和上下文
- 在多个浏览器中并行隔离运行测试
- 内置测试工件收集
您还将获得所有这些随 Playwright Test 捆绑的 ✨ 强大工具 ✨
- Visual Studio Code 集成
- UI 模式,用于调试测试,具有时间旅行体验,并配有监视模式。
- Playwright Inspector
- Playwright Test 代码生成
- Playwright 跟踪 用于事后调试
进一步阅读
了解有关 Playwright Test 运行器的更多信息