Skip to content

Commit

Permalink
feat(playwright): allow AxeBuilder to use different version of axe-…
Browse files Browse the repository at this point in the history
…core (#335)

* feat(playwright): allow `AxeBuilder` to use different version of axe-core

* ignore eslint
  • Loading branch information
michael-siek committed Aug 17, 2021
1 parent 25a8c1b commit f803c98
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ packages/webdriverjs/src/test/fixtures/*
packages/webdriverio/fixtures/*
packages/puppeteer/test/fixtures/*
**/dist/*
packages/playwright/fixtures/*
1 change: 1 addition & 0 deletions packages/playwright/fixtures/external
8 changes: 5 additions & 3 deletions packages/playwright/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';
import type { Page, Frame } from 'playwright';
import type { RunOptions, AxeResults } from 'axe-core';
import { source } from 'axe-core';
import { normalizeContext, analyzePage } from './utils';
import type { AxePlaywrightParams } from './types';

Expand All @@ -10,12 +10,14 @@ export default class AxeBuilder {
private excludes: string[];
private option: RunOptions;
private source: string;
constructor({ page }: AxePlaywrightParams) {
constructor({ page, axeSource }: AxePlaywrightParams) {
const axePath = require.resolve('axe-core');
const source = fs.readFileSync(axePath, 'utf-8');
this.page = page;
this.includes = [];
this.excludes = [];
this.option = {};
this.source = source;
this.source = axeSource || source;
}

/**
Expand Down
34 changes: 27 additions & 7 deletions packages/playwright/src/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'mocha';
import * as fs from 'fs';
import * as playwright from 'playwright';
import * as express from 'express';
import type { AxeResults } from 'axe-core';
Expand All @@ -13,6 +14,11 @@ describe('@axe-core/playwright', () => {
let addr: string;
let page: playwright.Page;
let browser: playwright.ChromiumBrowser;
const axeTestFixtures = path.resolve(__dirname, '..', 'fixtures');
const axeLegacySource = fs.readFileSync(
path.resolve(axeTestFixtures, 'external', 'axe-core@legacy.js'),
'utf-8'
);

before(async () => {
const app = express();
Expand All @@ -38,6 +44,20 @@ describe('@axe-core/playwright', () => {
});

describe('analyze', () => {
it('returns results using a different version of axe-core', async () => {
await page.goto(`${addr}/index.html`);
const results = await new AxeBuilder({
page,
axeSource: axeLegacySource
}).analyze();
assert.strictEqual(results.testEngine.version, '4.0.3');
assert.isNotNull(results);
assert.isArray(results.violations);
assert.isArray(results.incomplete);
assert.isArray(results.passes);
assert.isArray(results.inapplicable);
});

it('returns results', async () => {
await page.goto(`${addr}/index.html`);
const results = await new AxeBuilder({ page }).analyze();
Expand Down Expand Up @@ -196,22 +216,22 @@ describe('@axe-core/playwright', () => {
});

it('injects once per iframe', async () => {
await page.goto(`${addr}/nested-frames.html`)
await page.goto(`${addr}/nested-frames.html`);

const builder = new AxeBuilder({ page });
const origScript = (builder as any).script;
let count = 0;
Object.defineProperty(builder, 'script', {
get() {
count++
return origScript
count++;
return origScript;
}
})
});

await builder.analyze()
await builder.analyze();

assert.strictEqual(count, 4)
})
assert.strictEqual(count, 4);
});
});

describe('include/exclude', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/playwright/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface NormalizeContextResponse {

export interface AxePlaywrightParams {
page: Page;
axeSource?: string;
}

export interface AnalyzePageResponse {
Expand Down

0 comments on commit f803c98

Please sign in to comment.