Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let backend handle device pixel ratio in cypress #65

Merged
merged 3 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions visual-js/.changeset/soft-shirts-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@saucelabs/cypress-visual-plugin": patch
---

Let backend handle device pixel ratio in cypress
3 changes: 0 additions & 3 deletions visual-js/visual-cypress/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,10 @@ const sauceVisualCheckCommand = (
hasError = true;
}

const applyScalingRatio = !isRegion(visualRegions[idx].element);

for (const plainRegion of regions[idx]) {
result.push({
...visualRegions[idx],
element: plainRegion,
applyScalingRatio,
} satisfies ResolvedVisualRegion);
}
}
Expand Down
38 changes: 4 additions & 34 deletions visual-js/visual-cypress/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ import {
BuildMode,
DiffingOptionsIn,
selectiveRegionOptionsToDiffingOptions,
RegionIn,
} from '@saucelabs/visual';
import {
HasSauceConfig,
ScreenshotMetadata,
SauceVisualOptions,
SauceVisualViewport,
ResolvedVisualRegion,
} 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
6 changes: 2 additions & 4 deletions visual-js/visual-cypress/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,14 @@ export type VisualRegion<
R extends Omit<object, 'element'> = PlainRegion | Cypress.Chainable,
> = { element: R } & SelectiveRegionOptions;

export type ResolvedVisualRegion = {
applyScalingRatio?: boolean;
} & VisualRegion<PlainRegion>;
export type ResolvedVisualRegion = VisualRegion<PlainRegion>;

export type ScreenshotMetadata = {
id: string;
name: string;
testName: string;
suiteName: string;
regions?: ResolvedVisualRegion[];
regions: ResolvedVisualRegion[];
diffingMethod?: DiffingMethod;
diffingOptions?: DiffingOptionsIn;
viewport: SauceVisualViewport | undefined;
Expand Down