import { i18n } from '@kbn/i18n' ; import apm from 'elastic-apm-node' ; import type { Logger } from 'src/core/server' ; import type { HeadlessChromiumDriver } from '../browsers' ; import { Layout } from '../layouts' ; import { CONTEXT_WAITFORELEMENTSTOBEINDOM } from './constants' ; interface CompletedItemsCountParameters { context : string ; count : number ; renderCompleteSelector : string ; } const getCompletedItemsCount = ( { context , count , renderCompleteSelector , } ) => { const { length } = document . querySelectorAll ( renderCompleteSelector ) ; console . debug ( `evaluate ${ context }: waitng for ${ count } elements, got ${ length }.` ) ; return length >= count ; } ; export const waitForVisualizations = async ( browser , logger , timeout , toEqual , layout ) : Promise < void > => { const span = apm . startSpan ( 'wait_for_visualizations' , 'wait' ) ; const { renderComplete : renderCompleteSelector } = layout . selectors ; logger . debug ( i18n . translate ( 'xpack.screenshotting.screencapture.waitingForRenderedElements' , { defaultMessage : `waiting for {itemsCount} rendered elements to be in the DOM` , values : { itemsCount : toEqual } , } ) ) ; try { await browser . waitFor ( { fn : getCompletedItemsCount , args : [ { renderCompleteSelector , context : CONTEXT_WAITFORELEMENTSTOBEINDOM , count : toEqual } ] , timeout , } ) ; logger . debug ( `found ${ toEqual } rendered elements in the DOM` ) ; } catch ( err ) { logger . error ( err ) ; throw new Error ( i18n . translate ( 'xpack.screenshotting.screencapture.couldntFinishRendering' , { defaultMessage : `An error occurred when trying to wait for {count} visualizations to finish rendering. {error}` , values : { count : toEqual , error : err } , } ) ) ; } span ?. end ( ) ; } ;