Skip to content

Commit

Permalink
speed up playwright
Browse files Browse the repository at this point in the history
  • Loading branch information
djahandarie committed Dec 28, 2024
1 parent a0302f0 commit b1b5ad0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
12 changes: 12 additions & 0 deletions test/data/html/js/popup-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ HtmlTestUtilities.runMain(() => {
if (!(iframeWithSrcdoc instanceof HTMLIFrameElement)) { continue; }
iframeWithSrcdoc.srcdoc = HtmlTestUtilities.dataUrlToContent(src).content;
}

const testCases = document.querySelectorAll('test-case[data-expected-result="failure"]');
for (const testCase of testCases) {
const description = testCase.querySelector('test-description');
if (description) {
const dangerSpan = document.createElement('span');
dangerSpan.className = 'danger';
dangerSpan.textContent = 'This element is expected to not work.';
description.appendChild(document.createElement('br'));
description.appendChild(dangerSpan);
}
}
});
19 changes: 10 additions & 9 deletions test/data/html/popup-tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
.danger {
color: #c83c28;
}


</style>
</head>
<body>
Expand All @@ -53,7 +55,7 @@ <h1>Popup Tests</h1>
</div>
</test-case>

<test-case data-shadow-mode="open">
<test-case data-shadow-mode="open" data-expected-result="failure">
<test-description>Content inside of an open shadow DOM.</test-description>
<div class="template-content-container hovertarget"></div>
<template>
Expand All @@ -71,7 +73,7 @@ <h1>Popup Tests</h1>
</template>
</test-case>

<test-case data-shadow-mode="closed">
<test-case data-shadow-mode="closed" data-expected-result="failure">
<test-description>Content inside of a closed shadow DOM.</test-description>
<div class="template-content-container hovertarget"></div>
<template>
Expand Down Expand Up @@ -110,14 +112,14 @@ <h1>Popup Tests</h1>
</template>
</test-case>

<test-case>
<test-case data-expected-result="failure">
<test-description>&lt;iframe&gt; element with data URL.</test-description>
<iframe id="iframe-with-data-url"
src="data:text/html;base64,PCFET0NUWVBFIGh0bWw+DQo8aHRtbD4NCiAgICA8aGVhZD4NCiAgICAgICAgPG1ldGEgY2hhcnNld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zY3JpcHQ+DQogICAgICAgIDxzdHlsZT4NCmJvZHkgew0KICAgIGZvbnQtZmFtaWx5OiAiSGVsdmV0aWNhIE5ldWUiLCBIZWx2ZXRpY2EsIEFyaWFsLCBzYW5zLXNlcmlmOw0KICAgIGZvbnQtc2l6ZTogMTRweDsNCiAgICBwYWRkaW5nOiAwOw0KICAgIG1hcmdpbjogMDsNCiAgICBiYWNrZ3JvdW5kLWNvbG9yOiAjZjhmOGY4Ow0KfQ0KYSwgYTp2aXNpdGVkIHsNCiAgICBjb2xvcjogIzEwODBjMDsNCiAgICB0ZXh0LWRlY29yYXRpb246IHVuZGVybGluZTsNCn0NCi5jb250ZW50IHsNCiAgICBwb3NpdGlvbjogYWJzb2x1dGU7DQogICAgbGVmdDogMDsNCiAgICB0b3A6IDA7DQogICAgcmlnaHQ6IDA7DQogICAgYm90dG9tOiAwOw0KICAgIHBhZGRpbmc6IDAuNWVtOw0KICAgIGJhY2tncm91bmQtY29sb3I6ICNmOGY4Zjg7DQp9DQogICAgICAgIDwvc3R5bGU+DQogICAgPC9oZWFkPg0KPGJvZHk+PGRpdiBjbGFzcz0iY29udGVudCI+DQo8ZGl2Pg0KICAgIOOBguOCiuOBjOOBqOOBhg0KPC9kaXY+DQo8ZGl2Pg0KICAgIDxhIGhyZWY9IiMiIGNsYXNzPSJmdWxsc2NyZWVuLWxpbmsiPlRvZ2dsZSBmdWxsc2NyZWVuPC9hPg0KICAgIDxzY3JpcHQ+c2V0dXAoZG9jdW1lbnQuYm9keSwgZG9jdW1lbnQuYm9keSk7PC9zY3JpcHQ+DQo8L2Rpdj4NCjwvZGl2PjwvYm9keT4NCjwvaHRtbD4="
allowfullscreen="true" class="container hovertarget"></iframe>
</test-case>

<test-case>
<test-case data-expected-result="failure">
<test-description>&lt;iframe&gt; element with blob URL.</test-description>
<iframe id="iframe-with-blob-url" allowfullscreen="true" class="container hovertarget"></iframe>
</test-case>
Expand All @@ -134,16 +136,15 @@ <h1>Popup Tests</h1>
sandbox="allow-same-origin allow-scripts"></iframe>
</test-case>

<test-case>
<test-case data-expected-result="failure">
<test-description>
&lt;iframe&gt; element with srcdoc and <code>sandbox="allow-scripts"</code>.<br>
<span class="danger">This element is expected to not work.</span>
&lt;iframe&gt; element with srcdoc and <code>sandbox="allow-scripts"</code>.
</test-description>
<iframe allowfullscreen="true" class="iframe-with-srcdoc container hovertarget"
sandbox="allow-scripts"></iframe>
</test-case>

<test-case>
<test-case data-expected-result="failure">
<test-description>SVG &lt;img&gt;.</test-description>
<img src="popup-tests-frame2.svg" class="container hovertarget" alt="">
</test-case>
Expand All @@ -153,7 +154,7 @@ <h1>Popup Tests</h1>
<object data="popup-tests-frame2.svg" type="image/svg+xml" class="container hovertarget"></object>
</test-case>

<test-case>
<test-case data-expected-result="failure">
<test-description>SVG &lt;embed&gt;.</test-description>
<embed type="image/svg+xml" src="popup-tests-frame2.svg" class="container hovertarget">
</test-case>
Expand Down
25 changes: 11 additions & 14 deletions test/playwright/visual.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ test('visual', async ({page, extensionId}) => {
/**
* @param {number} doc_number
* @param {number} test_number
* @param {import('@playwright/test').ElementHandle<Node>} el
* @param {import('@playwright/test').Locator} test_locator
* @param {{x: number, y: number}} offset
*/
const screenshot = async (doc_number, test_number, el, offset) => {
const screenshot = async (doc_number, test_number, test_locator, offset) => {
const test_name = 'doc' + doc_number + '-test' + test_number;

const box = (await el.boundingBox()) || {x: 0, y: 0, width: 0, height: 0};
const box = (await test_locator.boundingBox()) || {x: 0, y: 0, width: 0, height: 0};

// Find the popup frame if it exists
let popup_frame = page.frames().find((f) => f.url().includes('popup.html'));
Expand All @@ -110,13 +110,10 @@ test('visual', async ({page, extensionId}) => {
if (typeof popup_frame === 'undefined') {
popup_frame = await frame_attached; // Wait for popup to be attached
}
try {
// Some tests don't have a popup, so don't fail if it's not there
// TODO: check if the popup is expected to be there
await (await /** @type {import('@playwright/test').Frame} */ (popup_frame).frameElement()).waitForElementState('visible', {timeout: 500});
} catch (error) {
console.log(test_name + ' has no popup');
}

const expectedState = (await test_locator.locator('..').getAttribute('data-expected-result')) === 'failure' ? 'hidden' : 'visible';
await (await /** @type {import('@playwright/test').Frame} */ (popup_frame).frameElement()).waitForElementState(expectedState, {timeout: 50});


await page.bringToFront(); // Bring the page to the foreground so the screenshot doesn't hang; for some reason the frames result in page being in the background
await expect.soft(page).toHaveScreenshot(test_name + '.png');
Expand All @@ -130,8 +127,8 @@ test('visual', async ({page, extensionId}) => {
await page.setViewportSize({width: 1000, height: 1800});
await page.keyboard.down('Shift');
let i = 1;
for (const el of await page.locator('div > *:nth-child(1)').elementHandles()) {
await screenshot(1, i, el, {x: 6, y: 6});
for (const test_locator of await page.locator('div > *:nth-child(1)').all()) {
await screenshot(1, i, test_locator, {x: 6, y: 6});
i++;
}

Expand All @@ -140,8 +137,8 @@ test('visual', async ({page, extensionId}) => {
await page.setViewportSize({width: 1000, height: 4500});
await page.keyboard.down('Shift');
i = 1;
for (const el of await page.locator('.hovertarget').elementHandles()) {
await screenshot(2, i, el, {x: 15, y: 15});
for (const test_locator of await page.locator('.hovertarget').all()) {
await screenshot(2, i, test_locator, {x: 15, y: 15});
i++;
}
});

0 comments on commit b1b5ad0

Please sign in to comment.