test: add a test for the web build (#12367)

This commit is contained in:
Alex Rudenko 2024-05-02 13:20:22 +02:00 committed by GitHub
parent 54a6377d7d
commit 44fb53eb33
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 86 additions and 4 deletions

View File

@ -0,0 +1,34 @@
import puppeteer from 'puppeteer';
const browser = await puppeteer.launch({
headless: true,
args: ['--remote-allow-origins=*'],
});
try {
const port = process.argv[2];
const page = await browser.newPage();
await page.goto(`http://localhost:${port}/index.html`);
await page.type('input', browser.wsEndpoint());
const [result] = await Promise.all([
new Promise(resolve => {
page.once('dialog', dialog => {
dialog.accept();
resolve(dialog.message());
});
}),
page.click('button'),
]);
const alert = await result;
if (alert !== 'Browser has 2 pages') {
throw new Error('Unexpected alert content: ' + alert);
}
} finally {
await browser.close();
}

View File

@ -29,7 +29,8 @@
"dependencies": [ "dependencies": [
"../../packages/puppeteer:build", "../../packages/puppeteer:build",
"../../packages/puppeteer-core:build", "../../packages/puppeteer-core:build",
"../../packages/browsers:build" "../../packages/browsers:build",
"../../packages/testserver:build"
], ],
"files": [], "files": [],
"output": [ "output": [

View File

@ -23,3 +23,10 @@ export const ASSETS_DIR = join(
'..', '..',
'assets' 'assets'
); );
export const EXAMPLES_DIR = join(
dirname(fileURLToPath(import.meta.url)),
'..',
'..',
'..',
'examples'
);

View File

@ -5,11 +5,16 @@
*/ */
import assert from 'assert'; import assert from 'assert';
import {spawnSync} from 'child_process';
import {readdirSync} from 'fs'; import {readdirSync} from 'fs';
import fs from 'fs';
import {readdir} from 'fs/promises'; import {readdir} from 'fs/promises';
import {platform} from 'os'; import {platform} from 'os';
import {join} from 'path'; import {join} from 'path';
import {TestServer} from '@pptr/testserver';
import {EXAMPLES_DIR} from './constants.js';
import {configureSandbox} from './sandbox.js'; import {configureSandbox} from './sandbox.js';
import {readAsset} from './util.js'; import {readAsset} from './util.js';
@ -37,6 +42,33 @@ describe('`puppeteer`', () => {
const script = await readAsset('puppeteer-core', 'imports.js'); const script = await readAsset('puppeteer-core', 'imports.js');
await this.runScript(script, 'mjs'); await this.runScript(script, 'mjs');
}); });
it('runs in the browser', async function () {
const puppeteerInBrowserPath = join(this.sandbox, 'puppeteer-in-browser');
fs.cpSync(
join(EXAMPLES_DIR, 'puppeteer-in-browser'),
puppeteerInBrowserPath,
{
recursive: true,
}
);
spawnSync('npm', ['ci'], {
cwd: puppeteerInBrowserPath,
shell: true,
});
spawnSync('npm', ['run', 'build'], {
cwd: puppeteerInBrowserPath,
shell: true,
});
const server = await TestServer.create(puppeteerInBrowserPath);
try {
const script = await readAsset('puppeteer', 'puppeteer-in-browser.js');
await this.runScript(script, 'mjs', [String(server.port)]);
} finally {
await server.stop();
}
});
}); });
// Skipping this test on Windows as windows runners are much slower. // Skipping this test on Windows as windows runners are much slower.

View File

@ -52,7 +52,11 @@ declare module 'mocha' {
*/ */
sandbox: string; sandbox: string;
env: NodeJS.ProcessEnv | undefined; env: NodeJS.ProcessEnv | undefined;
runScript: (content: string, type: 'cjs' | 'mjs') => Promise<void>; runScript: (
content: string,
type: 'cjs' | 'mjs',
args?: string[]
) => Promise<void>;
} }
} }
@ -111,10 +115,14 @@ export const configureSandbox = (options: SandboxOptions): void => {
this.sandbox = sandbox; this.sandbox = sandbox;
this.env = env; this.env = env;
this.runScript = async (content: string, type: 'cjs' | 'mjs') => { this.runScript = async (
content: string,
type: 'cjs' | 'mjs',
args?: string[]
) => {
const script = join(sandbox, `script-${crypto.randomUUID()}.${type}`); const script = join(sandbox, `script-${crypto.randomUUID()}.${type}`);
await writeFile(script, content); await writeFile(script, content);
await execFile('node', [script], {cwd: sandbox, env}); await execFile('node', [script, ...(args ?? [])], {cwd: sandbox, env});
}; };
console.timeEnd('before'); console.timeEnd('before');
}); });