Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions packages/plugin-axe/src/lib/runner/run-axe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import AxeBuilder from '@axe-core/playwright';
import { AxeBuilder } from '@axe-core/playwright';
import { createRequire } from 'node:module';
import path from 'node:path';
import { type Browser, chromium } from 'playwright-core';
import type { AuditOutputs } from '@code-pushup/models';
import {
Expand All @@ -9,8 +11,10 @@ import {
} from '@code-pushup/utils';
import { toAuditOutputs } from './transform.js';

/* eslint-disable functional/no-let */
let browser: Browser | undefined;
let browserChecked = false;
/* eslint-enable functional/no-let */

export async function runAxeForUrl(
url: string,
Expand Down Expand Up @@ -71,16 +75,27 @@ export async function closeBrowser(): Promise<void> {
}
}

/**
* Ensures Chromium browser binary is installed before running accessibility audits.
*
* Uses Node's module resolution and npm's bin specification to locate playwright-core CLI,
* working reliably with all package managers (npm, pnpm, yarn).
*/
async function ensureBrowserInstalled(): Promise<void> {
if (browserChecked) {
return;
}

logger.debug('Checking Chromium browser installation...');

const require = createRequire(import.meta.url);
const pkgPath = require.resolve('playwright-core/package.json');
const pkg = require(pkgPath);
const cliPath = path.join(path.dirname(pkgPath), pkg.bin['playwright-core']);

await executeProcess({
command: 'npx',
args: ['playwright-core', 'install', 'chromium'],
command: 'node',
args: [cliPath, 'install', 'chromium'],
});

browserChecked = true;
Expand Down
Loading