Skip to content

Commit d30dae4

Browse files
authored
fix(playwright): skip unloaded iframes (#1060)
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
1 parent 232476e commit d30dae4

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed

package-lock.json

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/playwright/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"axe-core": "~4.9.1"
5555
},
5656
"devDependencies": {
57-
"@playwright/test": "^1.34.3",
57+
"@playwright/test": "^1.44.0",
5858
"@types/chai": "^4.3.3",
5959
"@types/express": "^4.17.14",
6060
"@types/mocha": "^10.0.0",

packages/playwright/src/index.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,24 @@ export default class AxeBuilder {
183183
* @returns Promise<void>
184184
*/
185185

186-
private async inject(frames: Frame[]): Promise<void> {
186+
private async inject(frames: Frame[], shouldThrow?: boolean): Promise<void> {
187187
for (const iframe of frames) {
188-
await iframe.evaluate(await this.script());
189-
await iframe.evaluate(await this.axeConfigure());
188+
const race = new Promise((_, reject) => {
189+
setTimeout(() => {
190+
reject(new Error('Script Timeout'));
191+
}, 1000);
192+
});
193+
const evaluate = iframe.evaluate(this.script());
194+
195+
try {
196+
await Promise.race([evaluate, race]);
197+
await iframe.evaluate(await this.axeConfigure());
198+
} catch (err) {
199+
// in legacy mode we don't want to throw the error we just want to skip injecting into the frame
200+
if (shouldThrow) {
201+
throw err;
202+
}
203+
}
190204
}
191205
}
192206

@@ -256,7 +270,7 @@ export default class AxeBuilder {
256270
iframeHandle.asElement() as ElementHandle<Element>;
257271
const childFrame = await iframeElement.contentFrame();
258272
if (childFrame) {
259-
await this.inject([childFrame]);
273+
await this.inject([childFrame], true);
260274
childResults = await this.runPartialRecursive(
261275
childFrame,
262276
frameContext

packages/playwright/test/axe-playwright.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,12 @@ describe('@axe-core/playwright', () => {
447447
.analyze();
448448

449449
assert.equal(res?.status(), 200);
450-
assert.lengthOf(results.incomplete, 0);
450+
assert.equal(results.incomplete[0].id, 'frame-tested');
451+
assert.lengthOf(results.incomplete[0].nodes, 1);
452+
assert.deepEqual(results.incomplete[0].nodes[0].target, [
453+
'#ifr-lazy',
454+
'#lazy-iframe'
455+
]);
451456
assert.equal(results.violations[0].id, 'label');
452457
assert.lengthOf(results.violations[0].nodes, 1);
453458
assert.deepEqual(results.violations[0].nodes[0].target, [

0 commit comments

Comments
 (0)