mcpField
W
write-e2e-tests

write-e2e-tests

github.com/tldrawテスト・品質51.1k432 回インストール今日更新
リポジトリ

write-e2e-tests について

Playwright で tldraw のサンプルと tldraw.com のエンドツーエンドテストを書くためのスキルです。フィクスチャ、エディター操作、UI のアサーション、セットアップ、プラットフォーム差異への対応を説明します。

主な機能

  1. 01Playwright テスト構成とフィクスチャ
  2. 02page.evaluate によるエディター操作
  3. 03ツールバーとメニューの操作パターン
  4. 04UI イベントと図形のアサーション
  5. 05プラットフォーム差異への対応

活用シーン

  1. 01エディターツールと図形作成のテスト
  2. 02メニュー操作と UI イベントの検証

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/tldraw/tldraw --skill write-e2e-tests

インストール後、Claude Code で/write-e2e-testsを入力して読み込まれたか確認します。

詳細

E2E テストの作成

E2E テストには Playwright を使用します。配置場所は apps/examples/e2e/(SDK の例)と apps/dotcom/client/e2e/(tldraw.com)です。

テストファイルの構成

apps/examples/e2e/
├── fixtures/
│   ├── fixtures.ts        # Test fixtures (toolbar, menus, etc.)
│   └── menus/             # Page object models
├── tests/
│   └── test-*.spec.ts     # Test files
└── shared-e2e.ts          # Shared utilities

テストファイルの名前は test-<feature>.spec.ts とします。

必須の宣言

page.evaluate() を使用してエディターや UI イベントにアクセスする場合:

import { Editor } from 'tldraw'

declare const editor: Editor
declare const __tldraw_ui_event: { name: string; data?: any }

基本的なテスト構成

import { expect } from '@playwright/test'
import test from '../fixtures/fixtures'
import { setupOrReset } from '../shared-e2e'

test.describe('Feature name', () => {
	test.beforeEach(setupOrReset)

	test('does something', async ({ page, toolbar }) => {
		// Test implementation
	})
})

セットアップのパターン

標準的なセットアップ(推奨)
test.beforeEach(setupOrReset) // Smart: navigates first run, fast reset after
パフォーマンスのための共有ページ

完全な分離を必要としないテストの場合:

let page: Page

test.describe('Feature', () => {
	test.beforeAll(async ({ browser }) => {
		page = await browser.newPage()
		await setupPage(page)
	})

	test.beforeEach(async () => {
		await hardResetEditor(page)
	})
})
シェイプを使ったセットアップ
import { setupPageWithShapes, hardResetWithShapes } from '../shared-e2e'

test.beforeEach(async ({ browser }) => {
	if (!page) {
		page = await browser.newPage()
		await setupPage(page)
	} else {
		await hardResetEditor(page)
	}
	await setupPageWithShapes(page)
})

利用可能なフィクスチャ

test('example', async ({
	page, // Playwright page
	toolbar, // Toolbar page object
	stylePanel, // Style panel
	actionsMenu, // Actions menu
	mainMenu, // Main menu
	pageMenu, // Page menu
	navigationPanel, // Navigation panel
	richTextToolbar, // Rich text toolbar
	api, // tldrawApi methods
	isMobile, // Mobile viewport check
	isMac, // Mac platform check
}) => {})

エディターの操作

page.evaluate 経由
// Execute code in browser context
await page.evaluate(() => {
	editor.createShapes([{ type: 'geo', x: 100, y: 100, props: { w: 100, h: 100 } }])
})

// Fast reset (faster than keyboard shortcuts)
await page.evaluate(() => {
	editor.selectAll().deleteShapes(editor.getSelectedShapeIds())
	editor.setCurrentTool('select')
})

// Get data from editor
const shape = await page.evaluate(() => editor.getOnlySelectedShape())
expect(shape).toMatchObject({ type: 'geo', x: 100, y: 100 })
UI イベントのテスト
await page.keyboard.press('Control+a')
expect(await page.evaluate(() => __tldraw_ui_event)).toMatchObject({
	name: 'select-all-shapes',
	data: { source: 'kbd' },
})

ツールと UI 要素の選択

テスト ID による指定
await page.getByTestId('tools.rectangle').click()
await page.getByTestId('tools.more.cloud').click() // In popover
await expect(page.getByTestId('tools.select')).toHaveAttribute('aria-pressed', 'true')
ツールバーフィクスチャ経由
const { select, draw, arrow, rectangle } = toolbar.tools
await rectangle.click()
await toolbar.isSelected(rectangle)
await toolbar.isNotSelected(select)

// More tools popover
await toolbar.moreToolsButton.click()
await toolbar.popOverTools.popoverCloud.click()

メニューの操作

import { clickMenu, withMenu } from '../shared-e2e'

// Click a menu item
await clickMenu(page, 'main-menu.edit.copy')
await clickMenu(page, 'context-menu.copy-as.copy-as-png')

// Focus and interact with menu item
await page.mouse.click(200, 200, { button: 'right' })
await withMenu(page, 'context-menu.arrange.distribute-horizontal', (item) => item.focus())
await page.keyboard.press('Enter')

データ駆動型テスト

const tools = [
	{ tool: 'rectangle', shape: 'geo' },
	{ tool: 'arrow', shape: 'arrow' },
	{ tool: 'draw', shape: 'draw' },
]

test('creates shapes with tools', async ({ page, toolbar }) => {
	for (const { tool, shape } of tools) {
		await page.getByTestId(`tools.${tool}`).click()
		await page.mouse.click(200, 200)
		expect(await getAllShapeTypes(page)).toContain(shape)

		// Reset for next iteration
		await page.evaluate(() => {
			editor.selectAll().deleteShapes(editor.getSelectedShapeIds())
		})
	}
})

プラットフォーム固有の処理

修飾キー
test('copy paste', async ({ page, isMac }) => {
	const modifier = isMac ? 'Meta' : 'Control'
	await page.keyboard.down(modifier)
	await page.keyboard.press('KeyC')
	await page.keyboard.press('KeyV')
	await page.keyboard.up(modifier)
})
モバイルでのスキップ
test('desktop only feature', async ({ isMobile }) => {
	if (isMobile) return
	// Desktop-specific test
})

ヘルパー関数

import { getAllShapeTypes, getAllShapeLabels, sleep, sleepFrames } from '../shared-e2e'

// Get shape types on canvas
const shapes = await getAllShapeTypes(page)
expect(shapes).toEqual(['geo', 'arrow'])

// Wait for async operations
await sleep(100)
await sleepFrames(2) // Wait for animation frames

アサーション

// Shape assertions
expect(await page.evaluate(() => editor.getOnlySelectedShape())).toMatchObject({
	type: 'geo',
	props: { w: 100, h: 100 },
})

// Attribute assertions
await expect(page.getByTestId('tools.select')).toHaveAttribute('aria-pressed', 'true')

// CSS assertions (for selection state)
await expect(tool).toHaveCSS('color', 'rgb(255, 255, 255)')

// Visibility
await expect(toolbar.moreToolsPopover).toBeVisible()
await expect(toolbar.toolLock).toBeHidden()

不安定なテストのスキップ

test.describe.skip('clipboard tests', () => {
	// Skipped because flaky in CI
})

test.skip('known issue', async () => {})

E2E テストの実行

pnpm e2e                    # Examples E2E
pnpm e2e-dotcom            # Dotcom E2E
pnpm e2e-ui                # With Playwright UI
pnpm e2e -- --grep "toolbar"  # Filter by pattern

主要なパターンのまとめ

  • テストの分離のために beforeEach で setupOrReset を使用する
  • page.evaluate() のために editor と __tldraw_ui_event を宣言する
  • 高速なエディター操作には page.evaluate() を使用する(キーボードより高速)
  • ツールの選択には tools.<name> パターンで getByTestId() を使用する
  • メニューの操作には clickMenu() / withMenu() を使用する
  • プラットフォームの違いは isMac と isMobile フィクスチャで処理する
  • localhost:5420/end-to-end の例に対してテストする

コメント