const path = require('path'); const { pathToFileURL } = require('url'); const { chromium } = require('playwright'); (async () => { const browser = await chromium.launch({ headless: true }); const page = await browser.newPage({ viewport: { width: 1000, height: 760 } }); await page.goto(pathToFileURL(path.join(process.cwd(), 'index.html')).href, { waitUntil: 'domcontentloaded' }); // Required controls: upload input, canvas, configurable watermark text. const upload = page.locator('[data-testid="image-upload"]'); if (!(await upload.count())) throw new Error('page should expose an image upload control'); const canvas = page.locator('[data-testid="watermark-canvas"], canvas'); if (!(await canvas.count())) throw new Error('a canvas is required to draw the watermarked image'); const wmText = page.locator('[data-testid="wm-text"]'); if (!(await wmText.count())) throw new Error('a configurable watermark text input is required'); // Default watermark text should be present (e.g. "Answer"). const defaultText = (await wmText.first().inputValue()) || ''; if (!defaultText.trim()) throw new Error('watermark text should have a sensible default'); // Canvas must be a usable size. const dims0 = await canvas.first().evaluate(c => ({ w: c.width, h: c.height })); // Upload a real, non-trivial source image (200x120) generated in-page, and // confirm the canvas is drawn (non-blank) and preserves the image aspect ratio. async function uploadGenerated() { await page.evaluate(async () => { const src = document.createElement('canvas'); src.width = 200; src.height = 120; const c = src.getContext('2d'); c.fillStyle = '#3b82f6'; c.fillRect(0, 0, 200, 120); c.fillStyle = '#f59e0b'; c.fillRect(20, 20, 80, 60); const blob = await new Promise(res => src.toBlob(res, 'image/png')); const file = new File([blob], 'src.png', { type: 'image/png' }); const input = document.querySelector('[data-testid="image-upload"]'); const dt = new DataTransfer(); dt.items.add(file); input.files = dt.files; input.dispatchEvent(new Event('change', { bubbles: true })); }); await page.waitForFunction(() => { const canvas = document.querySelector('[data-testid="watermark-canvas"]'); return window.__getImageStore().originals.length === 1 && canvas.width / canvas.height === 200 / 120; }, null, {timeout: 5000}); } await uploadGenerated(); const drawn = await canvas.first().evaluate(c => { const ctx = c.getContext('2d'); const data = ctx.getImageData(0, 0, c.width, c.height).data; let nonBlank = false; for (let i = 0; i < data.length; i += 4) { if (data[i] !== 0 || data[i + 1] !== 0 || data[i + 2] !== 0 || data[i + 3] !== 0) { nonBlank = true; break; } } return { w: c.width, h: c.height, nonBlank, url: c.toDataURL() }; }); if (!drawn.nonBlank) throw new Error('uploaded image should be drawn onto the canvas'); // Aspect ratio of the source (200:120) should be preserved on the canvas. const ratio = drawn.w / drawn.h; if (Math.abs(ratio - (200 / 120)) > 0.05) { throw new Error('canvas should preserve the original image aspect ratio, got ' + ratio.toFixed(3)); } // The watermark must actually be drawn: changing the watermark text must // change the rendered pixels (a no-op "image only" solution would not). await wmText.first().fill('WMTEST'); await page.waitForFunction(before => document.querySelector('[data-testid="watermark-canvas"]').toDataURL() !== before, drawn.url, {timeout: 5000}); const afterUrl = await canvas.first().evaluate(c => c.toDataURL()); if (afterUrl === drawn.url) { throw new Error('changing the watermark text should re-render the watermark on the canvas'); } await browser.close(); })().catch(err => { console.error(err && err.stack || err); process.exit(1); });