[Reporting] Optimize visualizations awaiter performance (#118012)

This commit is contained in:
Michael Dokolin 2021-11-10 17:17:07 +01:00 committed by GitHub
parent 60a9221527
commit 5cccf0cdd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 38 deletions

View file

@ -210,36 +210,16 @@ export class HeadlessChromiumDriver {
return resp; return resp;
} }
public async waitFor( public async waitFor({
{ fn,
fn, args,
args, timeout,
toEqual, }: {
timeout, fn: EvaluateFn;
}: { args: SerializableOrJSHandle[];
fn: EvaluateFn; timeout: number;
args: SerializableOrJSHandle[]; }): Promise<void> {
toEqual: number; await this.page.waitForFunction(fn, { timeout, polling: WAIT_FOR_DELAY_MS }, ...args);
timeout: number;
},
context: EvaluateMetaOpts,
logger: LevelLogger
): Promise<void> {
const startTime = Date.now();
while (true) {
const result = await this.evaluate({ fn, args }, context, logger);
if (result === toEqual) {
return;
}
if (Date.now() - startTime > timeout) {
throw new Error(
`Timed out waiting for the items selected to equal ${toEqual}. Found: ${result}. Context: ${context.context}`
);
}
await new Promise((r) => setTimeout(r, WAIT_FOR_DELAY_MS));
}
} }
public async setViewport( public async setViewport(

View file

@ -11,10 +11,23 @@ import { HeadlessChromiumDriver } from '../../browsers';
import { LayoutInstance } from '../layouts'; import { LayoutInstance } from '../layouts';
import { CONTEXT_WAITFORELEMENTSTOBEINDOM } from './constants'; import { CONTEXT_WAITFORELEMENTSTOBEINDOM } from './constants';
type SelectorArgs = Record<string, string>; interface CompletedItemsCountParameters {
context: string;
count: number;
renderCompleteSelector: string;
}
const getCompletedItemsCount = ({ renderCompleteSelector }: SelectorArgs) => { const getCompletedItemsCount = ({
return document.querySelectorAll(renderCompleteSelector).length; context,
count,
renderCompleteSelector,
}: CompletedItemsCountParameters) => {
const { length } = document.querySelectorAll(renderCompleteSelector);
// eslint-disable-next-line no-console
console.debug(`evaluate ${context}: waitng for ${count} elements, got ${length}.`);
return length >= count;
}; };
/* /*
@ -40,11 +53,11 @@ export const waitForVisualizations = async (
); );
try { try {
await browser.waitFor( await browser.waitFor({
{ fn: getCompletedItemsCount, args: [{ renderCompleteSelector }], toEqual, timeout }, fn: getCompletedItemsCount,
{ context: CONTEXT_WAITFORELEMENTSTOBEINDOM }, args: [{ renderCompleteSelector, context: CONTEXT_WAITFORELEMENTSTOBEINDOM, count: toEqual }],
logger timeout,
); });
logger.debug(`found ${toEqual} rendered elements in the DOM`); logger.debug(`found ${toEqual} rendered elements in the DOM`);
} catch (err) { } catch (err) {