跳至主要内容

从Testing Library迁移

迁移原则

本指南描述了如何从DOM Testing LibraryReact Testing LibraryVue Testing LibrarySvelte Testing Library迁移到Playwright的Experimental Component Testing

note

如果您在浏览器中使用DOM Testing Library(例如,您使用webpack打包端到端测试),您可以直接切换到Playwright Test。下面的示例主要关注组件测试,但对于端到端测试,您只需将await mount替换为await page.goto('http://localhost:3000/')来打开待测试页面。

速查表

Testing LibraryPlaywright
screenpage and component
querieslocators
async helpersassertions
user eventsactions
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 />);

示例

测试库:

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代码片段中的内联注释):

  1. 对于组件测试,从@playwright/experimental-ct-react(或-vue、-svelte)导入所有内容;对于端到端测试,则从@playwright/test导入。
  2. 测试函数会获得一个与其他测试隔离的page,以及一个在该页面渲染组件的mount方法。这两个是Playwright Test中有用的fixtures
  3. render替换为返回组件定位器mount
  4. 使用通过locator.locator()page.locator()创建的定位器来执行大多数操作。
  5. 使用断言来验证状态。

迁移查询

所有类似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)上运行所有网页引擎(Chrome、Firefox、Safari)的测试
  • 全面支持多源站、(i)frames标签页和上下文
  • 在多个浏览器中以隔离方式并行运行测试
  • 内置测试artifact collection

你还将获得这些✨超棒工具✨,它们都随Playwright Test捆绑提供:

延伸阅读

了解更多关于Playwright测试运行器的信息: