test: reduce flakiness and types (#12347)

This commit is contained in:
Nikolay Vitkov 2024-04-26 12:21:21 +02:00 committed by GitHub
parent 137236ee2a
commit c2244d301a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 22 deletions

View File

@ -520,7 +520,7 @@ describe('network', function () {
const responses: HTTPResponse[] = [];
page.on('response', response => {
return responses.push(response);
return !isFavicon(response) && responses.push(response);
});
await page.goto(server.EMPTY_PAGE);
expect(responses).toHaveLength(1);

View File

@ -6,7 +6,6 @@
import {rm} from 'fs/promises';
import {tmpdir} from 'os';
import path from 'path';
import expect from 'expect';
import type {Frame} from 'puppeteer-core/internal/api/Frame.js';
@ -16,8 +15,6 @@ import {Deferred} from 'puppeteer-core/internal/util/Deferred.js';
import {compare} from './golden-utils.js';
const PROJECT_ROOT = path.join(__dirname, '..', '..');
declare module 'expect' {
interface Matchers<R> {
toBeGolden(pathOrBuffer: string | Buffer): R;
@ -56,28 +53,26 @@ export const extendExpectWithToBeGolden = (
});
};
export const projectRoot = (): string => {
return PROJECT_ROOT;
};
export const attachFrame = async (
pageOrFrame: Page | Frame,
frameId: string,
url: string
): Promise<Frame | undefined> => {
using handle = await pageOrFrame.evaluateHandle(attachFrame, frameId, url);
return (await handle.asElement()?.contentFrame()) ?? undefined;
async function attachFrame(frameId: string, url: string) {
const frame = document.createElement('iframe');
frame.src = url;
frame.id = frameId;
document.body.appendChild(frame);
await new Promise(x => {
return (frame.onload = x);
});
return frame;
}
): Promise<Frame> => {
using handle = await pageOrFrame.evaluateHandle(
async (frameId, url) => {
const frame = document.createElement('iframe');
frame.src = url;
frame.id = frameId;
document.body.appendChild(frame);
await new Promise(x => {
return (frame.onload = x);
});
return frame;
},
frameId,
url
);
return await handle.contentFrame();
};
export const isFavicon = (request: {url: () => string | string[]}): boolean => {