UNPKG

@wdio/image-comparison-core

Version:

Image comparison core module for @wdio/visual-service - WebdriverIO visual testing framework

54 lines (53 loc) 2.83 kB
import { takeElementScreenshot } from '../methods/takeElementScreenshots.js'; import beforeScreenshot from '../helpers/beforeScreenshot.js'; import afterScreenshot from '../helpers/afterScreenshot.js'; import { DEFAULT_RESIZE_DIMENSIONS } from '../helpers/constants.js'; import { canUseBidiScreenshot, getMethodOrWicOption } from '../helpers/utils.js'; import { createBeforeScreenshotOptions, buildAfterScreenshotOptions } from '../helpers/options.js'; /** * Saves an image of an element */ export default async function saveWebElement({ browserInstance, instanceData, folders, element, tag, saveElementOptions, }) { // 1. Set some variables const { addressBarShadowPadding, autoElementScroll, formatImageName, savePerInstance } = saveElementOptions.wic; const enableLegacyScreenshotMethod = getMethodOrWicOption(saveElementOptions.method, saveElementOptions.wic, 'enableLegacyScreenshotMethod'); const resizeDimensions = saveElementOptions.method.resizeDimensions || DEFAULT_RESIZE_DIMENSIONS; // 2. Prepare the screenshot const beforeOptions = createBeforeScreenshotOptions(instanceData, saveElementOptions.method, saveElementOptions.wic); const enrichedInstanceData = await beforeScreenshot(browserInstance, beforeOptions, true); const { deviceName, dimensions: { window: { devicePixelRatio, innerHeight, isEmulated, isLandscape, }, }, initialDevicePixelRatio, isAndroid, isAndroidChromeDriverScreenshot, isAndroidNativeWebScreenshot, isIOS, isMobile, } = enrichedInstanceData; // 3. Take the screenshot const elementScreenshotOptions = { addressBarShadowPadding, autoElementScroll, deviceName, devicePixelRatio: devicePixelRatio || 1, deviceRectangles: instanceData.deviceRectangles, element, isEmulated, initialDevicePixelRatio: initialDevicePixelRatio || 1, innerHeight, isAndroidNativeWebScreenshot, isAndroidChromeDriverScreenshot, isAndroid, isIOS, isLandscape, isMobile, resizeDimensions, toolBarShadowPadding: beforeOptions.toolBarShadowPadding, }; const shouldUseBidi = canUseBidiScreenshot(browserInstance) && !isMobile && !enableLegacyScreenshotMethod; const screenshotData = await takeElementScreenshot(browserInstance, elementScreenshotOptions, shouldUseBidi); // 4. Return the data const afterOptions = buildAfterScreenshotOptions({ base64Image: screenshotData.base64Image, folders, tag, isNativeContext: false, instanceData, enrichedInstanceData, beforeOptions, wicOptions: { formatImageName, savePerInstance, alwaysSaveActualImage: saveElementOptions.wic.alwaysSaveActualImage } }); return afterScreenshot(browserInstance, afterOptions); }