From dfb4ad388ae7afa675ff5e333399666b4da999ff Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Thu, 10 Nov 2022 16:16:05 -0800 Subject: [PATCH] feat: support custom png comparator (#18689) This way we might experiment with different custom PNG comparators for VRT. --- packages/playwright-core/src/utils/comparators.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/playwright-core/src/utils/comparators.ts b/packages/playwright-core/src/utils/comparators.ts index 43140c2da2..029764bbe9 100644 --- a/packages/playwright-core/src/utils/comparators.ts +++ b/packages/playwright-core/src/utils/comparators.ts @@ -24,9 +24,19 @@ export type ImageComparatorOptions = { threshold?: number, maxDiffPixels?: numbe export type ComparatorResult = { diff?: Buffer; errorMessage: string; } | null; export type Comparator = (actualBuffer: Buffer | string, expectedBuffer: Buffer, options?: any) => ComparatorResult; +let customPNGComparator: Comparator | undefined; +if (process.env.PW_CUSTOM_PNG_COMPARATOR) { + try { + customPNGComparator = require(process.env.PW_CUSTOM_PNG_COMPARATOR); + if (typeof customPNGComparator !== 'function') + customPNGComparator = undefined; + } catch (e) { + } +} + export function getComparator(mimeType: string): Comparator { if (mimeType === 'image/png') - return compareImages.bind(null, 'image/png'); + return customPNGComparator ?? compareImages.bind(null, 'image/png'); if (mimeType === 'image/jpeg') return compareImages.bind(null, 'image/jpeg'); if (mimeType === 'text/plain')