|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:path/path.dart' as p; |
| 8 | +import 'package:puppeteer/puppeteer.dart'; |
| 9 | + |
| 10 | +// Default screen with 16:10 ratio. |
| 11 | +final desktopDeviceViewport = DeviceViewport(width: 1280, height: 800); |
| 12 | + |
| 13 | +final _screenshotDir = Platform.environment['SCREENSHOT_DIR']; |
| 14 | +final isScreenshotDirSet = _screenshotDir != null && _screenshotDir!.isNotEmpty; |
| 15 | + |
| 16 | +// Set this variable to enable screenshot files to be updated with new takes. |
| 17 | +// The default is to throw an exception to prevent accidental overrides from |
| 18 | +// separate tests. |
| 19 | +final _allowScreeshotUpdates = Platform.environment['SCREENSHOT_UPDATE'] == '1'; |
| 20 | + |
| 21 | +// Note: The default values are the last, so we don't need reset |
| 22 | +// the original values after taking the screenshots. |
| 23 | +final _themes = ['dark', 'light']; |
| 24 | +final _viewports = { |
| 25 | + 'mobile': DeviceViewport(width: 400, height: 800), |
| 26 | + 'tablet': DeviceViewport(width: 768, height: 1024), |
| 27 | + 'desktop': desktopDeviceViewport, |
| 28 | +}; |
| 29 | + |
| 30 | +extension ScreenshotPageExt on Page { |
| 31 | + Future<void> writeScreenshotToFile(String path) async { |
| 32 | + await File(path).writeAsBytes(await screenshot()); |
| 33 | + } |
| 34 | + |
| 35 | + /// Takes screenshots **if** `SCREENSHOT_DIR` environment variable is set. |
| 36 | + /// |
| 37 | + /// Iterates over viewports and themes, and generates screenshot files with the |
| 38 | + /// following pattern: |
| 39 | + /// - `SCREENSHOT_DIR/$prefix-desktop-dark.png` |
| 40 | + /// - `SCREENSHOT_DIR/$prefix-desktop-light.png` |
| 41 | + /// - `SCREENSHOT_DIR/$prefix-mobile-dark.png` |
| 42 | + /// - `SCREENSHOT_DIR/$prefix-mobile-light.png` |
| 43 | + /// - `SCREENSHOT_DIR/$prefix-tablet-dark.png` |
| 44 | + /// - `SCREENSHOT_DIR/$prefix-tablet-light.png` |
| 45 | + Future<void> takeScreenshots({ |
| 46 | + required String selector, |
| 47 | + required String prefix, |
| 48 | + }) async { |
| 49 | + final handle = await $(selector); |
| 50 | + await handle.takeScreenshots(prefix); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +extension ScreenshotElementHandleExt on ElementHandle { |
| 55 | + /// Takes screenshots **if** `SCREENSHOT_DIR` environment variable is set. |
| 56 | + /// |
| 57 | + /// Iterates over viewports and themes, and generates screenshot files with the |
| 58 | + /// following pattern: |
| 59 | + /// - `SCREENSHOT_DIR/$prefix-desktop-dark.png` |
| 60 | + /// - `SCREENSHOT_DIR/$prefix-desktop-light.png` |
| 61 | + /// - `SCREENSHOT_DIR/$prefix-mobile-dark.png` |
| 62 | + /// - `SCREENSHOT_DIR/$prefix-mobile-light.png` |
| 63 | + /// - `SCREENSHOT_DIR/$prefix-tablet-dark.png` |
| 64 | + /// - `SCREENSHOT_DIR/$prefix-tablet-light.png` |
| 65 | + Future<void> takeScreenshots(String prefix) async { |
| 66 | + final body = await page.$('body'); |
| 67 | + final bodyClassAttr = (await body |
| 68 | + .evaluate<String>('el => el.getAttribute("class")')) as String; |
| 69 | + final bodyClasses = [ |
| 70 | + ...bodyClassAttr.split(' '), |
| 71 | + '--ongoing-screenshot', |
| 72 | + ]; |
| 73 | + |
| 74 | + for (final vp in _viewports.entries) { |
| 75 | + await page.setViewport(vp.value); |
| 76 | + |
| 77 | + for (final theme in _themes) { |
| 78 | + final newClasses = [ |
| 79 | + ...bodyClasses.where((c) => !c.endsWith('-theme')), |
| 80 | + '$theme-theme', |
| 81 | + ]; |
| 82 | + await body.evaluate<String>('(el, v) => el.setAttribute("class", v)', |
| 83 | + args: [newClasses.join(' ')]); |
| 84 | + |
| 85 | + // The presence of the element is verified, continue only if screenshots are enabled. |
| 86 | + if (!isScreenshotDirSet) continue; |
| 87 | + |
| 88 | + // Arbitrary delay in the hope that potential ongoing updates complete. |
| 89 | + await Future<void>.delayed(Duration(milliseconds: 500)); |
| 90 | + |
| 91 | + final path = p.join(_screenshotDir!, '$prefix-${vp.key}-$theme.png'); |
| 92 | + await _writeScreenshotToFile(path); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // restore the original body class attributes |
| 97 | + await body.evaluate<String>('(el, v) => el.setAttribute("class", v)', |
| 98 | + args: [bodyClassAttr]); |
| 99 | + } |
| 100 | + |
| 101 | + Future<void> _writeScreenshotToFile(String path) async { |
| 102 | + final file = File(path); |
| 103 | + final exists = file.existsSync(); |
| 104 | + if (exists && !_allowScreeshotUpdates) { |
| 105 | + throw Exception('Screenshot update is detected in: $path'); |
| 106 | + } |
| 107 | + await file.parent.create(recursive: true); |
| 108 | + await File(path).writeAsBytes(await screenshot()); |
| 109 | + } |
| 110 | +} |
0 commit comments