Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Test 3
  • Loading branch information
devversion committed Jun 22, 2021
commit 1b2ac82e23bb91f6f23902c831ed2f01beb680b8
Binary file modified goldens/kitchen-sink-prerendered.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
"parse5": "^6.0.1",
"postcss": "^8.2.1",
"protractor": "^7.0.0",
"puppeteer-core": "^10.0.0",
"reflect-metadata": "^0.1.3",
"requirejs": "^2.3.6",
"rollup": "~2.42.2",
Expand Down
2 changes: 1 addition & 1 deletion src/universal-app/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ts_library(
"@npm//@types/selenium-webdriver",
"@npm//fast-png",
"@npm//pixelmatch",
"@npm//selenium-webdriver",
"@npm//puppeteer-core",
],
)

Expand Down
47 changes: 28 additions & 19 deletions src/universal-app/ssr-screenshot-test-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
*/

import {runfiles} from '@bazel/runfiles';
import * as webdriver from 'selenium-webdriver';
import {readFileSync, writeFileSync} from 'fs';
import {decode, encode, PNGDataArray} from 'fast-png';
import {join} from 'path';
import {launch, Page} from 'puppeteer-core';

const pixelmatch = require('pixelmatch');

Expand All @@ -23,7 +23,12 @@ const pixelmatch = require('pixelmatch');
* https://github.com/bazelbuild/rules_webtesting/blob/06023bb3/web/internal/metadata.bzl#L69-L82
*/
interface WebTestMetadata {
capabilities: any;
/**
* List of web test files for the current browser. We limit our type to Chromium which
* will be extracted at build time. More details on the properties:
* https://github.com/bazelbuild/rules_webtesting/blob/34c659ab3e78f41ebe6453bee6201a69aef90f56/go/metadata/web_test_files.go#L29.
*/
webTestFiles: {namedFiles: {CHROMIUM?: string}}[];
}

if (process.env['WEB_TEST_METADATA'] === undefined) {
Expand All @@ -35,6 +40,9 @@ if (process.env['WEB_TEST_METADATA'] === undefined) {
const webTestMetadata: WebTestMetadata =
require(runfiles.resolve(process.env['WEB_TEST_METADATA']));

/** Path to Chromium extracted from the Bazel `web_test` metadata. */
const chromiumExecutableRootPath = webTestMetadata.webTestFiles?.[0].namedFiles.CHROMIUM;

/** Path to a directory where undeclared test artifacts can be stored. e.g. a diff file. */
const testOutputDirectory = process.env.TEST_UNDECLARED_OUTPUTS_DIR!;

Expand All @@ -58,23 +66,25 @@ if (require.main === module) {
/** Entry point for the screenshot test runner. */
async function main(goldenPath: string, approveGolden: boolean) {
const outputPath = await renderKitchenSinkOnServer();
const driver = await new webdriver.Builder()
.usingServer(process.env.WEB_TEST_WEBDRIVER_SERVER!)
.withCapabilities(webTestMetadata.capabilities)
.build();
const browser = await launch({
executablePath: runfiles.resolve(chromiumExecutableRootPath!),
headless: false,
});
const page = await browser.newPage();

await driver.get(`file://${outputPath}`);
await updateBrowserViewportToMatchContent(driver);
const currentScreenshotBase64 = await driver.takeScreenshot();
await driver.close();

await page.goto(`file://${outputPath}`);
await updateBrowserViewportToMatchContent(page);
const currentScreenshotBuffer = await page.screenshot({encoding: 'binary'}) as Buffer;
await browser.close();

if (approveGolden) {
writeFileSync(goldenPath, currentScreenshotBase64, 'base64');
writeFileSync(goldenPath, currentScreenshotBuffer);
console.info('Golden screenshot updated.');
return;
}

const currentScreenshot = decode(Buffer.from(currentScreenshotBase64, 'base64'));
const currentScreenshot = decode(currentScreenshotBuffer);
const goldenScreenshot = decode(readFileSync(goldenPath));
const diffImageData: PNGDataArray = new Uint8Array({length: currentScreenshot.data.length});
const numDiffPixels = pixelmatch(goldenScreenshot.data, currentScreenshot.data, diffImageData,
Expand Down Expand Up @@ -110,14 +120,13 @@ async function renderKitchenSinkOnServer(): Promise<string> {
* becomes visible without any scrollbars. This is useful for screenshots as it
* allows Selenium to take full-page screenshots.
*/
async function updateBrowserViewportToMatchContent(driver: webdriver.WebDriver) {
const windowFrameHeight = await driver.executeScript<number>(
'return window.outerHeight - window.innerHeight');
const bodyScrollHeight = await driver.executeScript<number>(
'return document.body.scrollHeight');
async function updateBrowserViewportToMatchContent(page: Page) {
const bodyScrollHeight = await page.evaluate(() => document.body.scrollHeight);
// We use a hard-coded large width for the window, so that the screenshot does not become
// too large vertically. This also helps with potential webdriver screenshot issues where
// screenshots render incorrectly if the window height has been increased too much.
await driver.manage().window().setSize(
screenshotBrowserWidth, bodyScrollHeight + windowFrameHeight);
await page.setViewport({
width: screenshotBrowserWidth,
height: bodyScrollHeight,
});
}
114 changes: 105 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2553,6 +2553,13 @@
dependencies:
yaml "*"

"@types/yauzl@^2.9.1":
version "2.9.1"
resolved "https://registry.yarnpkg.com/@types/yauzl/-/yauzl-2.9.1.tgz#d10f69f9f522eef3cf98e30afb684a1e1ec923af"
integrity sha512-A1b8SU4D10uoPjwb0lnHmmu8wZhR9d+9o2PKBQT2jU5YPTKsxac6M2qGAdY7VcL+dHHhARVUDmeg0rOrcd9EjA==
dependencies:
"@types/node" "*"

"@types/youtube@^0.0.42":
version "0.0.42"
resolved "https://registry.yarnpkg.com/@types/youtube/-/youtube-0.0.42.tgz#c3568e3e82e04cbd00cac3b66564ebcd21fd9d90"
Expand Down Expand Up @@ -3628,7 +3635,7 @@ buffer-alloc@^1.2.0:
buffer-alloc-unsafe "^1.1.0"
buffer-fill "^1.0.0"

buffer-crc32@^0.2.1, buffer-crc32@^0.2.13:
buffer-crc32@^0.2.1, buffer-crc32@^0.2.13, buffer-crc32@~0.2.3:
version "0.2.13"
resolved "https://registry.yarnpkg.com/buffer-crc32/-/buffer-crc32-0.2.13.tgz#0d333e3f00eac50aa1454abd30ef8c2a5d9a7242"
integrity sha1-DTM+PwDqxQqhRUq9MO+MKl2ackI=
Expand Down Expand Up @@ -3658,7 +3665,7 @@ buffer-indexof-polyfill@~1.0.0:
resolved "https://registry.yarnpkg.com/buffer-indexof-polyfill/-/buffer-indexof-polyfill-1.0.2.tgz#d2732135c5999c64b277fcf9b1abe3498254729c"
integrity sha512-I7wzHwA3t1/lwXQh+A5PbNvJxgfo5r3xulgpYDB5zckTu/Z9oUK9biouBKQUjEqzaz3HnAT6TYoovmE+GqSf7A==

buffer@^5.5.0:
buffer@^5.2.1, buffer@^5.5.0:
version "5.7.1"
resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.7.1.tgz#ba62e7c13133053582197160851a8f648e99eed0"
integrity sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==
Expand Down Expand Up @@ -5170,6 +5177,11 @@ dev-ip@^1.0.1:
resolved "https://registry.yarnpkg.com/dev-ip/-/dev-ip-1.0.1.tgz#a76a3ed1855be7a012bb8ac16cb80f3c00dc28f0"
integrity sha1-p2o+0YVb56ASu4rBbLgPPADcKPA=

[email protected]:
version "0.0.883894"
resolved "https://registry.yarnpkg.com/devtools-protocol/-/devtools-protocol-0.0.883894.tgz#d403f2c75cd6d71c916aee8dde9258da988a4da9"
integrity sha512-33idhm54QJzf3Q7QofMgCvIVSd2o9H3kQPWaKT/fhoZh+digc+WSiMhbkeG3iN79WY4Hwr9G05NpbhEVrsOYAg==

dgeni-packages@^0.28.4:
version "0.28.4"
resolved "https://registry.yarnpkg.com/dgeni-packages/-/dgeni-packages-0.28.4.tgz#53a3e6700b8d8f6be168cadcc9fdb36e1d7011d3"
Expand Down Expand Up @@ -5951,6 +5963,17 @@ extglob@^2.0.4:
snapdragon "^0.8.1"
to-regex "^3.0.1"

[email protected]:
version "2.0.1"
resolved "https://registry.yarnpkg.com/extract-zip/-/extract-zip-2.0.1.tgz#663dca56fe46df890d5f131ef4a06d22bb8ba13a"
integrity sha512-GDhU9ntwuKyGXdZBUgTIe+vXnWj0fppUEtMDL0+idd5Sta8TGpHssn/eusA9mrPr9qNDym6SxAYZjNvCn/9RBg==
dependencies:
debug "^4.1.1"
get-stream "^5.1.0"
yauzl "^2.10.0"
optionalDependencies:
"@types/yauzl" "^2.9.1"

[email protected]:
version "1.3.0"
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.3.0.tgz#96918440e3041a7a414f8c52e3c574eb3c3e1e05"
Expand Down Expand Up @@ -6051,6 +6074,13 @@ fastq@^1.6.0:
dependencies:
reusify "^1.0.0"

fd-slicer@~1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/fd-slicer/-/fd-slicer-1.1.0.tgz#25c7c89cb1f9077f8891bbe61d8f390eae256f1e"
integrity sha1-JcfInLH5B3+IkbvmHY85Dq4lbx4=
dependencies:
pend "~1.2.0"

fecha@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/fecha/-/fecha-4.2.0.tgz#3ffb6395453e3f3efff850404f0a59b6747f5f41"
Expand Down Expand Up @@ -6171,7 +6201,7 @@ find-up@^2.0.0:
dependencies:
locate-path "^2.0.0"

find-up@^4.1.0:
find-up@^4.0.0, find-up@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/find-up/-/find-up-4.1.0.tgz#97afe7d6cdc0bc5928584b7c8d7b16e8a9aa5d19"
integrity sha512-PpOwAdQ/YlXQ2vj8a3h8IipDuYRi3wceVQQGYWxNINccq40Anw7BlsEXCMbt1Zt+OLA6Fq9suIpIWD0OsnISlw==
Expand Down Expand Up @@ -7350,7 +7380,7 @@ http-signature@~1.2.0:
jsprim "^1.2.2"
sshpk "^1.7.0"

https-proxy-agent@5, https-proxy-agent@^5.0.0:
https-proxy-agent@5, https-proxy-agent@5.0.0, https-proxy-agent@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-5.0.0.tgz#e2a90542abb68a762e0a0850f6c9edadfd8506b2"
integrity sha512-EkYm5BcKUGiduxzSt3Eppko+PiNWNEpa4ySk9vTC6wDsQJW9rHSa+UhGNJoRYp7bz6Ht1eaRIa6QaJqO5rCFbA==
Expand Down Expand Up @@ -9670,7 +9700,7 @@ node-emoji@^1.4.1:
dependencies:
lodash.toarray "^4.4.0"

node-fetch@^2.3.0, node-fetch@^2.6.0, node-fetch@^2.6.1:
node-fetch@2.6.1, node-fetch@^2.3.0, node-fetch@^2.6.0, node-fetch@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
Expand Down Expand Up @@ -10476,6 +10506,11 @@ [email protected]:
dependencies:
through "~2.3"

pend@~1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/pend/-/pend-1.2.0.tgz#7a57eb550a6783f9115331fcf4663d5c8e007a50"
integrity sha1-elfrVQpng/kRUzH89GY9XI4AelA=

performance-now@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
Expand Down Expand Up @@ -10520,6 +10555,13 @@ pixelmatch@^5.2.1:
dependencies:
pngjs "^4.0.1"

[email protected]:
version "4.2.0"
resolved "https://registry.yarnpkg.com/pkg-dir/-/pkg-dir-4.2.0.tgz#f099133df7ede422e81d1d8448270eeb3e4261f3"
integrity sha512-HRDzbaKjC+AOWVXxAU/x54COGeIv9eb+6CkDSQoNTt4XyWoIJvuPsXizxu/Fr23EiekbtZwmh1IcIG/l/a10GQ==
dependencies:
find-up "^4.0.0"

plist@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/plist/-/plist-3.0.1.tgz#a9b931d17c304e8912ef0ba3bdd6182baf2e1f8c"
Expand Down Expand Up @@ -10725,6 +10767,11 @@ process-nextick-args@~1.0.6:
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-1.0.7.tgz#150e20b756590ad3f91093f25a4f2ad8bff30ba3"
integrity sha1-FQ4gt1ZZCtP5EJPyWk8q2L/zC6M=

[email protected]:
version "2.0.1"
resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.1.tgz#c9242169342b1c29d275889c95734621b1952e31"
integrity sha512-OE+a6vzqazc+K6LxJrX5UPyKFvGnL5CYmq2jFGNIBWHpc4QyE49/YOumcrpQFJpfejmvRtbJzgO1zPmMCqlbBg==

progress@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8"
Expand Down Expand Up @@ -10850,7 +10897,7 @@ proxy-agent@^4.0.0:
proxy-from-env "^1.0.0"
socks-proxy-agent "^5.0.0"

proxy-from-env@^1.0.0:
proxy-from-env@1.1.0, proxy-from-env@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==
Expand Down Expand Up @@ -10914,6 +10961,24 @@ pupa@^2.0.1:
dependencies:
escape-goat "^2.0.0"

puppeteer-core@^10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/puppeteer-core/-/puppeteer-core-10.0.0.tgz#5bf787ad95b5f70ea61634d8c80ffca9f2e92b6e"
integrity sha512-kaNsKhNYcayHnlwpkBf1w/lhyi1zUSHGLgh5z6DwhTbTrVN0pQHjWj7/TNBooop5Ehv0H7KFuH5QTbxrRFeDdA==
dependencies:
debug "4.3.1"
devtools-protocol "0.0.883894"
extract-zip "2.0.1"
https-proxy-agent "5.0.0"
node-fetch "2.6.1"
pkg-dir "4.2.0"
progress "2.0.1"
proxy-from-env "1.1.0"
rimraf "3.0.2"
tar-fs "2.0.0"
unbzip2-stream "1.3.3"
ws "7.4.6"

[email protected]:
version "1.4.1"
resolved "https://registry.yarnpkg.com/q/-/q-1.4.1.tgz#55705bcd93c5f3673530c2c2cbc0c2b3addc286e"
Expand Down Expand Up @@ -11518,7 +11583,7 @@ rimraf@2, rimraf@^2.2.8, rimraf@^2.5.2, rimraf@^2.5.4, rimraf@^2.6.0, rimraf@^2.
dependencies:
glob "^7.1.3"

rimraf@^3.0.0, rimraf@^3.0.2:
rimraf@3.0.2, rimraf@^3.0.0, rimraf@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.2.tgz#f1a5402ba6220ad52cc1282bac1ae3aa49fd061a"
integrity sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==
Expand Down Expand Up @@ -12738,7 +12803,17 @@ tapable@^2.2.0:
resolved "https://registry.yarnpkg.com/tapable/-/tapable-2.2.0.tgz#5c373d281d9c672848213d0e037d1c4165ab426b"
integrity sha512-FBk4IesMV1rBxX2tfiK8RAmogtWn53puLOQlvO8XuwlgxcYbP4mVPS9Ph4aeamSyyVjOl24aYWAuc8U5kCVwMw==

tar-stream@^2.1.4:
[email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/tar-fs/-/tar-fs-2.0.0.tgz#677700fc0c8b337a78bee3623fdc235f21d7afad"
integrity sha512-vaY0obB6Om/fso8a8vakQBzwholQ7v5+uy+tF3Ozvxv1KNezmVQAiWtcNmMHFSFPqL3dJA8ha6gdtFbfX9mcxA==
dependencies:
chownr "^1.1.1"
mkdirp "^0.5.1"
pump "^3.0.0"
tar-stream "^2.0.0"

tar-stream@^2.0.0, tar-stream@^2.1.4:
version "2.2.0"
resolved "https://registry.yarnpkg.com/tar-stream/-/tar-stream-2.2.0.tgz#acad84c284136b060dc3faa64474aa9aebd77287"
integrity sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ==
Expand Down Expand Up @@ -12866,7 +12941,7 @@ through2@^4.0.0:
dependencies:
readable-stream "3"

through@2, "through@>=2.2.7 <3", [email protected], through@^2.3.6, through@~2.3, through@~2.3.1:
through@2, "through@>=2.2.7 <3", [email protected], through@^2.3.6, through@^2.3.8, through@~2.3, through@~2.3.1:
version "2.3.8"
resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5"
integrity sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=
Expand Down Expand Up @@ -13247,6 +13322,14 @@ ultron@~1.1.0:
resolved "https://registry.yarnpkg.com/ultron/-/ultron-1.1.1.tgz#9fe1536a10a664a65266a1e3ccf85fd36302bc9c"
integrity sha512-UIEXBNeYmKptWH6z8ZnqTeS8fV74zG0/eRU9VGkpzz+LIJNs8W/zM/L+7ctCkRrgbNnnR0xxw4bKOr0cW0N0Og==

[email protected]:
version "1.3.3"
resolved "https://registry.yarnpkg.com/unbzip2-stream/-/unbzip2-stream-1.3.3.tgz#d156d205e670d8d8c393e1c02ebd506422873f6a"
integrity sha512-fUlAF7U9Ah1Q6EieQ4x4zLNejrRvDWUYmxXUpN3uziFYCHapjWFaCAnreY9bGgxzaMCFAPPpYNng57CypwJVhg==
dependencies:
buffer "^5.2.1"
through "^2.3.8"

unc-path-regex@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/unc-path-regex/-/unc-path-regex-0.1.2.tgz#e73dd3d7b0d7c5ed86fbac6b0ae7d8c6a69d50fa"
Expand Down Expand Up @@ -13879,6 +13962,11 @@ write-file-atomic@^3.0.0, write-file-atomic@^3.0.3:
signal-exit "^3.0.2"
typedarray-to-buffer "^3.1.5"

[email protected]:
version "7.4.6"
resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.6.tgz#5654ca8ecdeee47c33a9a4bf6d28e2be2980377c"
integrity sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A==

ws@^7.2.3, ws@~7.4.2:
version "7.4.3"
resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.3.tgz#1f9643de34a543b8edb124bdcbc457ae55a6e5cd"
Expand Down Expand Up @@ -14082,6 +14170,14 @@ yargs@^7.1.0:
y18n "^3.2.1"
yargs-parser "^5.0.0"

yauzl@^2.10.0:
version "2.10.0"
resolved "https://registry.yarnpkg.com/yauzl/-/yauzl-2.10.0.tgz#c7eb17c93e112cb1086fa6d8e51fb0667b79a5f9"
integrity sha1-x+sXyT4RLLEIb6bY5R+wZnt5pfk=
dependencies:
buffer-crc32 "~0.2.3"
fd-slicer "~1.1.0"

[email protected]:
version "0.1.2"
resolved "https://registry.yarnpkg.com/yeast/-/yeast-0.1.2.tgz#008e06d8094320c372dbc2f8ed76a0ca6c8ac419"
Expand Down