import { i18n } from '@kbn/i18n' ; import apm from 'elastic-apm-node' ; import type { Logger } from 'src/core/server' ; import type { Layout } from 'src/plugins/screenshot_mode/common' ; import { Context } from '../../common' ; import type { HeadlessChromiumDriver } from '../browsers' ; import type { ConditionalHeaders } from '../browsers' ; import { DEFAULT_PAGELOAD_SELECTOR } from './constants' ; type Url = string ; type UrlWithContext = [ url : Url , context : Context ] ; export type UrlOrUrlWithContext = Url | UrlWithContext ; export const openUrl = async ( browser , logger , timeout , index , urlOrUrlWithContext , conditionalHeaders , layout ? ) : Promise < void > => { const page = index + 1 ; const waitForSelector = page > 1 ? `[data-shared-page="${ page }"]` : DEFAULT_PAGELOAD_SELECTOR ; const span = apm . startSpan ( 'open_url' , 'wait' ) ; let url ; let context ; if ( typeof urlOrUrlWithContext === 'string' ) { url = urlOrUrlWithContext ; } else { [ url , context ] = urlOrUrlWithContext ; } try { await browser . open ( url , { conditionalHeaders , context , layout , waitForSelector , timeout } , logger ) ; } catch ( err ) { logger . error ( err ) ; throw new Error ( i18n . translate ( 'xpack.screenshotting.screencapture.couldntLoadKibana' , { defaultMessage : `An error occurred when trying to open the Kibana URL: {error}` , values : { error : err } , } ) ) ; } span ?. end ( ) ; } ;