import { yellow } from 'chalk' ; import scrollToPosition from '../clientSideScripts/scrollToPosition' ; import getDocumentScrollHeight from '../clientSideScripts/getDocumentScrollHeight' ; import getAndroidStatusAddressToolBarHeight from '../clientSideScripts/getAndroidStatusAddressToolBarHeight' ; import getIosStatusAddressToolBarHeight from '../clientSideScripts/getIosStatusAddressToolBarHeight' ; import { OFFSETS } from '../helpers/constants' ; import { calculateDprData , getScreenshotSize , waitFor } from '../helpers/utils' ; import { Executor , TakeScreenShot } from './methods.interface' ; import { FullPageScreenshotOptions , FullPageScreenshotNativeMobileOptions , FullPageScreenshotDataOptions , FullPageScreenshotsData , } from './screenshots.interfaces' ; import { StatusAddressToolBarHeight } from '../clientSideScripts/statusAddressToolBarHeight.interfaces' ; import hideRemoveElements from '../clientSideScripts/hideRemoveElements' ; import hideScrollBars from '../clientSideScripts/hideScrollbars' ; import { LogLevel } from "../helpers/options.interface" ; import { error } from "selenium-webdriver" ; /**
 * Take a full page screenshots for desktop / iOS / Android
 */ export async function getBase64FullPageScreenshotsData ( takeScreenshot , executor , options ) { const { addressBarShadowPadding , devicePixelRatio , fullPageScrollTimeout , hideAfterFirstScroll , innerHeight , isAndroid , isAndroidNativeWebScreenshot , isAndroidChromeDriverScreenshot , isHybridApp , isIos , logLevel , toolBarShadowPadding , } = options ; const desktopOptions = { devicePixelRatio , fullPageScrollTimeout , hideAfterFirstScroll , innerHeight , logLevel , } ; const nativeMobileOptions = { ... desktopOptions , addressBarShadowPadding , toolBarShadowPadding , } ; if ( isAndroid && isAndroidNativeWebScreenshot ) { const statusAddressBarHeight = ( < StatusAddressToolBarHeight > ( await executor ( getAndroidStatusAddressToolBarHeight , OFFSETS . ANDROID , isHybridApp ) ) ) . statusAddressBar . height ; const androidNativeMobileOptions = { ... nativeMobileOptions , statusAddressBarHeight } ; return getFullPageScreenshotsDataNativeMobile ( takeScreenshot , executor , androidNativeMobileOptions ) ; } else if ( isAndroid && isAndroidChromeDriverScreenshot ) { const chromeDriverOptions = { devicePixelRatio , fullPageScrollTimeout , hideAfterFirstScroll , innerHeight , logLevel } ; return getFullPageScreenshotsDataAndroidChromeDriver ( takeScreenshot , executor , chromeDriverOptions ) ; } else if ( isIos ) { const statusAddressBarHeight = ( < StatusAddressToolBarHeight > ( await executor ( getIosStatusAddressToolBarHeight , OFFSETS . IOS ) ) ) . statusAddressBar . height ; const iosNativeMobileOptions = { ... nativeMobileOptions , statusAddressBarHeight } ; return getFullPageScreenshotsDataNativeMobile ( takeScreenshot , executor , iosNativeMobileOptions ) ; } return getFullPageScreenshotsDataDesktop ( takeScreenshot , executor , desktopOptions ) ; } /**
 * Take a full page screenshots for native mobile
 */ export async function getFullPageScreenshotsDataNativeMobile ( takeScreenshot , executor , options ) { const viewportScreenshots = [ ] ; const { addressBarShadowPadding , devicePixelRatio , fullPageScrollTimeout , hideAfterFirstScroll , innerHeight , logLevel , statusAddressBarHeight , toolBarShadowPadding , } = options ; const iosViewportHeight = innerHeight - addressBarShadowPadding - toolBarShadowPadding ; const amountOfScrollsArray = [ ] ; let scrollHeight ; let screenshotSizeWidth ; for ( let i = 0 ; i <= amountOfScrollsArray . length ; i ++ ) { const scrollY = iosViewportHeight * i ; await executor ( scrollToPosition , scrollY ) ; await executor ( hideScrollBars , true ) ; await waitFor ( fullPageScrollTimeout ) ; if ( i === 1 && hideAfterFirstScroll . length > 0 ) { try { await executor ( hideRemoveElements , { hide : hideAfterFirstScroll , remove : [ ] } , true ) ; } catch ( e ) { logHiddenRemovedError ( e , logLevel ) ; } } const screenshot = await takeBase64Screenshot ( takeScreenshot ) ; screenshotSizeWidth = getScreenshotSize ( screenshot , devicePixelRatio ) . width ; scrollHeight = await executor ( getDocumentScrollHeight ) ; if ( ( ( scrollY + iosViewportHeight ) < scrollHeight ) ) { amountOfScrollsArray . push ( amountOfScrollsArray . length ) ; } const imageHeight = amountOfScrollsArray . length === i ? scrollHeight - scrollY : iosViewportHeight ; const imageYPosition = ( amountOfScrollsArray . length === i ? innerHeight - imageHeight : 0 ) + statusAddressBarHeight + addressBarShadowPadding ; viewportScreenshots . push ( { ... calculateDprData ( { canvasWidth : screenshotSizeWidth , canvasYPosition : scrollY , imageHeight : imageHeight , imageWidth : screenshotSizeWidth , imageYPosition : imageYPosition , } , devicePixelRatio ) , screenshot , } ) ; await executor ( hideScrollBars , false ) ; } if ( hideAfterFirstScroll . length > 0 ) { try { await executor ( hideRemoveElements , { hide : hideAfterFirstScroll , remove : [ ] } , false ) ; } catch ( e ) { logHiddenRemovedError ( e , logLevel ) ; } } return { ... calculateDprData ( { fullPageHeight : scrollHeight - addressBarShadowPadding - toolBarShadowPadding , fullPageWidth : screenshotSizeWidth , } , devicePixelRatio ) , data : viewportScreenshots , } ; } /**
 * Take a full page screenshot for Android with Chromedriver
 */ export async function getFullPageScreenshotsDataAndroidChromeDriver ( takeScreenshot , executor , options ) { const viewportScreenshots = [ ] ; const { devicePixelRatio , fullPageScrollTimeout , hideAfterFirstScroll , innerHeight , logLevel } = options ; const amountOfScrollsArray = [ ] ; let scrollHeight ; let screenshotSize ; for ( let i = 0 ; i <= amountOfScrollsArray . length ; i ++ ) { const scrollY = innerHeight * i ; await executor ( scrollToPosition , scrollY ) ; await executor ( hideScrollBars , true ) ; await waitFor ( fullPageScrollTimeout ) ; if ( i === 1 && hideAfterFirstScroll . length > 0 ) { try { await executor ( hideRemoveElements , { hide : hideAfterFirstScroll , remove : [ ] } , true ) ; } catch ( e ) { logHiddenRemovedError ( e , logLevel ) ; } } const screenshot = await takeBase64Screenshot ( takeScreenshot ) ; screenshotSize = getScreenshotSize ( screenshot , devicePixelRatio ) ; scrollHeight = await executor ( getDocumentScrollHeight ) ; if ( ( scrollY + innerHeight ) < scrollHeight ) { amountOfScrollsArray . push ( amountOfScrollsArray . length ) ; } const imageHeight = amountOfScrollsArray . length === i ? scrollHeight - ( innerHeight * viewportScreenshots . length ) : innerHeight ; const imageYPosition = ( amountOfScrollsArray . length === i && amountOfScrollsArray . length !== 0 ) ? innerHeight - imageHeight : 0 ; viewportScreenshots . push ( { ... calculateDprData ( { canvasWidth : screenshotSize . width , canvasYPosition : scrollY , imageHeight : imageHeight , imageWidth : screenshotSize . width , imageYPosition : imageYPosition , } , devicePixelRatio ) , screenshot , } ) ; await executor ( hideScrollBars , false ) ; } if ( hideAfterFirstScroll . length > 0 ) { try { await executor ( hideRemoveElements , { hide : hideAfterFirstScroll , remove : [ ] } , false ) ; } catch ( e ) { logHiddenRemovedError ( e , logLevel ) ; } } return { ... calculateDprData ( { fullPageHeight : scrollHeight , fullPageWidth : screenshotSize . width , } , devicePixelRatio ) , data : viewportScreenshots , } ; } /**
 * Take a full page screenshots
 */ export async function getFullPageScreenshotsDataDesktop ( takeScreenshot , executor , options ) { const viewportScreenshots = [ ] ; const { devicePixelRatio , fullPageScrollTimeout , hideAfterFirstScroll , innerHeight , logLevel } = options ; let actualInnerHeight = innerHeight ; const amountOfScrollsArray = [ ] ; let scrollHeight ; let screenshotSize ; for ( let i = 0 ; i <= amountOfScrollsArray . length ; i ++ ) { const scrollY = actualInnerHeight * i ; await executor ( scrollToPosition , scrollY ) ; await waitFor ( fullPageScrollTimeout ) ; if ( i === 1 && hideAfterFirstScroll . length > 0 ) { try { await executor ( hideRemoveElements , { hide : hideAfterFirstScroll , remove : [ ] } , true ) ; } catch ( e ) { logHiddenRemovedError ( e , logLevel ) ; } } const screenshot = await takeBase64Screenshot ( takeScreenshot ) ; screenshotSize = getScreenshotSize ( screenshot , devicePixelRatio ) ; if ( i === 0 && screenshotSize . height !== actualInnerHeight ) { if ( Math . round ( screenshotSize . height ) === actualInnerHeight ) { actualInnerHeight = screenshotSize . height ; } } scrollHeight = await executor ( getDocumentScrollHeight ) ; if ( ( ( scrollY + actualInnerHeight ) < scrollHeight && screenshotSize . height === actualInnerHeight ) ) { amountOfScrollsArray . push ( amountOfScrollsArray . length ) ; } const imageHeight = amountOfScrollsArray . length === i ? scrollHeight - ( actualInnerHeight * viewportScreenshots . length ) : screenshotSize . height ; const imageYPosition = ( amountOfScrollsArray . length === i && amountOfScrollsArray . length !== 0 ) ? actualInnerHeight - imageHeight : 0 ; viewportScreenshots . push ( { ... calculateDprData ( { canvasWidth : screenshotSize . width , canvasYPosition : scrollY , imageHeight : imageHeight , imageWidth : screenshotSize . width , imageYPosition : imageYPosition , } , devicePixelRatio ) , screenshot , } ) ; } if ( hideAfterFirstScroll . length > 0 ) { try { await executor ( hideRemoveElements , { hide : hideAfterFirstScroll , remove : [ ] } , false ) ; } catch ( e ) { logHiddenRemovedError ( e , logLevel ) ; } } return { ... calculateDprData ( { fullPageHeight : scrollHeight , fullPageWidth : screenshotSize . width , } , devicePixelRatio ) , data : viewportScreenshots , } ; } /**
 * Take a screenshot
 */ export async function takeBase64Screenshot ( takeScreenshot ) { return takeScreenshot ( ) ; } TODO function logHiddenRemovedError ( error , logLevel ) { if ( logLevel === LogLevel . debug || logLevel === LogLevel . warn ) { console . log ( yellow ( `
#####################################################################################
 WARNING:
 (One of) the elements that needed to be hidden or removed could not be found on the
 page and caused this error
 Error: ${ error }
 We made sure the test didn't break.
#####################################################################################
` ) ) ; } }