diff --git a/.travis.yml b/.travis.yml index fd76256970e4..820dd19267cc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,17 +17,20 @@ cache: - lighthouse-viewer/node_modules - /home/travis/.rvm/gems/ install: + # if our e2e tests fail in the future it might be that we are not compatible + # with the latest puppeteer api so we probably need to run on chromimum + # @see https://github.com/GoogleChrome/lighthouse/pull/4640/files#r171425004 + - export PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=1 - yarn # travis can't handle the parallel install (without caches) - yarn run install-all:task:windows before_script: - export DISPLAY=:99.0 - - export CHROME_PATH="$(pwd)/chrome-linux/chrome" + # see comment above about puppeteer + - export CHROME_PATH="$(which google-chrome-stable)" - sh -e /etc/init.d/xvfb start - yarn build-all script: - - echo $TRAVIS_EVENT_TYPE; - - echo $TRAVIS_BRANCH - yarn bundlesize - yarn lint - yarn unit @@ -35,6 +38,7 @@ script: - yarn closure - yarn smoke - yarn smokehouse + - yarn test-extension # _JAVA_OPTIONS is breaking parsing of compiler output. See #3338. - unset _JAVA_OPTIONS - yarn compile-devtools diff --git a/lighthouse-extension/package.json b/lighthouse-extension/package.json index 495635a165d4..eede59ab3b87 100644 --- a/lighthouse-extension/package.json +++ b/lighthouse-extension/package.json @@ -6,7 +6,8 @@ }, "scripts": { "watch": "gulp watch", - "build": "gulp build:production" + "build": "gulp build:production", + "test": "mocha test/extension-test.js" }, "devDependencies": { "brfs": "^1.5.0", diff --git a/lighthouse-extension/test/extension-test.js b/lighthouse-extension/test/extension-test.js new file mode 100644 index 000000000000..17a795730fac --- /dev/null +++ b/lighthouse-extension/test/extension-test.js @@ -0,0 +1,159 @@ +/** + * @license 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. + */ +'use strict'; + +/* eslint-env mocha */ + +const path = require('path'); +const assert = require('assert'); +const fs = require('fs'); +const puppeteer = require('../../node_modules/puppeteer/index.js'); + +const lighthouseExtensionPath = path.resolve(__dirname, '../dist'); +const config = require(path.resolve(__dirname, '../../lighthouse-core/config/default.js')); + +const getAuditsOfCategory = category => config.categories[category].audits; + +describe('Lighthouse chrome extension', function() { + const manifestLocation = path.join(lighthouseExtensionPath, 'manifest.json'); + const lighthouseCategories = Object.keys(config.categories); + let browser; + let extensionPage; + let originalManifest; + + function getAuditElementsCount({category, selector}) { + return extensionPage.evaluate( + ({category, selector}) => + document.querySelector(`#${category}`).parentNode.querySelectorAll(selector).length, + {category, selector} + ); + } + + before(async function() { + // eslint-disable-next-line + this.timeout(90 * 1000); + + // read original manifest + originalManifest = fs.readFileSync(manifestLocation); + + const manifest = JSON.parse(originalManifest); + // add tabs permision to the manifest + manifest.permissions.push('tabs'); + // write new file to document + fs.writeFileSync(manifestLocation, JSON.stringify(manifest, null, 2)); + + // start puppeteer + browser = await puppeteer.launch({ + headless: false, + executablePath: process.env.CHROME_PATH, + args: [ + `--disable-extensions-except=${lighthouseExtensionPath}`, + `--load-extension=${lighthouseExtensionPath}`, + ], + }); + + const page = await browser.newPage(); + await page.goto('https://www.paulirish.com', {waitUntil: 'networkidle2'}); + const targets = await browser.targets(); + const extensionTarget = targets.find(({_targetInfo}) => { + return _targetInfo.title === 'Lighthouse' && _targetInfo.type === 'background_page'; + }); + + if (!extensionTarget) { + return await browser.close(); + } + + const client = await extensionTarget.createCDPSession(); + const lighthouseResult = await client.send('Runtime.evaluate', { + expression: `runLighthouseInExtension({ + restoreCleanState: true, + }, ${JSON.stringify(lighthouseCategories)})`, + awaitPromise: true, + returnByValue: true, + }); + + if (lighthouseResult.exceptionDetails) { + if (lighthouseResult.exceptionDetails.exception) { + throw new Error(lighthouseResult.exceptionDetails.exception.description); + } + + throw new Error(lighthouseResult.exceptionDetails.text); + } + + extensionPage = (await browser.pages()).find(page => + page.url().includes('blob:chrome-extension://') + ); + }); + + after(async () => { + // put the default manifest back + fs.writeFileSync(manifestLocation, originalManifest); + + if (browser) { + await browser.close(); + } + }); + + + const selectors = { + audits: '.lh-audit,.lh-timeline-metric,.lh-perf-hint', + titles: '.lh-score__title, .lh-perf-hint__title, .lh-timeline-metric__title', + }; + + it('should contain all categories', async () => { + const categories = await extensionPage.$$(`#${lighthouseCategories.join(',#')}`); + assert.equal( + categories.length, + lighthouseCategories.length, + `${categories.join(' ')} does not match ${lighthouseCategories.join(' ')}` + ); + }); + + it('should contain audits of all categories', async () => { + for (const category of lighthouseCategories) { + let expected = getAuditsOfCategory(category).length; + if (category === 'performance') { + expected = getAuditsOfCategory(category).filter(a => !!a.group).length; + } + + const elementCount = await getAuditElementsCount({category, selector: selectors.audits}); + + assert.equal( + expected, + elementCount, + `${category} does not have the correct amount of audits` + ); + } + }); + + it('should contain a filmstrip', async () => { + const filmstrip = await extensionPage.$('.lh-filmstrip'); + + assert.ok(!!filmstrip, `filmstrip is not available`); + }); + + it('should not have any audit errors', async () => { + function getDebugStrings(elems, selectors) { + return elems.map(el => { + const audit = el.closest(selectors.audits); + const auditTitle = audit && audit.querySelector(selectors.titles); + return { + debugString: el.textContent, + title: auditTitle ? auditTitle.textContent : 'Audit title unvailable', + }; + }); + } + + const auditErrors = await extensionPage.$$eval('.lh-debug', getDebugStrings, selectors); + const errors = auditErrors.filter( + item => + item.debugString.includes('Audit error:') && + // FIXME(phulce): fix timing failing on travis. + !item.debugString.includes('No timing information available') + ); + assert.deepStrictEqual(errors, [], 'Audit errors found within the report'); + }); +}); diff --git a/package.json b/package.json index 06dba9472323..e842faab77d7 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "debug": "node --inspect-brk ./lighthouse-cli/index.js", "start": "node ./lighthouse-cli/index.js", "test": "yarn lint --quiet && yarn unit && yarn type-check && yarn closure", + "test-extension": "cd lighthouse-extension && yarn test", "unit-core": "bash lighthouse-core/scripts/run-mocha.sh --core", "unit-cli": "bash lighthouse-core/scripts/run-mocha.sh --cli", "unit-viewer": "bash lighthouse-core/scripts/run-mocha.sh --viewer", @@ -78,6 +79,7 @@ "mocha": "^3.2.0", "npm-run-posix-or-windows": "^2.0.2", "postinstall-prepare": "^1.0.1", + "puppeteer": "^1.1.1", "sinon": "^2.3.5", "typescript": "^2.8.0-rc", "vscode-chrome-debug-core": "^3.23.8", diff --git a/yarn.lock b/yarn.lock index f8349689928b..f616c58ebfae 100644 --- a/yarn.lock +++ b/yarn.lock @@ -211,6 +211,12 @@ add-stream@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/add-stream/-/add-stream-1.0.0.tgz#6a7990437ca736d5e1288db92bd3266d5f5cb2aa" +agent-base@^4.1.0: + version "4.2.0" + resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.2.0.tgz#9838b5c3392b962bad031e6a4c5e1024abec45ce" + dependencies: + es6-promisify "^5.0.0" + ajv-keywords@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-2.1.0.tgz#a296e17f7bfae7c1ce4f7e0de53d29cb32162df0" @@ -887,7 +893,7 @@ concat-map@0.0.1: version "0.0.1" resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" -concat-stream@^1.4.7, concat-stream@^1.6.0: +concat-stream@1.6.0, concat-stream@^1.4.7, concat-stream@^1.6.0: version "1.6.0" resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-1.6.0.tgz#0aac662fd52be78964d5532f694784e70110acf7" dependencies: @@ -1182,13 +1188,19 @@ debug@2.2.0: dependencies: ms "0.7.1" +debug@2.6.9: + version "2.6.9" + resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" + dependencies: + ms "2.0.0" + debug@^2.1.1, debug@^2.2.0, debug@^2.4.5, debug@^2.6.8: version "2.6.8" resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.8.tgz#e731531ca2ede27d188222427da17821d68ff4fc" dependencies: ms "2.0.0" -debug@^3.0.1: +debug@^3.0.1, debug@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261" dependencies: @@ -1326,6 +1338,16 @@ error-ex@^1.2.0: dependencies: is-arrayish "^0.2.1" +es6-promise@^4.0.3: + version "4.2.4" + resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.2.4.tgz#dc4221c2b16518760bd8c39a52d8f356fc00ed29" + +es6-promisify@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/es6-promisify/-/es6-promisify-5.0.0.tgz#5109d62f3e56ea967c4b63505aef08291c8a5203" + dependencies: + es6-promise "^4.0.3" + escape-string-regexp@1.0.5, escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.3, escape-string-regexp@^1.0.5: version "1.0.5" resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" @@ -1509,6 +1531,15 @@ extglob@^0.3.1: dependencies: is-extglob "^1.0.0" +extract-zip@^1.6.5: + version "1.6.6" + resolved "https://registry.yarnpkg.com/extract-zip/-/extract-zip-1.6.6.tgz#1290ede8d20d0872b429fd3f351ca128ec5ef85c" + dependencies: + concat-stream "1.6.0" + debug "2.6.9" + mkdirp "0.5.0" + yauzl "2.4.1" + extsprintf@1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.0.2.tgz#e1080e0658e300b06294990cc70e1502235fd550" @@ -1532,6 +1563,12 @@ fast-levenshtein@~2.0.4: version "2.0.5" resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.5.tgz#bd33145744519ab1c36c3ee9f31f08e9079b67f2" +fd-slicer@~1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/fd-slicer/-/fd-slicer-1.0.1.tgz#8b5bcbd9ec327c5041bf9ab023fd6750f1177e65" + dependencies: + pend "~1.2.0" + figures@^1.3.5: version "1.7.0" resolved "https://registry.yarnpkg.com/figures/-/figures-1.7.0.tgz#cbe1e3affcf1cd44b80cadfed28dc793a9701d2e" @@ -2147,6 +2184,13 @@ http-signature@~1.1.0: jsprim "^1.2.2" sshpk "^1.7.0" +https-proxy-agent@^2.1.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-2.2.0.tgz#7fbba856be8cd677986f42ebd3664f6317257887" + dependencies: + agent-base "^4.1.0" + debug "^3.1.0" + iconv-lite@0.4.13: version "0.4.13" resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.13.tgz#1f88aba4ab0b1508e8312acc39345f36e992e2f2" @@ -2974,6 +3018,10 @@ mime-types@^2.1.11, mime-types@^2.1.12, mime-types@~2.1.7: dependencies: mime-db "~1.24.0" +mime@^1.3.4: + version "1.6.0" + resolved "https://registry.yarnpkg.com/mime/-/mime-1.6.0.tgz#32cd9e5c64553bd58d19a568af452acff04981b1" + mimic-fn@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-1.1.0.tgz#e667783d92e89dbd342818b5230b9d62a672ad18" @@ -3011,6 +3059,12 @@ minimist@1.2.0, minimist@^1.1.0, minimist@^1.1.3, minimist@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284" +mkdirp@0.5.0: + version "0.5.0" + resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.0.tgz#1d73076a6df986cd9344e15e71fcc05a4c9abf12" + dependencies: + minimist "0.0.8" + mkdirp@0.5.1, mkdirp@0.5.x, mkdirp@^0.5.0, mkdirp@^0.5.1: version "0.5.1" resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903" @@ -3371,6 +3425,10 @@ path-type@^2.0.0: dependencies: pify "^2.0.0" +pend@~1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/pend/-/pend-1.2.0.tgz#7a57eb550a6783f9115331fcf4663d5c8e007a50" + pify@^2.0.0, pify@^2.3.0: version "2.3.0" resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c" @@ -3431,6 +3489,10 @@ progress@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.0.tgz#8a1be366bf8fc23db2bd23f10c6fe920b4389d1f" +proxy-from-env@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.0.0.tgz#33c50398f70ea7eb96d21f7b817630a55791c7ee" + pseudomap@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3" @@ -3439,6 +3501,19 @@ punycode@^1.4.1: version "1.4.1" resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e" +puppeteer@^1.1.1: + version "1.1.1" + resolved "https://registry.yarnpkg.com/puppeteer/-/puppeteer-1.1.1.tgz#adbf25e49f5ef03443c10ab8e09a954ca0c7bfee" + dependencies: + debug "^2.6.8" + extract-zip "^1.6.5" + https-proxy-agent "^2.1.0" + mime "^1.3.4" + progress "^2.0.0" + proxy-from-env "^1.0.0" + rimraf "^2.6.1" + ws "^3.0.0" + q@^1.4.1: version "1.5.1" resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7" @@ -4549,7 +4624,7 @@ ws@3.3.2: safe-buffer "~5.1.0" ultron "~1.1.0" -ws@^3.3.2: +ws@^3.0.0, ws@^3.3.2: version "3.3.3" resolved "https://registry.yarnpkg.com/ws/-/ws-3.3.3.tgz#f1cf84fe2d5e901ebce94efaece785f187a228f2" dependencies: @@ -4604,6 +4679,12 @@ yargs@~3.10.0: decamelize "^1.0.0" window-size "0.1.0" +yauzl@2.4.1: + version "2.4.1" + resolved "https://registry.yarnpkg.com/yauzl/-/yauzl-2.4.1.tgz#9528f442dab1b2284e58b4379bb194e22e0c4005" + dependencies: + fd-slicer "~1.0.1" + zone.js@^0.7.3: version "0.7.3" resolved "https://registry.yarnpkg.com/zone.js/-/zone.js-0.7.3.tgz#d91432b6584f73c2c9e03ce4bb0870becc45d445"