跳到主要内容

从 Testing Library 迁移

迁移原则

本指南介绍如何从 DOM Testing LibraryReact Testing LibraryVue Testing LibrarySvelte Testing Library 迁移到 Playwright 的 实验性组件测试

注意

如果您在浏览器中使用 DOM Testing Library(例如,您使用 webpack 捆绑端到端测试),您可以直接切换到 Playwright Test。以下示例侧重于组件测试,但对于端到端测试,您只需将 await mount 替换为 await page.goto('https://127.0.0.1:3000/') 即可打开被测页面。

速查表

Testing LibraryPlaywright
screenpagecomponent
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 />);

示例

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

  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 支持
  • 所有 Web 引擎(Chrome、Firefox、Safari)和 任何流行的操作系统(Windows、macOS、Ubuntu)上运行测试
  • 完全支持多源、(i)框架选项卡和上下文
  • 跨多个浏览器并行隔离运行测试
  • 内置测试工件收集

您还可以获得与 Playwright Test 捆绑在一起的所有这些 ✨ 强大的工具 ✨

延伸阅读

了解有关 Playwright Test 运行器的更多信息