Skip to content

Commit 1793ead

Browse files
authored
test: yfm html visual test stability (#1073)
1 parent a8d4620 commit 1793ead

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

demo/tests/playwright/core/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export interface WaitFixture {
5757
visible(selector: Locator): Promise<void>;
5858
hidden(selector: Locator): Promise<void>;
5959
timeout(delay?: number): Promise<void>;
60+
markupRendered(delay?: number, stableThreshold?: number): Promise<void>;
6061
}
6162

6263
export interface CaptureScreenshotParams extends PageScreenshotOptions {

demo/tests/playwright/core/wait.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import type {Locator} from '@playwright/test';
33
import type {PlaywrightFixture, WaitFixture} from './types';
44

55
const DEFAULT_DELAY = 100;
6+
const MAX_STABLE_CHECKS = 50;
7+
const REQUIRED_STABLE_COUNT = 2;
8+
9+
type Snapshot = {text: string; height: number};
610

711
export const wait: PlaywrightFixture<WaitFixture> = async ({page}, use) => {
812
await use({
@@ -23,5 +27,36 @@ export const wait: PlaywrightFixture<WaitFixture> = async ({page}, use) => {
2327
timeout: async (delay = DEFAULT_DELAY) => {
2428
await page.waitForTimeout(delay);
2529
},
30+
markupRendered: async (delay = DEFAULT_DELAY, stableThreshold = REQUIRED_STABLE_COUNT) => {
31+
const locator = page.locator('.playground__markup');
32+
await locator.waitFor({state: 'visible'});
33+
34+
let prev: Snapshot = {text: '', height: 0};
35+
let stableCount = 0;
36+
37+
for (let i = 0; i < MAX_STABLE_CHECKS; i++) {
38+
const curr = await getMarkupPreviewMetrics(locator);
39+
40+
if (isStable(prev, curr)) {
41+
stableCount++;
42+
if (stableCount >= stableThreshold) break;
43+
} else {
44+
stableCount = 0;
45+
prev = curr;
46+
}
47+
48+
await page.waitForTimeout(delay);
49+
}
50+
},
2651
});
2752
};
53+
54+
async function getMarkupPreviewMetrics(locator: Locator): Promise<Snapshot> {
55+
const text = (await locator.textContent())?.trim() ?? '';
56+
const height = (await locator.boundingBox())?.height ?? 0;
57+
return {text, height};
58+
}
59+
60+
function isStable(prev: Snapshot, curr: Snapshot): boolean {
61+
return Boolean(curr.text) && prev.text === curr.text && prev.height === curr.height;
62+
}

demo/tests/visual-tests/YfmExtensions.visual.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ test.describe('Extensions, YFM', () => {
2323
await mount(<YFMStories.YfmTabs />);
2424
await expectScreenshot();
2525
});
26-
test('YFM HTML', async ({mount, expectScreenshot, page}) => {
26+
test('YFM HTML', async ({mount, expectScreenshot, page, wait}) => {
2727
await mount(<YFMStories.YfmHtmlBlock />);
2828

2929
// TODO: @makhnatkin Improve iframe height stabilization wait
3030
await page.waitForTimeout(200);
31+
await wait.markupRendered();
3132
await expectScreenshot();
3233
});
3334
test('YFM File', async ({mount, expectScreenshot}) => {

0 commit comments

Comments
 (0)