Skip to content

Commit

Permalink
IRIS-997 Let backend handle device pixel ratio in cypress
Browse files Browse the repository at this point in the history
  • Loading branch information
ebekebe committed May 27, 2024
1 parent 8895d2e commit 9566927
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 37 deletions.
42 changes: 6 additions & 36 deletions visual-js/visual-cypress/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ import {
VisualApiRegion,
BuildMode,
DiffingOptionsIn,
selectiveRegionOptionsToDiffingOptions,
RegionIn,
selectiveRegionOptionsToDiffingOptions

Check failure on line 21 in visual-js/visual-cypress/src/index.ts

View workflow job for this annotation

GitHub Actions / build

Insert `,`
} from '@saucelabs/visual';
import {
HasSauceConfig,
ScreenshotMetadata,
SauceVisualOptions,
SauceVisualViewport,
ResolvedVisualRegion,
SauceVisualViewport

Check failure on line 27 in visual-js/visual-cypress/src/index.ts

View workflow job for this annotation

GitHub Actions / build

Insert `,`
} from './types';
import { Logger } from './logger';
import { buildUrl, screenshotSectionStart } from './messages';
Expand Down Expand Up @@ -357,37 +355,6 @@ Sauce Labs Visual: Unable to create new build.
return;
}

metadata.regions ??= [];

// Check if there is a need to compute a ratio. Otherwise just use 1.
const needRatioComputation = metadata.regions
.map((region) => region.applyScalingRatio)
.reduce((prev, next) => prev || next, false);

const scalingRatio = needRatioComputation
? this.computeScalingRatio(metadata.viewport, {
height: screenshot.height,
width: screenshot.width,
})
: 1;

const ignoreRegions = metadata.regions.map(
(resolvedRegion: ResolvedVisualRegion): RegionIn => {
const { x, y, height, width } = resolvedRegion.element;

const ratio = resolvedRegion.applyScalingRatio ? scalingRatio : 1;
return {
x: Math.floor(x * ratio),
y: Math.floor(y * ratio),
height: Math.ceil((y + height) * ratio - Math.floor(y * ratio)),
width: Math.ceil((x + width) * ratio - Math.floor(x * ratio)),
name: '',
diffingOptions:
selectiveRegionOptionsToDiffingOptions(resolvedRegion),
};
},
);

// Publish image
try {
const screenshotId = await this.api.uploadSnapshot({
Expand All @@ -406,7 +373,10 @@ Sauce Labs Visual: Unable to create new build.
operatingSystemVersion: osInfo.version,
suiteName: metadata.suiteName,
testName: metadata.testName,
ignoreRegions,
ignoreRegions: metadata.regions.map((r) => ({
...r.element,
diffingOptions: selectiveRegionOptionsToDiffingOptions(r),
})),
device: metadata.viewport
? `Desktop (${metadata.viewport.width}x${metadata.viewport.height})`
: 'Desktop',
Expand Down
2 changes: 1 addition & 1 deletion visual-js/visual-cypress/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type ScreenshotMetadata = {
name: string;
testName: string;
suiteName: string;
regions?: ResolvedVisualRegion[];
regions: ResolvedVisualRegion[];
diffingMethod?: DiffingMethod;
diffingOptions?: DiffingOptionsIn;
viewport: SauceVisualViewport | undefined;
Expand Down

0 comments on commit 9566927

Please sign in to comment.