Skip to content

Instantly share code, notes, and snippets.

@ebidel
Last active October 1, 2019 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ebidel/b0ca932037d84bbf2c34f2336f48347f to your computer and use it in GitHub Desktop.
Save ebidel/b0ca932037d84bbf2c34f2336f48347f to your computer and use it in GitHub Desktop.
Testing lazy loaded image libraries
/**
* Copyright 2018 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author ebidel@ (Eric Bidelman)
*/
const puppeteer = require('puppeteer');
const {spawnSync} = require('child_process');
const DEMO_LINKS = 'https://avgp.github.io/lazyloading-experiments/';
// https://github.com/GoogleChromeLabs/puppeteer-examples/blob/master/lazyimages_without_scroll_events.js
const TEST_SCRIPT = `${__dirname}/lazyimages_without_scroll_events.js`;
async function collectDemos(url) {
const browser = await puppeteer.launch({headless: true});
console.info(`Collecting demo links from ${url}...`);
const page = await browser.newPage();
await page.goto(url);
const lis = await page.$$('#lazyloading-experiments + ul a');
const demosUrls = await Promise.all(lis.map(async li => {
const href = await li.getProperty('href');
return href.jsonValue();
}));
await browser.close();
return demosUrls;
}
(async() => {
const demosUrls = await collectDemos(DEMO_LINKS);
// Add a few more.
demosUrls.push('https://rawgit.com/GoogleChromeLabs/puppeteer-examples/master/html/lazyload.html'); // Should PASS.
demosUrls.push('https://css-tricks.com/examples/LazyLoading/'); // should FAIL.
demosUrls.push('http://dinbror.dk/blazy/'); // should FAIL.
console.info('Testing started...');
demosUrls.forEach((demo, i) => {
console.info(`\nTesting ${demo}.`);
const child = spawnSync('node', [TEST_SCRIPT, '-u', demo, '-o', `results${i}.html`], {
// detached: true,
stdio: 'inherit',
shell: true,
});
});
console.info('Testing complete...');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment