-
Notifications
You must be signed in to change notification settings - Fork 166
Take screenshots from pub_integration tests (store locally). #8575
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'dart:io'; | ||
|
|
||
| import 'package:path/path.dart' as p; | ||
| import 'package:puppeteer/puppeteer.dart'; | ||
|
|
||
| // Default screen with 16:10 ratio. | ||
| final desktopDeviceViewport = DeviceViewport(width: 1280, height: 800); | ||
|
|
||
| final _screenshotDir = Platform.environment['PUB_SCREENSHOT_DIR']; | ||
| final _isScreenshotDirSet = | ||
| _screenshotDir != null && _screenshotDir!.isNotEmpty; | ||
|
|
||
| // Set this variable to enable screenshot files to be updated with new takes. | ||
| // The default is to throw an exception to prevent accidental overrides from | ||
| // separate tests. | ||
| final _allowScreeshotUpdates = | ||
| Platform.environment['PUB_SCREENSHOT_UPDATE'] == '1'; | ||
|
|
||
| // Note: The default values are the last, so we don't need reset | ||
| // the original values after taking the screenshots. | ||
| final _themes = ['dark', 'light']; | ||
| final _viewports = { | ||
| 'mobile': DeviceViewport(width: 400, height: 800), | ||
| 'tablet': DeviceViewport(width: 768, height: 1024), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might I propose we don't do I'm assuming this is more about aspect ratio?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't do tablet? I think it is a good way to check how the site behaves in narrower screen, and tbh. I don't think we will manually check tablet screens, but may catch if something degrades.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Mostly the width.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm mostly thinking that we already have a LOT of screenshots, so I don't mind it, I'm only concerned that it's hard to get an overview.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep them for now, in the hope they are useful. If they are not, create too much noise, we may remove them or ignore them in most comparison reports. |
||
| 'desktop': desktopDeviceViewport, | ||
| }; | ||
|
|
||
| extension ScreenshotPageExt on Page { | ||
| Future<void> writeScreenshotToFile(String path) async { | ||
| await File(path).writeAsBytes(await screenshot()); | ||
| } | ||
|
|
||
| /// Takes screenshots **if** `PUB_SCREENSHOT_DIR` environment variable is set. | ||
| /// | ||
| /// Iterates over viewports and themes, and generates screenshot files with the | ||
| /// following pattern: | ||
| /// - `PUB_SCREENSHOT_DIR/$prefix-desktop-dark.png` | ||
| /// - `PUB_SCREENSHOT_DIR/$prefix-desktop-light.png` | ||
| /// - `PUB_SCREENSHOT_DIR/$prefix-mobile-dark.png` | ||
| /// - `PUB_SCREENSHOT_DIR/$prefix-mobile-light.png` | ||
| /// - `PUB_SCREENSHOT_DIR/$prefix-tablet-dark.png` | ||
| /// - `PUB_SCREENSHOT_DIR/$prefix-tablet-light.png` | ||
| Future<void> takeScreenshots({ | ||
| required String selector, | ||
| required String prefix, | ||
| }) async { | ||
| final handle = await $(selector); | ||
| await handle.takeScreenshots(prefix); | ||
| } | ||
| } | ||
|
|
||
| extension ScreenshotElementHandleExt on ElementHandle { | ||
| /// Takes screenshots **if** `PUB_SCREENSHOT_DIR` environment variable is set. | ||
| /// | ||
| /// Iterates over viewports and themes, and generates screenshot files with the | ||
| /// following pattern: | ||
| /// - `PUB_SCREENSHOT_DIR/$prefix-desktop-dark.png` | ||
| /// - `PUB_SCREENSHOT_DIR/$prefix-desktop-light.png` | ||
| /// - `PUB_SCREENSHOT_DIR/$prefix-mobile-dark.png` | ||
| /// - `PUB_SCREENSHOT_DIR/$prefix-mobile-light.png` | ||
| /// - `PUB_SCREENSHOT_DIR/$prefix-tablet-dark.png` | ||
| /// - `PUB_SCREENSHOT_DIR/$prefix-tablet-light.png` | ||
| Future<void> takeScreenshots(String prefix) async { | ||
| final body = await page.$('body'); | ||
| final bodyClassAttr = | ||
| (await body.evaluate('el => el.getAttribute("class")')) as String; | ||
| final bodyClasses = bodyClassAttr.split(' '); | ||
|
|
||
| for (final vp in _viewports.entries) { | ||
| await page.setViewport(vp.value); | ||
|
|
||
| for (final theme in _themes) { | ||
| final newClasses = [ | ||
| ...bodyClasses.where((c) => !c.endsWith('-theme')), | ||
| '$theme-theme', | ||
| ]; | ||
| await body.evaluate('(el, v) => el.setAttribute("class", v)', | ||
| args: [newClasses.join(' ')]); | ||
|
|
||
| // The presence of the element is verified, continue only if screenshots are enabled. | ||
| if (!_isScreenshotDirSet) continue; | ||
|
|
||
| final path = p.join(_screenshotDir!, '$prefix-${vp.key}-$theme.png'); | ||
| await _writeScreenshotToFile(path); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Future<void> _writeScreenshotToFile(String path) async { | ||
| final file = File(path); | ||
| final exists = await file.exists(); | ||
| if (exists && !_allowScreeshotUpdates) { | ||
| throw Exception('Screenshot update is detected in: $path'); | ||
| } | ||
| await file.parent.create(recursive: true); | ||
| await File(path).writeAsBytes(await screenshot()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about we just always store the screenshots in
.dart_tool/screenshots/..dart_tool/folder is already there..dart_tool/folder is always in.gitignore.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Borderline crazy idea:
Whenever, we create screenshots we:
.dart_tool/screenshots/current/git status --porcelain(meaning we have no changes), then:.dart_tool/screenshots/<revision>/.dart_tool/screenshots/comparison.html.dart_tool/screenshots/current/on the right side<revision>on the left side, where you pick<revision>from a dropdowngit logto find relevant revisions.dart_tool/screenshots/<revision>/exists locally.git log, and default to the most recent revision we have.We could also use
gh apito download artifacts from github:https://docs.github.com/en/rest/actions/artifacts?apiVersion=2022-11-28#get-an-artifact
Note
If wanted to have before/after images on github, we'll probably have to do something borderline crazy like uploading the images to a separate branch or a GCS bucket.
Which probably involve more infrastructure than we care to stand up and maintain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a crazy idea, but let's not rush to it yet, and reach it maybe step-by-step. There is an immediate benefit with this + a comparison tool, and then we can add further automatization with github artifacts and/or per-revision storage...