Skip to content
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

🏗✅ Make visual diff tests hermetic #21935

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions build-system/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ app.use('/api/dont-show', (req, res) => {
});
});

app.use('/api/echo/query', (req, res) => {
const sourceOrigin = req.query['__amp_source_origin'];
if (sourceOrigin) {
res.setHeader('AMP-Access-Control-Allow-Source-Origin', sourceOrigin);
}
res.json(JSON.parse(req.query.data));
});

app.use('/api/echo/post', (req, res) => {
res.setHeader('Content-Type', 'application/json');
res.end(req.body);
Expand Down
41 changes: 36 additions & 5 deletions build-system/tasks/visual-diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,10 @@ async function launchBrowser() {
* @param {!puppeteer.Browser} browser a Puppeteer controlled browser.
* @param {JsonObject} viewport optional viewport size object with numeric
* fields `width` and `height`.
* @param {boolean} hermetic whether this test should be able to access external
* network resources (`false`) or be hermetic (`true`).
*/
async function newPage(browser, viewport = null) {
async function newPage(browser, viewport = null, hermetic = true) {
const width = viewport ? viewport.width : VIEWPORT_WIDTH;
const height = viewport ? viewport.height : VIEWPORT_HEIGHT;

Expand All @@ -216,6 +218,31 @@ async function newPage(browser, viewport = null) {
await page.setViewport({width, height});
page.setDefaultNavigationTimeout(NAVIGATE_TIMEOUT_MS);
await page.setJavaScriptEnabled(true);
if (hermetic) {
await page.setRequestInterception(true);
page.on('request', interceptedRequest => {
const requestUrl = new URL(interceptedRequest.url());
const mockedFilepath = path.join(
path.dirname(__filename), 'network-mocks', requestUrl.hostname,
encodeURIComponent(
`${requestUrl.pathname.substr(1)}${requestUrl.search}`)
.replace(/%2F/g, '/'));

if (requestUrl.hostname == HOST ||
requestUrl.hostname.endsWith(`.${HOST}`)) {
return interceptedRequest.continue();
} else if (fs.existsSync(mockedFilepath)) {
log('verbose', 'Mocked network request for',
colors.yellow(requestUrl.href), 'with file',
colors.cyan(mockedFilepath));
return interceptedRequest.respond(fs.readFileSync(mockedFilepath));
} else {
log('verbose', 'Blocked external network request for',
colors.yellow(requestUrl.href));
return interceptedRequest.abort('blockedbyclient');
}
});
}
return page;
}

Expand Down Expand Up @@ -400,7 +427,8 @@ async function snapshotWebpages(percy, browser, webpages) {
const name = testName ? `${pageName} (${testName})` : pageName;
log('verbose', 'Visual diff test', colors.yellow(name));

const page = await newPage(browser, viewport);
const page = await newPage(
browser, viewport, !webpage.allow_external_network_access);
log('verbose', 'Navigating to page', colors.yellow(webpage.url));

// Puppeteer is flaky when it comes to catching navigation requests, so
Expand Down Expand Up @@ -616,9 +644,11 @@ async function ensureOrBuildAmpRuntimeInTestMode_() {
}

function installPercy_() {
log('info', 'Running', colors.cyan('yarn'), 'to install Percy...');
execOrDie('npx yarn --cwd build-system/tasks/visual-diff',
{'stdio': 'ignore'});
if (!argv.noyarn) {
log('info', 'Running', colors.cyan('yarn'), 'to install Percy...');
execOrDie('npx yarn --cwd build-system/tasks/visual-diff',
{'stdio': 'ignore'});
}

puppeteer = require('puppeteer');
Percy = require('@percy/puppeteer').Percy;
Expand Down Expand Up @@ -661,6 +691,7 @@ gulp.task(
'percy_disabled':
' Disables Percy integration (for testing local changes only)',
'nobuild': ' Skip build',
'noyarn': ' Skip calling yarn to install dependencies',
},
}
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"keys":[{"alg":"RS256","e":"AQAB","ext":true,"kid":"1","kty":"RSA","n":"oDK9vY5WkwS25IJWhFTmyy_xTeBHA5b72On2FqhjZPLSwadlC0gZG0lvzPjxE1bakbAM3rR2mRJmtrKDAcZSZxIfxpVhG5e7yFAZURnKSKGHvLLwSeohnR6zHgZ0Rm6fnvBhYBpHGaFboPXgK1IjgVZ_aEq5CRj24JLvqovMtpJJXwJ1fndMprEfDAzw5rEzfZxvGP3QObEQENHAlyPe54Z0vfCYhiXLWhQuOyaKkVIf3xn7t6Pu7PbreCN9f-Ca8noVVKNUZCdlUqiQjXZZfu5pi8ZCto_HEN26hE3nqoEFyBWQwMvgJMhpkS2NjIX2sQuM5KangAkjJRe-Ej6aaQ"}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"keys":[{"alg":"RS256","e":"AQAB","ext":true,"kid":"1","kty":"RSA","n":"z43rjaJ9PLk1FHMEL31_ILXGtUTN03rxJ9amD9y3BRDpbTA-GkUKiQM07xAd8OXPUZRqcjvXQfc7b1RCEtwrcfx9oBRdF78QMA4tLLCqSHP0tSuqYF0fA7-GyTFWDcYzey90jRFNNWxjzKrvSazacE0TvJ8S_AVP4EV67VdbByCC1tpBzLhhy7RFHp2cXGTpWYUqZUAVUdJoeBuCho_zQz2au7c6sDaLiF-uYL9Td9MrZ6tSLo3MeMIZia4WgWqjTDICR0h-zlbHUd0K9CoXbGTt5nvkebXHmbKd99ma6zRYVlYNJTuSqsRCBNYtCTFVHIZeBlkjHKsQ46HTZPexZw"}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@font-face {
font-family: 'Georgia';
font-style: normal;
font-weight: 400;
src: local('Georgia'), url(https://fonts.gstatic.com/l/font?kit=-zkl91Ksy8U47Wnsfmm8xA&skey=ca45512e77838097&v=v9) format('truetype');
}
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 400;
src: local('Open Sans Regular'), local('OpenSans-Regular'), url(https://fonts.gstatic.com/s/opensans/v16/mem8YaGs126MiZpBA-UFVZ0e.ttf) format('truetype');
}
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 400;
src: local('Roboto'), local('Roboto-Regular'), url(https://fonts.gstatic.com/s/roboto/v19/KFOmCnqEu92Fr1Mu4mxP.ttf) format('truetype');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@font-face {
font-family: 'Questrial';
font-style: normal;
font-weight: 400;
src: local('Questrial'), local('Questrial-Regular'), url(https://fonts.gstatic.com/s/questrial/v8/QdVUSTchPBm7nuUeVf70viFg.ttf) format('truetype');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@font-face {
font-family: 'Roboto Condensed';
font-style: normal;
font-weight: 400;
src: local('Roboto Condensed'), local('RobotoCondensed-Regular'), url(https://fonts.gstatic.com/s/robotocondensed/v17/ieVl2ZhZI2eCN5jzbjEETS9weq8-19K7CA.ttf) format('truetype');
}
6 changes: 3 additions & 3 deletions examples/visual-tests/amp-accordion/amp-accordion.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ <h2 tabindex="0">Section 2. Move focus here and press enter/space to open.</h2>
<section>
<h2 tabindex="0">Section 3. <a href="https://google.com" target="_blank">Clicking this link should not expand/collapse this section.</a></h2>
<div>
<amp-img id="img1" srcset="https://lh4.googleusercontent.com/kDqWeHQ0Zt-UFeHAlc2ydE9AwK7W-kbMXBqyuNvPNy0mNSGAP7FOVB3_1iAOdbQSsbZByErbehvvSKtnTb1L5GoYreijfKwgYwpP1eLCHyl9Am7BSpwRuBABOAO12PtyPBTirdAadnxOmfq9dH_rsLSTaYGmNz1D5QIwXeWd8UeDNUQ3f-cMgvkq4ePqmKoe9t5ySqqLMZs-v3wTi2pd4jV_CurzcMB76k_b4lyD5w77NUowkSaQfscYVQpkDjo6OgG2nBiKJ2TITWVDu2rvt6NuEoOD9xaHgXuv81OnOjXCokxZd5K6TZiAH-Qm1jTKGMANklXiXt6hbwrN3QPA1mq2FardxGNLj1_oqOpXaqfUuj8LvejFRY6zJMGq0r6S_TEtPvbyulIg4PkKPIaVzi5nVdGrAWWoesWh-ORqmxZ4FIhdbd_Igsdh5AcMETBcZz3l5-IX0hmnyUeT5IOPSGw-p3Esgp_abwWB9-kElEiiHPD4QuQ_swsRu0NSFwfRi_QefnJQJ5UATng6iVP3K0g7uumHcwLtFId0vCeHp4A=w1024-h768-no" width=512 height=344></amp-img>
<amp-img id="img1" srcset="/examples/visual-tests/picsum.photos/image1043_1024x768.jpg" width=512 height=344></amp-img>
</div>
</section>
</amp-accordion>
Expand All @@ -54,8 +54,8 @@ <h1 tabindex="0">1) Click me first... two time - <i>2) Click me now ... two time
<section>
<h1 tabindex="0">
Click here to open
<amp-img src="https://lh3.googleusercontent.com/5rcQ32ml8E5ONp9f9-Rf78IofLb9QjS5_0mqsY1zEFc=w300-h200-no" layout="fixed" height=45 width=45 class="show-on-collapse"></amp-img>
<amp-img src="https://lh3.googleusercontent.com/pSECrJ82R7-AqeBCOEPGPM9iG9OEIQ_QXcbubWIOdkY=w300-h200-no" layout="fixed" height=45 width=45 class="show-on-expand"></amp-img>
<amp-img src="/examples/visual-tests/picsum.photos/image1026_300x200.jpg" layout="fixed" height=45 width=45 class="show-on-collapse"></amp-img>
<amp-img src="/examples/visual-tests/picsum.photos/image1068_300x169.jpg" layout="fixed" height=45 width=45 class="show-on-expand"></amp-img>
</h1>
<p>Bunch of awesome content</p>
</section>
Expand Down
12 changes: 6 additions & 6 deletions examples/visual-tests/amp-consent/amp-consent.amp.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@ <h1>amp-consent</h1>

<h3>Image that is blocked by '_till_responded' consent</h3>
<amp-img data-block-on-consent='_till_responded'
src="https://lh3.googleusercontent.com/pSECrJ82R7-AqeBCOEPGPM9iG9OEIQ_QXcbubWIOdkY=w300-h200-no-n"
src="/examples/visual-tests/picsum.photos/image1068_300x169.jpg"
width=30
height=20>
</amp-img>

<h3>Image that is blocked by '_till_accepted' consent</h3>
<amp-img data-block-on-consent='_till_accepted'
src="https://lh3.googleusercontent.com/pSECrJ82R7-AqeBCOEPGPM9iG9OEIQ_QXcbubWIOdkY=w300-h200-no-n"
src="/examples/visual-tests/picsum.photos/image1068_300x169.jpg"
width=30
height=20>
</amp-img>

<h3>Image that is blocked by '_auto_reject' consent</h3>
<amp-img data-block-on-consent='_auto_reject'
src="https://lh3.googleusercontent.com/pSECrJ82R7-AqeBCOEPGPM9iG9OEIQ_QXcbubWIOdkY=w300-h200-no-n"
src="/examples/visual-tests/picsum.photos/image1068_300x169.jpg"
width=30
height=20>
</amp-img>

<h3>Image that is blocked by 'default' consent</h3>
<amp-img
data-block-on-consent='default'
src="https://lh3.googleusercontent.com/pSECrJ82R7-AqeBCOEPGPM9iG9OEIQ_QXcbubWIOdkY=w300-h200-no-n"
src="/examples/visual-tests/picsum.photos/image1068_300x169.jpg"
width=30
height=20>
</amp-img>
Expand All @@ -47,14 +47,14 @@ <h3>Image that is blocked by default (not specified) consent</h3>
<amp-img
id="not-specified"
data-block-on-consent
src="https://lh3.googleusercontent.com/pSECrJ82R7-AqeBCOEPGPM9iG9OEIQ_QXcbubWIOdkY=w300-h200-no-n"
src="/examples/visual-tests/picsum.photos/image1068_300x169.jpg"
width=30
height=20>
</amp-img>

<h3>Image that is NOT blocked by consent</h3>
<amp-img
src="https://lh3.googleusercontent.com/pSECrJ82R7-AqeBCOEPGPM9iG9OEIQ_QXcbubWIOdkY=w300-h200-no-n"
src="/examples/visual-tests/picsum.photos/image1068_300x169.jpg"
width=30
height=20>
</amp-img>
Expand Down
4 changes: 3 additions & 1 deletion examples/visual-tests/amp-form/amp-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ module.exports = {
]);
},

// TODO(danielrozenberg): fix and restore this test
/*
'try to submit to a dead server': async (page, name) => {
await page.setRequestInterception(true);
page.on('request', interceptedRequest => interceptedRequest.abort());

await page.tap('#name');
Expand All @@ -63,4 +64,5 @@ module.exports = {
'div[submit-error] div[id^="rendered-message-amp-form-"]',
]);
}
*/
};
26 changes: 13 additions & 13 deletions examples/visual-tests/amp-lightbox-gallery.html
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ <h1>Experimental Lightbox 2.0</h1>
<time class="header-time" datetime="2015-09-14 13:00">September 14, 2015</time>
</header>
<figure>
<amp-img id="hero-img" lightbox="toronto" src="https://picsum.photos/1600/900?image=1075" layout="responsive" width="1600"
<amp-img id="hero-img" lightbox="toronto" src="/examples/visual-tests/picsum.photos/image1075_1600x900.jpg" layout="responsive" width="1600"
height="900" alt="Picture of CN tower.">
<span class="see-gallery">See all 4 skyscraper photos</span>
</amp-img>
Expand All @@ -363,19 +363,19 @@ <h1>Experimental Lightbox 2.0</h1>
<!-- toronto image gallery -->
<div hidden>
<figure>
<amp-img lightbox="toronto" src="https://picsum.photos/1600/900?image=972" layout="responsive" width="1600" height="900"
<amp-img lightbox="toronto" src="/examples/visual-tests/picsum.photos/image972_1600x900.jpg" layout="responsive" width="1600" height="900"
alt="Picture of four tall buildings">
</amp-img>
<figcaption>
Toronto's financial district it a metropolis of glass and steel.
</figcaption>
</figure>
<amp-img lightbox="toronto" src="https://picsum.photos/900/1600?image=948" layout="responsive" width="900" height="1600"
<amp-img lightbox="toronto" src="/examples/visual-tests/picsum.photos/image948_900x1600.jpg" layout="responsive" width="900" height="1600"
alt="Picture of a tall building" aria-describedby="img2desc">
<span id=img2desc>The brutalist architecture of this building is obvious from its small windows and raw concrete look.</span>
</amp-img>

<amp-img lightbox="toronto" src="https://picsum.photos/1600/1600?image=430" layout="responsive" width="1600" height="1600"
<amp-img lightbox="toronto" src="/examples/visual-tests/picsum.photos/image430_1600x1600.jpg" layout="responsive" width="1600" height="1600"
alt="Picture of new york city skyline" aria-describedby="img3desc">
<div id=img3desc><p>Compared to New York Cities skyline, Toronto falls short on density. Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Curabitur ullamcorper turpis vel commodo scelerisque. Phasellus luctus nunc ut elit cursus,
Expand Down Expand Up @@ -405,59 +405,59 @@ <h1>Experimental Lightbox 2.0</h1>
<div class="row">
<div class="box">
<div class="inner">
<amp-img lightbox="gallery" src="https://picsum.photos/1600/900?image=857" layout="fill" alt="Sed pharetra semper fringilla. Nulla">
<amp-img lightbox="gallery" src="/examples/visual-tests/picsum.photos/image857_1600x900.jpg" layout="fill" alt="Sed pharetra semper fringilla. Nulla">
</amp-img>
</div>
</div>
<div class="box">
<div class="inner">
<amp-img lightbox="gallery" src="https://picsum.photos/1600/1600?image=1081" layout="fill" alt="Sed pharetra semper fringilla. Nulla">
<amp-img lightbox="gallery" src="/examples/visual-tests/picsum.photos/image1081_1600x1600.jpg" layout="fill" alt="Sed pharetra semper fringilla. Nulla">
</amp-img>
</div>
</div>
<div class="box">
<div class="inner">
<amp-img lightbox="gallery" src="https://picsum.photos/900/1600?image=1076" layout="fill" alt="Sed pharetra semper fringilla. Nulla">
<amp-img lightbox="gallery" src="/examples/visual-tests/picsum.photos/image1076_900x1600.jpg" layout="fill" alt="Sed pharetra semper fringilla. Nulla">
</amp-img>
</div>
</div>
</div>
<div class="row">
<div class="box">
<div class="inner">
<amp-img lightbox="gallery" src="https://picsum.photos/1600/900?image=1067" layout="fill" alt="Sed pharetra semper fringilla. Nulla">
<amp-img lightbox="gallery" src="/examples/visual-tests/picsum.photos/image1067_1600x900.jpg" layout="fill" alt="Sed pharetra semper fringilla. Nulla">
</amp-img>
</div>
</div>
<div class="box">
<div class="inner">
<amp-img lightbox="gallery" src="https://picsum.photos/1600/1600?image=1048" layout="fill" alt="Sed pharetra semper fringilla. Nulla">
<amp-img lightbox="gallery" src="/examples/visual-tests/picsum.photos/image1048_1600x1600.jpg" layout="fill" alt="Sed pharetra semper fringilla. Nulla">
</amp-img>
</div>
</div>
<div class="box">
<div class="inner">
<amp-img lightbox="gallery" src="https://picsum.photos/900/1600?image=868" layout="fill" alt="Sed pharetra semper fringilla. Nulla">
<amp-img lightbox="gallery" src="/examples/visual-tests/picsum.photos/image868_900x1600.jpg" layout="fill" alt="Sed pharetra semper fringilla. Nulla">
</amp-img>
</div>
</div>
</div>
<div class="row">
<div class="box">
<div class="inner">
<amp-img lightbox="gallery" src="https://picsum.photos/1600/900?image=864" layout="fill" alt="Sed pharetra semper fringilla. Nulla">
<amp-img lightbox="gallery" src="/examples/visual-tests/picsum.photos/image864_1600x900.jpg" layout="fill" alt="Sed pharetra semper fringilla. Nulla">
</amp-img>
</div>
</div>
<div class="box">
<div class="inner">
<amp-img lightbox="gallery" src="https://picsum.photos/1600/1600?image=740" layout="fill" alt="Sed pharetra semper fringilla. Nulla">
<amp-img lightbox="gallery" src="/examples/visual-tests/picsum.photos/image740_1600x1600.jpg" layout="fill" alt="Sed pharetra semper fringilla. Nulla">
</amp-img>
</div>
</div>
<div class="box">
<div class="inner">
<amp-img lightbox="gallery" src="https://picsum.photos/900/1600?image=736" layout="fill" alt="Sed pharetra semper fringilla. Nulla">
<amp-img lightbox="gallery" src="/examples/visual-tests/picsum.photos/image736_900x1600.jpg" layout="fill" alt="Sed pharetra semper fringilla. Nulla">
</amp-img>
</div>
</div>
Expand Down
10 changes: 5 additions & 5 deletions examples/visual-tests/amp-story/amp-story-bookend.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@

<body>
<amp-story standalone publisher="AMP Team" title="Visual Diff Test"
publisher-logo-src="https://www.ampproject.org/static/img/icons/120.png"
poster-portrait-src="https://picsum.photos/900/1600?image=981"
poster-landscape-src="https://picsum.photos/1600/900?image=981"
poster-square-src="https://picsum.photos/1600/1600?image=981">
publisher-logo-src="/examples/visual-tests/photos/120.png"
poster-portrait-src="/examples/visual-tests/picsum.photos/image981_900x1600.jpg"
poster-landscape-src="/examples/visual-tests/picsum.photos/image981_1600x900.jpg"
poster-square-src="/examples/visual-tests/picsum.photos/image981_1600x1600.jpg">

<amp-story-page id="cover">
<amp-story-grid-layer template="fill">
<amp-video autoplay layout="fill" poster="https://picsum.photos/1600/900?image=981">
<amp-video autoplay layout="fill" poster="/examples/visual-tests/picsum.photos/image981_1600x900.jpg">
</amp-video>
</amp-story-grid-layer>
<amp-story-grid-layer template="vertical">
Expand Down
10 changes: 5 additions & 5 deletions examples/visual-tests/amp-story/amp-story-bookend.rtl.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@

<body>
<amp-story standalone publisher="AMP Team" title="Visual Diff Test"
publisher-logo-src="https://www.ampproject.org/static/img/icons/120.png"
poster-portrait-src="https://picsum.photos/900/1600?image=981"
poster-landscape-src="https://picsum.photos/1600/900?image=981"
poster-square-src="https://picsum.photos/1600/1600?image=981">
publisher-logo-src="/examples/visual-tests/photos/120.png"
poster-portrait-src="/examples/visual-tests/picsum.photos/image981_900x1600.jpg"
poster-landscape-src="/examples/visual-tests/picsum.photos/image981_1600x900.jpg"
poster-square-src="/examples/visual-tests/picsum.photos/image981_1600x1600.jpg">

<amp-story-page id="cover">
<amp-story-grid-layer template="fill">
<amp-video autoplay layout="fill" poster="https://picsum.photos/1600/900?image=981">
<amp-video autoplay layout="fill" poster="/examples/visual-tests/picsum.photos/image981_1600x900.jpg">
</amp-video>
</amp-story-grid-layer>
<amp-story-grid-layer template="vertical">
Expand Down