Skip to content

AI-native design editor. Open-source Figma alternative.

Playwright visual regression testing with Figma CDP reference

Danila Poyarkov40f7b3a64846
diff --git a/.gitignore b/.gitignore
index 0d0f8fc24..954717375 100644
--- a/.gitignore
+++ b/.gitignore
@@ -25,3 +25,4 @@ public/canvaskit.wasm
*.njsproj
*.sln
*.sw?
+test-results/
diff --git a/bun.lock b/bun.lock
index 3d0837dac..07642febb 100644
--- a/bun.lock
+++ b/bun.lock
@@ -19,6 +19,7 @@
"yoga-layout": "^3.2.1",
},
"devDependencies": {
+ "@playwright/test": "^1.58.2",
"@tauri-apps/cli": "^2",
"@typescript/native-preview": "^7.0.0-dev.20260227.1",
"@vitejs/plugin-vue": "^6.0.4",
@@ -191,6 +192,8 @@
"@oxlint/binding-win32-x64-msvc": ["@oxlint/[email protected]", "", { "os": "win32", "cpu": "x64" }, "sha512-682t7npLC4G2Ca+iNlI9fhAKTcFPYYXJjwoa88H4q+u5HHHlsnL/gHULapX3iqp+A8FIJbgdylL5KMYo2LaluQ=="],
+ "@playwright/test": ["@playwright/[email protected]", "", { "dependencies": { "playwright": "1.58.2" }, "bin": { "playwright": "cli.js" } }, "sha512-akea+6bHYBBfA9uQqSYmlJXn61cTa+jbO87xVLCWbTqbWadRVmhxlXATaOjOgcBaWU4ePo0wB41KMFv3o35IXA=="],
+
"@rolldown/pluginutils": ["@rolldown/[email protected]", "", {}, "sha512-izyXV/v+cHiRfozX62W9htOAvwMo4/bXKDrQ+vom1L1qRuexPock/7VZDAhnpHCLNejd3NJ6hiab+tO0D44Rgw=="],
"@rollup/rollup-android-arm-eabi": ["@rollup/[email protected]", "", { "os": "android", "cpu": "arm" }, "sha512-upnNBkA6ZH2VKGcBj9Fyl9IGNPULcjXRlg0LLeaioQWueH30p6IXtJEbKAgvyv+mJaMxSm1l6xwDXYjpEMiLMg=="],
@@ -425,6 +428,10 @@
"picomatch": ["[email protected]", "", {}, "sha512-5gTmgEY/sqK6gFXLIsQNH19lWb4ebPDLA4SdLP7dsWkIXHWlG66oPuVvXSGFPppYZz8ZDZq0dYYrbHfBCVUb1Q=="],
+ "playwright": ["[email protected]", "", { "dependencies": { "playwright-core": "1.58.2" }, "optionalDependencies": { "fsevents": "2.3.2" }, "bin": { "playwright": "cli.js" } }, "sha512-vA30H8Nvkq/cPBnNw4Q8TWz1EJyqgpuinBcHET0YVJVFldr8JDNiU9LaWAE1KqSkRYazuaBhTpB5ZzShOezQ6A=="],
+
+ "playwright-core": ["[email protected]", "", { "bin": { "playwright-core": "cli.js" } }, "sha512-yZkEtftgwS8CsfYo7nm0KE8jsvm6i/PTgVtB8DL726wNf6H2IMsDuxCpJj59KDaxCtSnrWan2AeDqM7JBaultg=="],
+
"postcss": ["[email protected]", "", { "dependencies": { "nanoid": "^3.3.11", "picocolors": "^1.1.1", "source-map-js": "^1.2.1" } }, "sha512-3Ybi1tAuwAP9s0r1UQ2J4n5Y0G05bJkpUIO0/bI9MhwmD70S5aTWbXGBwxHrelT+XM1k6dM0pk+SwNkpTRN7Pg=="],
"reka-ui": ["[email protected]", "", { "dependencies": { "@floating-ui/dom": "^1.6.13", "@floating-ui/vue": "^1.1.6", "@internationalized/date": "^3.5.0", "@internationalized/number": "^3.5.0", "@tanstack/vue-virtual": "^3.12.0", "@vueuse/core": "^14.1.0", "@vueuse/shared": "^14.1.0", "aria-hidden": "^1.2.4", "defu": "^6.1.4", "ohash": "^2.0.11" }, "peerDependencies": { "vue": ">= 3.2.0" } }, "sha512-8lTKcJhmG+D3UyJxhBnNnW/720sLzm0pbA9AC1MWazmJ5YchJAyTSl+O00xP/kxBmEN0fw5JqWVHguiFmsGjzA=="],
@@ -464,5 +471,7 @@
"@tailwindcss/oxide-wasm32-wasi/@tybys/wasm-util": ["@tybys/[email protected]", "", { "dependencies": { "tslib": "^2.4.0" }, "bundled": true }, "sha512-9tTaPJLSiejZKx+Bmog4uSubteqTvFrVrURwkmHixBo0G4seD0zUxp98E1DzUBJxLQ3NPwXrGKDiVjwx/DpPsg=="],
"@tailwindcss/oxide-wasm32-wasi/tslib": ["[email protected]", "", { "bundled": true }, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="],
+
+ "playwright/fsevents": ["[email protected]", "", { "os": "darwin" }, "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA=="],
}
}
diff --git a/package.json b/package.json
index 671ed9d6c..9a0c91b8d 100644
--- a/package.json
+++ b/package.json
@@ -11,7 +11,11 @@
"lint": "oxlint -c oxlint.json src/",
"format": "oxfmt --write src/",
"typecheck": "tsgo --noEmit",
- "check": "bun run lint && bun run typecheck"
+ "check": "bun run lint && bun run typecheck",
+ "test": "playwright test --project=openpencil",
+ "test:update": "playwright test --project=openpencil --update-snapshots",
+ "test:figma": "playwright test --project=figma",
+ "figma:debug": "open -a Figma --args --remote-debugging-port=9222"
},
"dependencies": {
"@tailwindcss/vite": "^4.2.1",
@@ -28,6 +32,7 @@
"yoga-layout": "^3.2.1"
},
"devDependencies": {
+ "@playwright/test": "^1.58.2",
"@tauri-apps/cli": "^2",
"@typescript/native-preview": "^7.0.0-dev.20260227.1",
"@vitejs/plugin-vue": "^6.0.4",
@@ -39,4 +44,4 @@
"patchedDependencies": {
}
-}
+}
\ No newline at end of file
diff --git a/playwright.config.ts b/playwright.config.ts
new file mode 100644
index 000000000..31b6487a9
--- /dev/null
+++ b/playwright.config.ts
@@ -0,0 +1,35 @@
+import { defineConfig } from '@playwright/test'
+
+export default defineConfig({
+ testDir: './tests',
+ timeout: 15_000,
+ expect: {
+ toHaveScreenshot: {
+ maxDiffPixelRatio: 0.01,
+ threshold: 0.3
+ }
+ },
+ use: {
+ baseURL: 'http://localhost:1420',
+ viewport: { width: 1280, height: 800 },
+ deviceScaleFactor: 2,
+ launchOptions: {
+ args: ['--enable-unsafe-swiftshader']
+ }
+ },
+ projects: [
+ {
+ name: 'openpencil',
+ testDir: './tests/e2e'
+ },
+ {
+ name: 'figma',
+ testDir: './tests/figma'
+ }
+ ],
+ webServer: {
+ command: 'bun run dev',
+ port: 1420,
+ reuseExistingServer: true
+ }
+})
diff --git a/src/App.vue b/src/App.vue
index 2539c7a67..b3574dd9c 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -16,7 +16,10 @@ useEventListener(document, 'wheel', (e: WheelEvent) => {
if (e.ctrlKey || e.metaKey) e.preventDefault()
}, { passive: false })
+const isTestMode = new URLSearchParams(window.location.search).has('test')
+
// Demo shapes
+if (!isTestMode) {
store.createShape('FRAME', 100, 80, 800, 500)
store.graph.updateNode(store.graph.getChildren(store.graph.rootId)[0].id, {
name: 'Desktop',
@@ -31,6 +34,7 @@ store.graph.updateNode(store.graph.getChildren(store.graph.rootId)[0].id, {
}
]
})
+}
const demo = [
{
@@ -74,7 +78,8 @@ const demo = [
}
]
-for (const d of demo) {
+if (!isTestMode)
+ for (const d of demo) {
const id = store.createShape(d.type, d.x, d.y, d.w, d.h)
store.graph.updateNode(id, {
name: d.name,
diff --git a/tests/e2e/create-shapes.spec.ts b/tests/e2e/create-shapes.spec.ts
new file mode 100644
index 000000000..10d0d2d4f
--- /dev/null
+++ b/tests/e2e/create-shapes.spec.ts
@@ -0,0 +1,47 @@
+import { test, expect } from '@playwright/test'
+
+import { CanvasHelper } from '../helpers/canvas'
+
+test.describe('create shapes', () => {
+ let canvas: CanvasHelper
+
+ test.beforeEach(async ({ page }) => {
+ await page.goto('/?test')
+ canvas = new CanvasHelper(page)
+ await canvas.waitForInit()
+ })
+
+ test('empty canvas', async () => {
+ await expect(canvas.canvas).toHaveScreenshot({ timeout: 3000 })
+ })
+
+ test('draw rectangle', async () => {
+ await canvas.drawRect(100, 100, 200, 150)
+ await expect(canvas.canvas).toHaveScreenshot({ timeout: 3000 })
+ })
+
+ test('draw ellipse', async () => {
+ await canvas.drawEllipse(100, 100, 200, 150)
+ await expect(canvas.canvas).toHaveScreenshot({ timeout: 3000 })
+ })
+
+ test('draw rectangle then move it', async () => {
+ await canvas.drawRect(100, 100, 200, 150)
+ await canvas.selectTool('select')
+ await canvas.drag(200, 175, 400, 300)
+ await canvas.waitForRender()
+ await expect(canvas.canvas).toHaveScreenshot({ timeout: 3000 })
+ })
+
+ test('draw and delete', async () => {
+ await canvas.drawRect(100, 100, 200, 150)
+ await canvas.deleteSelection()
+ await expect(canvas.canvas).toHaveScreenshot({ timeout: 3000 })
+ })
+
+ test('draw and undo', async () => {
+ await canvas.drawRect(100, 100, 200, 150)
+ await canvas.undo()
+ await expect(canvas.canvas).toHaveScreenshot({ timeout: 3000 })
+ })
+})
diff --git a/tests/e2e/create-shapes.spec.ts-snapshots/create-shapes-draw-and-delete-1-openpencil-darwin.png b/tests/e2e/create-shapes.spec.ts-snapshots/create-shapes-draw-and-delete-1-openpencil-darwin.png
new file mode 100644
index 000000000..a96c28bf1
Binary files /dev/null and b/tests/e2e/create-shapes.spec.ts-snapshots/create-shapes-draw-and-delete-1-openpencil-darwin.png differ
diff --git a/tests/e2e/create-shapes.spec.ts-snapshots/create-shapes-draw-and-undo-1-openpencil-darwin.png b/tests/e2e/create-shapes.spec.ts-snapshots/create-shapes-draw-and-undo-1-openpencil-darwin.png
new file mode 100644
index 000000000..a96c28bf1
Binary files /dev/null and b/tests/e2e/create-shapes.spec.ts-snapshots/create-shapes-draw-and-undo-1-openpencil-darwin.png differ
diff --git a/tests/e2e/create-shapes.spec.ts-snapshots/create-shapes-draw-ellipse-1-openpencil-darwin.png b/tests/e2e/create-shapes.spec.ts-snapshots/create-shapes-draw-ellipse-1-openpencil-darwin.png
new file mode 100644
index 000000000..b615cdea1
Binary files /dev/null and b/tests/e2e/create-shapes.spec.ts-snapshots/create-shapes-draw-ellipse-1-openpencil-darwin.png differ
diff --git a/tests/e2e/create-shapes.spec.ts-snapshots/create-shapes-draw-rectangle-1-openpencil-darwin.png b/tests/e2e/create-shapes.spec.ts-snapshots/create-shapes-draw-rectangle-1-openpencil-darwin.png
new file mode 100644
index 000000000..5d0aab530
Binary files /dev/null and b/tests/e2e/create-shapes.spec.ts-snapshots/create-shapes-draw-rectangle-1-openpencil-darwin.png differ
diff --git a/tests/e2e/create-shapes.spec.ts-snapshots/create-shapes-draw-rectangle-then-move-it-1-openpencil-darwin.png b/tests/e2e/create-shapes.spec.ts-snapshots/create-shapes-draw-rectangle-then-move-it-1-openpencil-darwin.png
new file mode 100644
index 000000000..4a382df4e
Binary files /dev/null and b/tests/e2e/create-shapes.spec.ts-snapshots/create-shapes-draw-rectangle-then-move-it-1-openpencil-darwin.png differ
diff --git a/tests/e2e/create-shapes.spec.ts-snapshots/create-shapes-empty-canvas-1-openpencil-darwin.png b/tests/e2e/create-shapes.spec.ts-snapshots/create-shapes-empty-canvas-1-openpencil-darwin.png
new file mode 100644
index 000000000..a96c28bf1
Binary files /dev/null and b/tests/e2e/create-shapes.spec.ts-snapshots/create-shapes-empty-canvas-1-openpencil-darwin.png differ
diff --git a/tests/figma/create-shapes.spec.ts b/tests/figma/create-shapes.spec.ts
new file mode 100644
index 000000000..ab03e22c1
--- /dev/null
+++ b/tests/figma/create-shapes.spec.ts
@@ -0,0 +1,30 @@
+import { test, expect } from '@playwright/test'
+
+import { FigmaHelper } from '../helpers/figma'
+
+test.describe('figma reference: create shapes', () => {
+ let figma: FigmaHelper
+
+ test.beforeAll(async () => {
+ figma = new FigmaHelper()
+ await figma.connect()
+ })
+
+ test.afterAll(async () => {
+ await figma.disconnect()
+ })
+
+ test.beforeEach(async () => {
+ await figma.deleteSelection()
+ await figma.waitForRender()
+ })
+
+ test('empty canvas', async () => {
+ await expect(figma.canvas).toHaveScreenshot()
+ })
+
+ test('draw rectangle', async () => {
+ await figma.drawRect(200, 200, 200, 150)
+ await expect(figma.canvas).toHaveScreenshot()
+ })
+})
diff --git a/tests/helpers/canvas.ts b/tests/helpers/canvas.ts
new file mode 100644
index 000000000..de77dc2a0
--- /dev/null
+++ b/tests/helpers/canvas.ts
@@ -0,0 +1,100 @@
+import type { Page, Locator } from '@playwright/test'
+
+export class CanvasHelper {
+ readonly page: Page
+ readonly canvas: Locator
+
+ constructor(page: Page) {
+ this.page = page
+ this.canvas = page.locator('canvas')
+ }
+
+ async waitForRender() {
+ await this.page.waitForTimeout(500)
+ }
+
+ async waitForInit() {
+ await this.canvas.waitFor({ state: 'visible', timeout: 5000 })
+ await this.page.waitForTimeout(1000)
+ }
+
+ async screenshotCanvas() {
+ return this.canvas.screenshot()
+ }
+
+ private async canvasBounds() {
+ return this.canvas.boundingBox().then((b) => b!)
+ }
+
+ async click(canvasX: number, canvasY: number) {
+ const box = await this.canvasBounds()
+ await this.page.mouse.click(box.x + canvasX, box.y + canvasY)
+ }
+
+ async drag(
+ fromX: number,
+ fromY: number,
+ toX: number,
+ toY: number,
+ steps = 10
+ ) {
+ const box = await this.canvasBounds()
+ await this.page.mouse.move(box.x + fromX, box.y + fromY)
+ await this.page.mouse.down()
+ await this.page.mouse.move(box.x + toX, box.y + toY, { steps })
+ await this.page.mouse.up()
+ }
+
+ async pressKey(key: string) {
+ await this.page.keyboard.press(key)
+ }
+
+ async drawRect(x: number, y: number, width: number, height: number) {
+ await this.pressKey('r')
+ await this.drag(x, y, x + width, y + height)
+ await this.waitForRender()
+ }
+
+ async drawEllipse(x: number, y: number, width: number, height: number) {
+ await this.pressKey('o')
+ await this.drag(x, y, x + width, y + height)
+ await this.waitForRender()
+ }
+
+ async selectTool(tool: 'select' | 'frame' | 'rectangle' | 'ellipse' | 'text' | 'pen' | 'hand') {
+ const keys: Record<string, string> = {
+ select: 'v',
+ frame: 'f',
+ rectangle: 'r',
+ ellipse: 'o',
+ text: 't',
+ pen: 'p',
+ hand: 'h'
+ }
+ await this.pressKey(keys[tool])
+ }
+
+ async deleteSelection() {
+ await this.pressKey('Backspace')
+ await this.waitForRender()
+ }
+
+ async undo() {
+ await this.pressKey('Meta+z')
+ await this.waitForRender()
+ }
+
+ async redo() {
+ await this.pressKey('Meta+Shift+z')
+ await this.waitForRender()
+ }
+
+ async selectAll() {
+ await this.pressKey('Meta+a')
+ }
+
+ async duplicate() {
+ await this.pressKey('Meta+d')
+ await this.waitForRender()
+ }
+}
diff --git a/tests/helpers/figma.ts b/tests/helpers/figma.ts
new file mode 100644
index 000000000..ec8fafaca
--- /dev/null
+++ b/tests/helpers/figma.ts
@@ -0,0 +1,86 @@
+import { chromium, type Browser, type Page, type BrowserContext } from '@playwright/test'
+
+const FIGMA_CDP_PORT = 9222
+
+export class FigmaHelper {
+ private browser!: Browser
+ private context!: BrowserContext
+ page!: Page
+
+ async connect() {
+ this.browser = await chromium.connectOverCDP(`http://localhost:${FIGMA_CDP_PORT}`)
+ const contexts = this.browser.contexts()
+ if (contexts.length === 0) throw new Error('No Figma contexts found')
+ this.context = contexts[0]
+
+ const pages = this.context.pages()
+ const editorPage = pages.find((p) => p.url().includes('figma.com/design'))
+ if (!editorPage) {
+ throw new Error(
+ `No Figma editor page found. Open a file in Figma first.\nPages: ${pages.map((p) => p.url()).join(', ')}`
+ )
+ }
+ this.page = editorPage
+ }
+
+ async disconnect() {
+ await this.browser?.close()
+ }
+
+ get canvas() {
+ return this.page.locator('canvas.gpu-view-content')
+ }
+
+ async screenshotCanvas() {
+ return this.canvas.screenshot()
+ }
+
+ private async canvasBounds() {
+ return this.canvas.boundingBox().then((b) => b!)
+ }
+
+ async waitForRender() {
+ await this.page.waitForTimeout(500)
+ }
+
+ async click(canvasX: number, canvasY: number) {
+ const box = await this.canvasBounds()
+ await this.page.mouse.click(box.x + canvasX, box.y + canvasY)
+ }
+
+ async drag(
+ fromX: number,
+ fromY: number,
+ toX: number,
+ toY: number,
+ steps = 10
+ ) {
+ const box = await this.canvasBounds()
+ await this.page.mouse.move(box.x + fromX, box.y + fromY)
+ await this.page.mouse.down()
+ await this.page.mouse.move(box.x + toX, box.y + toY, { steps })
+ await this.page.mouse.up()
+ }
+
+ async pressKey(key: string) {
+ await this.page.keyboard.press(key)
+ }
+
+ async drawRect(x: number, y: number, width: number, height: number) {
+ await this.pressKey('r')
+ await this.waitForRender()
+ await this.drag(x, y, x + width, y + height)
+ await this.waitForRender()
+ }
+
+ async selectAll() {
+ await this.pressKey('Meta+a')
+ await this.waitForRender()
+ }
+
+ async deleteSelection() {
+ await this.selectAll()
+ await this.pressKey('Backspace')
+ await this.waitForRender()
+ }
+}