From 445c9c4a2760a753adc1159fd348ed76db1e8bdd Mon Sep 17 00:00:00 2001 From: Pawel Tomaszewski Date: Wed, 25 Sep 2024 11:26:16 +0200 Subject: [PATCH] fix types --- .../nightwatch/commands/sauceVisualCheck.ts | 12 ++++++++-- visual-js/visual-nightwatch/src/types.ts | 2 +- .../visual-wdio/src/SauceVisualService.ts | 16 +++++++++---- visual-js/visual-wdio/src/guarded-types.ts | 9 ++++++- visual-js/visual/src/types.ts | 5 ++-- visual-js/visual/src/utils.ts | 24 +++++++++---------- 6 files changed, 44 insertions(+), 24 deletions(-) diff --git a/visual-js/visual-nightwatch/src/nightwatch/commands/sauceVisualCheck.ts b/visual-js/visual-nightwatch/src/nightwatch/commands/sauceVisualCheck.ts index 0e13733e..11473121 100644 --- a/visual-js/visual-nightwatch/src/nightwatch/commands/sauceVisualCheck.ts +++ b/visual-js/visual-nightwatch/src/nightwatch/commands/sauceVisualCheck.ts @@ -10,7 +10,11 @@ import { } from '@saucelabs/visual'; import { getMetaInfo, getVisualApi } from '../../utils/api'; import { VISUAL_BUILD_ID_KEY } from '../../utils/constants'; -import { NightwatchAPI, NightwatchCustomCommandsModel } from 'nightwatch'; +import { + NightwatchAPI, + NightwatchCustomCommandsModel, + ScopedElement, +} from 'nightwatch'; import { CheckOptions, NightwatchIgnorable, RunnerSettings } from '../../types'; import type { Runnable } from 'mocha'; @@ -148,7 +152,11 @@ class SauceVisualCheck implements NightwatchCustomCommandsModel { sessionMetadata: metaInfo, suiteName, testName, - fullPageConfig: await getFullPageConfig(fullPage, options.fullPage), + fullPageConfig: await getFullPageConfig( + fullPage, + options.fullPage, + async (el) => await el.getId(), + ), clipElement: (await options.clipElement?.getId()) ?? clipElementFromClipSelector, captureDom: options.captureDom ?? globalCaptureDom, diff --git a/visual-js/visual-nightwatch/src/types.ts b/visual-js/visual-nightwatch/src/types.ts index ef700947..de7ecbab 100644 --- a/visual-js/visual-nightwatch/src/types.ts +++ b/visual-js/visual-nightwatch/src/types.ts @@ -31,7 +31,7 @@ export interface CheckOptions { ignore?: NightwatchIgnorable[]; regions?: RegionType[]; diffingMethod?: DiffingMethod; - fullPage?: FullPageScreenshotOptions; + fullPage?: FullPageScreenshotOptions; /** * Whether we should take a snapshot of the DOM to compare with as a part of the diffing process. */ diff --git a/visual-js/visual-wdio/src/SauceVisualService.ts b/visual-js/visual-wdio/src/SauceVisualService.ts index c2ffa3d0..8a1ed9f6 100644 --- a/visual-js/visual-wdio/src/SauceVisualService.ts +++ b/visual-js/visual-wdio/src/SauceVisualService.ts @@ -28,7 +28,12 @@ import { import logger from '@wdio/logger'; import chalk from 'chalk'; -import { Ignorable, isWdioElement, WdioElement } from './guarded-types.js'; +import { + FullPageScreenshotWdioOptions, + Ignorable, + isWdioElement, + WdioElement, +} from './guarded-types.js'; import { backOff } from 'exponential-backoff'; import type { Test } from '@wdio/types/build/Frameworks'; @@ -95,7 +100,7 @@ export type SauceVisualServiceOptions = { clipSelector?: string; clipElement?: WdioElement; region?: SauceRegion; - fullPage?: FullPageScreenshotOptions; + fullPage?: FullPageScreenshotWdioOptions; baselineOverride?: BaselineOverrideIn; }; @@ -131,7 +136,7 @@ export type CheckOptions = { captureDom?: boolean; diffingMethod?: DiffingMethod; disable?: (keyof DiffingOptionsIn)[]; - fullPage?: FullPageScreenshotOptions; + fullPage?: FullPageScreenshotWdioOptions; baselineOverride?: BaselineOverrideIn; }; @@ -165,7 +170,7 @@ export default class SauceVisualService implements Services.ServiceInstance { captureDom: boolean | undefined; clipSelector: string | undefined; clipElement: WdioElement | undefined; - fullPage?: FullPageScreenshotOptions; + fullPage?: FullPageScreenshotWdioOptions; apiClient: VisualApi; baselineOverride?: BaselineOverrideIn; @@ -442,9 +447,10 @@ export default class SauceVisualService implements Services.ServiceInstance { options.diffingMethod || this.diffingMethod || DiffingMethod.Balanced, suiteName: this.test?.parent, testName: this.test?.title, - fullPageConfig: await getFullPageConfig( + fullPageConfig: await getFullPageConfig( this.fullPage, options.fullPage, + (el) => el.elementId, ), baselineOverride: options.baselineOverride || this.baselineOverride, }); diff --git a/visual-js/visual-wdio/src/guarded-types.ts b/visual-js/visual-wdio/src/guarded-types.ts index 66dbdb79..9f8601ba 100644 --- a/visual-js/visual-wdio/src/guarded-types.ts +++ b/visual-js/visual-wdio/src/guarded-types.ts @@ -1,8 +1,15 @@ import { type } from 'arktype'; -import { makeValidate, RegionIn } from '@saucelabs/visual'; +import { + FullPageScreenshotOptions, + makeValidate, + RegionIn, +} from '@saucelabs/visual'; export type WdioElement = WebdriverIO.Element; +export type FullPageScreenshotWdioOptions = + FullPageScreenshotOptions; + const wdioElementType = type({ elementId: 'string', selector: 'string', diff --git a/visual-js/visual/src/types.ts b/visual-js/visual/src/types.ts index 16e5e779..6d78c0b5 100644 --- a/visual-js/visual/src/types.ts +++ b/visual-js/visual/src/types.ts @@ -1,9 +1,8 @@ import { RegionIn } from './graphql/__generated__/graphql'; import { SelectiveRegionOptions } from './common/selective-region'; import { SauceRegion } from './common/regions'; -import { WdioElement } from '@saucelabs/wdio-sauce-visual-service/build/guarded-types'; -export type FullPageScreenshotOptions = +export type FullPageScreenshotOptions = | boolean | { /** @@ -38,7 +37,7 @@ export type FullPageScreenshotOptions = /** * Element used for scrolling (available only in native apps) */ - scrollElement?: WdioElement | Promise; + scrollElement?: T | Promise; }; export type Ignorable = T | T[] | Promise | Promise | RegionIn; diff --git a/visual-js/visual/src/utils.ts b/visual-js/visual/src/utils.ts index bf1faac5..6472ea4a 100644 --- a/visual-js/visual/src/utils.ts +++ b/visual-js/visual/src/utils.ts @@ -13,10 +13,11 @@ import fs from 'fs/promises'; import * as os from 'node:os'; import { SauceRegion } from './common/regions'; -export const getFullPageConfig: ( - main?: FullPageScreenshotOptions | boolean, - local?: FullPageScreenshotOptions | boolean, -) => Promise = async (main, local) => { +export const getFullPageConfig: ( + main?: FullPageScreenshotOptions | boolean, + local?: FullPageScreenshotOptions | boolean, + getId?: (el: T) => Promise | string, +) => Promise = async (main, local, getId) => { const isNoConfig = !main && !local; const isLocalOff = local === false; @@ -24,16 +25,15 @@ export const getFullPageConfig: ( return; } - const globalCfg: FullPageScreenshotOptions = - typeof main === 'object' ? main : {}; - const localCfg: FullPageScreenshotOptions = - typeof local === 'object' ? local : {}; - const mergedCfg = { ...globalCfg, ...localCfg }; - if (mergedCfg.scrollElement) { - mergedCfg.scrollElement = (await mergedCfg.scrollElement).elementId; + const globalCfg: typeof main = typeof main === 'object' ? main : {}; + const localCfg: typeof main = typeof local === 'object' ? local : {}; + const { scrollElement, ...rest } = { ...globalCfg, ...localCfg }; + const result: FullPageConfigIn = rest; + if (scrollElement && getId) { + result.scrollElement = await getId(await scrollElement); } - return mergedCfg; + return result; }; export const isSkipMode = (): boolean => {