|
1 | | -import { t, ClientFunction } from 'testcafe' |
2 | | -import * as fs from 'node:fs' |
3 | | - |
4 | | -const script = fs.readFileSync('./tap/run-browser.js', { encoding: 'utf-8' }) |
5 | | - |
6 | | -fixture('test suite').page('https://important-clam-66.deno.dev') |
7 | | - |
8 | | -test('passes tests', async (user) => { |
9 | | - await ClientFunction( |
10 | | - () => { |
11 | | - function escapeHTML(str: string) { |
12 | | - return str |
13 | | - .replace(/&/g, '&') |
14 | | - .replace(/</g, '<') |
15 | | - .replace(/>/g, '>') |
16 | | - .replace(/"/g, '"') |
17 | | - .replace(/'/g, ''') |
18 | | - } |
19 | | - // @ts-expect-error |
20 | | - document.getElementById('js').innerHTML = escapeHTML(innerHTML) |
21 | | - document.querySelector('form')?.submit() |
22 | | - }, |
23 | | - { dependencies: { innerHTML: script } }, |
24 | | - )() |
25 | | - |
26 | | - let i = 0 |
27 | | - const interval = setInterval(() => { |
28 | | - t.getBrowserConsoleMessages().then(({ log: messages }) => { |
29 | | - messages.forEach((message, index) => { |
30 | | - if (i && index <= i) return |
31 | | - i++ |
32 | | - console.log(message) |
33 | | - }) |
34 | | - }) |
35 | | - }, 100) |
| 1 | +import { test, expect } from '@playwright/test' |
| 2 | +import { createServer } from 'node:http' |
| 3 | +import { readFileSync } from 'node:fs' |
| 4 | + |
| 5 | +const script = readFileSync('./tap/run-browser.js', 'utf-8') |
| 6 | + |
| 7 | +test('passes tests', async ({ page }) => { |
| 8 | + const server = createServer((req, res) => { |
| 9 | + if (req.url === '/run-browser.js') { |
| 10 | + res.writeHead(200, { 'Content-Type': 'application/javascript' }) |
| 11 | + res.end(script) |
| 12 | + } else { |
| 13 | + res.writeHead(200, { 'Content-Type': 'text/html' }) |
| 14 | + res.end( |
| 15 | + '<!DOCTYPE html><html><head></head><body><script type="module" src="/run-browser.js"></script></body></html>', |
| 16 | + ) |
| 17 | + } |
| 18 | + }) |
| 19 | + |
| 20 | + await new Promise<void>((resolve) => server.listen(0, resolve)) |
| 21 | + const port = (server.address() as import('node:net').AddressInfo).port |
| 22 | + |
| 23 | + page.on('console', (msg) => { |
| 24 | + if (msg.type() === 'log') { |
| 25 | + console.log(msg.text()) |
| 26 | + } |
| 27 | + }) |
| 28 | + |
| 29 | + await page.goto(`http://localhost:${port}`) |
36 | 30 |
|
37 | 31 | let stats |
38 | 32 | do { |
39 | | - await new Promise((resolve) => setTimeout(resolve, 1000)) |
40 | | - stats = await t.eval(() => globalThis.stats) |
| 33 | + await page.waitForTimeout(1000) |
| 34 | + stats = await page.evaluate(() => (globalThis as any).stats) |
41 | 35 | } while (!stats) |
42 | 36 |
|
43 | | - clearInterval(interval) |
| 37 | + server.close() |
44 | 38 |
|
45 | | - await t.expect(stats?.failed).eql(0) |
| 39 | + expect(stats.failed).toBe(0) |
46 | 40 | }) |
0 commit comments