(options: ScreenshotAssertionOptions)
| 141 | } |
| 142 | |
| 143 | const assertScreenshotUnchanged = async (options: ScreenshotAssertionOptions) => { |
| 144 | const { |
| 145 | elementOrPage, |
| 146 | generatedScreenshotPath, |
| 147 | goldenScreenshotPath, |
| 148 | fileName, |
| 149 | maximumDiffThreshold, |
| 150 | maximumRetries, |
| 151 | retryCount = 1, |
| 152 | } = options; |
| 153 | const screenshotOptions = {...defaultScreenshotOpts, ...options.screenshotOptions, path: generatedScreenshotPath}; |
| 154 | await elementOrPage.screenshot(screenshotOptions); |
| 155 | |
| 156 | /** |
| 157 | * The user can do UPDATE_GOLDEN=accordion/basic.png npm run screenshotstest |
| 158 | * to update the golden image. This is useful if work has caused the |
| 159 | * screenshot to change and therefore the test goldens need to be updated. |
| 160 | */ |
| 161 | const shouldUpdate = |
| 162 | TestConfig.onDiff.update && (TestConfig.onDiff.update === true || TestConfig.onDiff.update.includes(fileName)); |
| 163 | const throwAfterGoldensUpdate = TestConfig.onDiff.throw; |
| 164 | |
| 165 | let onBotAndImageNotFound = false; |
| 166 | |
| 167 | // In the event that a golden does not exist, assume the generated screenshot is the new golden. |
| 168 | if (!fs.existsSync(goldenScreenshotPath)) { |
| 169 | // LUCI_CONTEXT is an environment variable present on the bots. |
| 170 | if (process.env.LUCI_CONTEXT !== undefined && !shouldUpdate) { |
| 171 | // If the image is missing, there's no point retrying the test N more times. |
| 172 | onBotAndImageNotFound = true; |
| 173 | throw ScreenshotError.fromGeneratedScreenshot( |
| 174 | `Failing test: in an environment with LUCI_CONTEXT and did not find a golden screenshot. |
| 175 | |
| 176 | Here's the image that this test generated as a base64: |
| 177 | |
| 178 | data:image/png;base64,${fs.readFileSync(generatedScreenshotPath, { |
| 179 | encoding: 'base64', |
| 180 | })} |
| 181 | `, |
| 182 | generatedScreenshotPath); |
| 183 | } |
| 184 | |
| 185 | console.log('Golden does not exist, using generated screenshot.'); |
| 186 | setGeneratedFileAsGolden(goldenScreenshotPath, generatedScreenshotPath); |
| 187 | if (throwAfterGoldensUpdate) { |
| 188 | throw ScreenshotError.fromGeneratedScreenshot( |
| 189 | 'Golden does not exist, using generated screenshot.', generatedScreenshotPath); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | try { |
| 194 | await compare(goldenScreenshotPath, generatedScreenshotPath, maximumDiffThreshold, shouldUpdate); |
| 195 | } catch (compareError) { |
| 196 | if (!onBotAndImageNotFound) { |
| 197 | console.log(`=> Test failed. Retrying (retry ${retryCount} of ${maximumRetries} maximum).`); |
| 198 | } |
| 199 | |
| 200 | if (retryCount === maximumRetries || onBotAndImageNotFound) { |
no test coverage detected