Skip to content

Commit

Permalink
fix(playwright): skip unloaded iframes (#1060)
Browse files Browse the repository at this point in the history
Pulled out of #1048.
Starting in Playwright 1.41.0 (using Chrome version 121.0.6167.57)
Playwright is no longer loading the lazy loaded iframe that is out of
view. Updated the version of Playwright and updated the tests to now
account for this.

QA notes: Test a lazy loaded iframe with Playwright > 1.41.0 and ensure
Playwright will complete an analyze of the page
  • Loading branch information
straker authored May 13, 2024
1 parent 232476e commit d30dae4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
24 changes: 12 additions & 12 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion packages/playwright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"axe-core": "~4.9.1"
},
"devDependencies": {
"@playwright/test": "^1.34.3",
"@playwright/test": "^1.44.0",
"@types/chai": "^4.3.3",
"@types/express": "^4.17.14",
"@types/mocha": "^10.0.0",
Expand Down
22 changes: 18 additions & 4 deletions packages/playwright/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,24 @@ export default class AxeBuilder {
* @returns Promise<void>
*/

private async inject(frames: Frame[]): Promise<void> {
private async inject(frames: Frame[], shouldThrow?: boolean): Promise<void> {
for (const iframe of frames) {
await iframe.evaluate(await this.script());
await iframe.evaluate(await this.axeConfigure());
const race = new Promise((_, reject) => {
setTimeout(() => {
reject(new Error('Script Timeout'));
}, 1000);
});
const evaluate = iframe.evaluate(this.script());

try {
await Promise.race([evaluate, race]);
await iframe.evaluate(await this.axeConfigure());
} catch (err) {
// in legacy mode we don't want to throw the error we just want to skip injecting into the frame
if (shouldThrow) {
throw err;
}
}
}
}

Expand Down Expand Up @@ -256,7 +270,7 @@ export default class AxeBuilder {
iframeHandle.asElement() as ElementHandle<Element>;
const childFrame = await iframeElement.contentFrame();
if (childFrame) {
await this.inject([childFrame]);
await this.inject([childFrame], true);
childResults = await this.runPartialRecursive(
childFrame,
frameContext
Expand Down
7 changes: 6 additions & 1 deletion packages/playwright/test/axe-playwright.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,12 @@ describe('@axe-core/playwright', () => {
.analyze();

assert.equal(res?.status(), 200);
assert.lengthOf(results.incomplete, 0);
assert.equal(results.incomplete[0].id, 'frame-tested');
assert.lengthOf(results.incomplete[0].nodes, 1);
assert.deepEqual(results.incomplete[0].nodes[0].target, [
'#ifr-lazy',
'#lazy-iframe'
]);
assert.equal(results.violations[0].id, 'label');
assert.lengthOf(results.violations[0].nodes, 1);
assert.deepEqual(results.violations[0].nodes[0].target, [
Expand Down

0 comments on commit d30dae4

Please sign in to comment.