Skip to content

Commit

Permalink
chore(playwright): call finishRun in about:blank (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoFiers committed Aug 25, 2021
1 parent 2135347 commit 691bd2c
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 192 deletions.
1 change: 0 additions & 1 deletion packages/playwright/fixtures/external

This file was deleted.

10 changes: 0 additions & 10 deletions packages/playwright/fixtures/frames/bar.html

This file was deleted.

15 changes: 0 additions & 15 deletions packages/playwright/fixtures/frames/baz.html

This file was deleted.

10 changes: 0 additions & 10 deletions packages/playwright/fixtures/frames/foo.html

This file was deleted.

10 changes: 0 additions & 10 deletions packages/playwright/fixtures/frames/recursive.html

This file was deleted.

6 changes: 0 additions & 6 deletions packages/playwright/fixtures/index.html

This file was deleted.

11 changes: 0 additions & 11 deletions packages/playwright/fixtures/nested-frames.html

This file was deleted.

10 changes: 5 additions & 5 deletions packages/playwright/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/playwright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
"scripts": {
"prebuild": "rimraf dist",
"build": "tsc",
"test": "mocha --timeout 60000 -r ts-node/register 'src/test.ts'",
"test": "mocha --timeout 60000 -r ts-node/register 'tests/**.spec.ts'",
"coverage": "nyc npm run test",
"prepare": "npm run build"
},
"dependencies": {
"axe-core": "^4.3.2",
"axe-core": "^4.3.3",
"playwright": "^1.13.1"
},
"devDependencies": {
Expand Down
37 changes: 29 additions & 8 deletions packages/playwright/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import * as fs from 'fs';
import type { Page, Frame, ElementHandle } from 'playwright';
import type { RunOptions, AxeResults, ContextObject } from 'axe-core';
import type {
RunOptions,
AxeResults,
ContextObject,
PartialResults
} from 'axe-core';
import { normalizeContext, analyzePage } from './utils';
import type { AxePlaywrightParams } from './types';
import {
Expand Down Expand Up @@ -142,12 +147,7 @@ export default class AxeBuilder {
context
);
const partials = await partialResults.getPartials();
results = await page.evaluate(axeFinishRun, {
partialResults: partials,
options
});

return results;
return this.finishRun(partials);
}

/**
Expand Down Expand Up @@ -197,7 +197,13 @@ export default class AxeBuilder {

/**
* Inject `axe-core` into each frame and run `axe.runPartial`.
* Because we need to inject axe into all frames all at once (to avoid any potential problems with the DOM becoming out-of-sync) but also need to not process results for any child frames if the parent frame throws an error (requirements of the data structure for `axe.finishRun`), we have to return a deeply nested array of Promises and then flatten the array once all Promises have finished, throwing out any nested Promises if the parent Promise is not fulfilled.
* Because we need to inject axe into all frames all at once
* (to avoid any potential problems with the DOM becoming out-of-sync)
* but also need to not process results for any child frames if the parent
* frame throws an error (requirements of the data structure for `axe.finishRun`),
* we have to return a deeply nested array of Promises and then flatten
* the array once all Promises have finished, throwing out any nested Promises
* if the parent Promise is not fulfilled.
* @param frame - playwright frame object
* @param context - axe-core context object
* @returns Promise<AxePartialRunner>
Expand Down Expand Up @@ -242,4 +248,19 @@ export default class AxeBuilder {

return axePartialRunner;
}

private async finishRun(partialResults: PartialResults): Promise<AxeResults> {
const { page, option: options } = this;
const context = page.context();
const blankPage = await context.newPage();
blankPage.evaluate(this.script());
return await blankPage
.evaluate(axeFinishRun, {
partialResults,
options
})
.finally(() => {
blankPage.close();
});
}
}
Loading

0 comments on commit 691bd2c

Please sign in to comment.