import { join } from 'path' ; import { DESKTOP , PLATFORMS } from './constants' ; import { ensureDirSync } from 'fs-extra' ; import { FormatFileDefaults , FormatFileNameOptions , GetAddressBarShadowPaddingOptions , GetAndCreatePathOptions , GetToolBarShadowPaddingOptions , ScreenshotSize } from './utils.interfaces' ; /**
 * Get and create a folder
 */ export function getAndCreatePath ( folder , options ) { const { browserName , deviceName , isMobile , savePerInstance } = options ; const instanceName = ( isMobile ? deviceName : `${ DESKTOP }_${ browserName }` ) . replace ( / /g , '_' ) ; const subFolder = savePerInstance ? instanceName : '' ; const folderName = join ( folder , subFolder ) ; ensureDirSync ( folderName ) ; return folderName ; } /**
 * Format the filename
 */ export function formatFileName ( options ) { let defaults = { browserName : options . browserName , browserVersion : options . browserVersion , deviceName : options . deviceName , dpr : options . devicePixelRatio , height : options . isMobile ? options . screenHeight : options . outerHeight , logName : options . logName , mobile : ( options . isMobile && options . isTestInBrowser ) ? options . browserName : options . isMobile ? 'app' : '' , name : options . name , platformName : options . platformName , platformVersion : options . platformVersion , tag : options . tag , width : options . isMobile ? options . screenWidth : options . outerWidth } ; let fileName = options . formatImageName ; Object . keys ( defaults ) . forEach ( ( value ) => { fileName = fileName . replace ( `{${ value }}` , defaults [ value ] ) ; } ) ; return `${ fileName . replace ( / /g , '_' ) }.png` ; } /**
 * Checks if it is mobile
 */ export function checkIsMobile ( platformName ) { return checkIsAndroid ( platformName ) || checkIsIos ( platformName ) ; } /**
 * Checks if the os is Android
 */ export function checkIsAndroid ( platformName ) { return platformName . toLowerCase ( ) === PLATFORMS . ANDROID ; } /**
 * Checks if the os is IOS
 */ export function checkIsIos ( platformName ) { return platformName . toLowerCase ( ) === PLATFORMS . IOS ; } /**
 * Checks if the test is executed in a browser
 */ export function checkTestInBrowser ( browserName ) { return browserName !== '' ; } /**
 * Checks if the test is executed in a browser on a mobile phone
 */ export function checkTestInMobileBrowser ( platformName , browserName ) { return checkIsMobile ( platformName ) && checkTestInBrowser ( browserName ) ; } /**
 * Checks if this is a native webscreenshot on android
 */ export function checkAndroidNativeWebScreenshot ( platformName , nativeWebscreenshot ) { return ( checkIsAndroid ( platformName ) && nativeWebscreenshot ) || false ; } /**
 * Checks if this is an Android chromedriver screenshot
 */ export function checkAndroidChromeDriverScreenshot ( platformName , nativeWebScreenshot ) { return checkIsAndroid ( platformName ) && ! checkAndroidNativeWebScreenshot ( platformName , nativeWebScreenshot ) ; } /**
 * Get the address bar shadow padding. This is only needed for Android native webscreenshot and iOS
 */ export function getAddressBarShadowPadding ( options ) { const { platformName , browserName , nativeWebScreenshot , addressBarShadowPadding , addShadowPadding , } = options ; const isTestInMobileBrowser = checkTestInMobileBrowser ( platformName , browserName ) ; const isAndroidNativeWebScreenshot = checkAndroidNativeWebScreenshot ( platformName , nativeWebScreenshot ) ; const isAndroid = checkIsAndroid ( platformName ) ; const isIos = checkIsIos ( platformName ) ; return isTestInMobileBrowser && ( ( isAndroidNativeWebScreenshot && isAndroid ) || isIos ) && addShadowPadding ? addressBarShadowPadding : 0 ; } /**
 * Get the tool bar shadow padding. This is only needed for iOS
 */ export function getToolBarShadowPadding ( options ) { const { platformName , browserName , toolBarShadowPadding , addShadowPadding } = options ; return checkTestInMobileBrowser ( platformName , browserName ) && checkIsIos ( platformName ) && addShadowPadding ? toolBarShadowPadding : 0 ; } /**
 * Calculate the data based on the device pixel ratio
 */ export function calculateDprData < T > ( data , devicePixelRatio ) { Object . keys ( data ) . map ( ( key ) => data [ key ] = typeof data [ key ] === 'number' ? data [ key ] * devicePixelRatio : data [ key ] ) ; return data ; } /**
 * Wait for an amount of milliseconds
 */ export async function waitFor ( milliseconds ) { return new Promise ( resolve => setTimeout ( ( ) => resolve ( ) , milliseconds ) ) ; } /**
 * Get the size of a screenshot in pixels without the device pixel ratio
 */ export function getScreenshotSize ( screenshot , devicePixelRation = 1 ) { return { height : Buffer . from ( screenshot , 'base64' ) . readUInt32BE ( 20 ) / devicePixelRation , width : Buffer . from ( screenshot , 'base64' ) . readUInt32BE ( 16 ) / devicePixelRation , } ; }