Repository: GoogleChrome/lighthouse
Pull request #6089 (2018-09-21 23:05:41)

core(driver): add check to make sure Runtime.evaluate result exists
<!-- Thank you for submitting a pull request! -->

**Summary**
Added an undefined check for driver results to catch an undefined result coming back from the Protocol, which shouldn't happen, but we check anyway to catch if it does.

[Relevant Protocol](https://chromedevtools.github.io/devtools-protocol/tot/Runtime#method-evaluate)

**Related Issues/PRs**
addresses: #6026 



Patch:
diff --git a/lighthouse-core/audits/dobetterweb/js-libraries.js b/lighthouse-core/audits/dobetterweb/js-libraries.js
new file mode 100644
index 000000000..3a970ff7f
--- /dev/null
+++ b/lighthouse-core/audits/dobetterweb/js-libraries.js
@@ -0,0 +1,52 @@
+/**
+ * @license Copyright 2017 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.
+ */
+
+/**
+ * @fileoverview Diagnostic audit that lists all JavaScript libraries detected on the page
+ */
+
+'use strict';
+
+const Audit = require('../audit');
+
+class JsLibrariesAudit extends Audit {
+  /**
+   * @return {LH.Audit.Meta}
+   */
+  static get meta() {
+    return {
+      id: 'js-libraries',
+      title: 'Detected JavaScript libraries',
+      description: 'All front-end JavaScript libraries detected on the page.',
+      requiredArtifacts: ['JSLibraries'],
+    };
+  }
+
+  /**
+   * @param {LH.Artifacts} artifacts
+   * @return {LH.Audit.Product}
+   */
+  static audit(artifacts) {
+    const libDetails = artifacts.JSLibraries.map(lib => ({
+      name: lib.name,
+      version: lib.version, // null if not detected
+      npm: lib.npmPkgName || null, // ~70% of libs come with this field
+    }));
+
+    const headings = [
+      {key: 'name', itemType: 'text', text: 'Name'},
+      {key: 'version', itemType: 'text', text: 'Version'},
+    ];
+    const details = Audit.makeTableDetails(headings, libDetails, {});
+
+    return {
+      rawValue: true, // Always pass for now.
+      details,
+    };
+  }
+}
+
+module.exports = JsLibrariesAudit;
diff --git a/lighthouse-core/audits/seo/font-size.js b/lighthouse-core/audits/seo/font-size.js
index 46b78ba52..9a08eea1e 100644
--- a/lighthouse-core/audits/seo/font-size.js
+++ b/lighthouse-core/audits/seo/font-size.js
@@ -6,13 +6,10 @@
 'use strict';
 
 /** @typedef {LH.Artifacts.FontSize['analyzedFailingNodesData'][0]} FailingNodeData */
-/** @typedef {{Type: {Regular: 'Regular', Inline: 'Inline', Attributes: 'Attributes'}}} WebInspectorCSSStyle */
 
 const URL = require('../../lib/url-shim');
 const Audit = require('../audit');
 const ViewportAudit = require('../viewport');
-const WebInspector = require('../../lib/web-inspector');
-const CSSStyleDeclaration = /** @type {WebInspectorCSSStyle} */ (WebInspector.CSSStyleDeclaration);
 const MINIMAL_PERCENTAGE_OF_LEGIBLE_TEXT = 60;
 
 /**
@@ -107,8 +104,8 @@ function nodeToTableNode(node) {
 function findStyleRuleSource(baseURL, styleDeclaration, node) {
   if (
     !styleDeclaration ||
-    styleDeclaration.type === CSSStyleDeclaration.Type.Attributes ||
-    styleDeclaration.type === CSSStyleDeclaration.Type.Inline
+    styleDeclaration.type === 'Attributes' ||
+    styleDeclaration.type === 'Inline'
   ) {
     return {
       selector: nodeToTableNode(node),
@@ -124,7 +121,7 @@ function findStyleRuleSource(baseURL, styleDeclaration, node) {
     };
   }
 
-  if (styleDeclaration.type === CSSStyleDeclaration.Type.Regular && styleDeclaration.parentRule) {
+  if (styleDeclaration.type === 'Regular' && styleDeclaration.parentRule) {
     const rule = styleDeclaration.parentRule;
     const stylesheet = styleDeclaration.stylesheet;
 
@@ -169,7 +166,7 @@ function findStyleRuleSource(baseURL, styleDeclaration, node) {
  * @return {string}
  */
 function getFontArtifactId(styleDeclaration, node) {
-  if (styleDeclaration && styleDeclaration.type === CSSStyleDeclaration.Type.Regular) {
+  if (styleDeclaration && styleDeclaration.type === 'Regular') {
     const startLine = styleDeclaration.range ? styleDeclaration.range.startLine : 0;
     const startColumn = styleDeclaration.range ? styleDeclaration.range.startColumn : 0;
     return `${styleDeclaration.styleSheetId}@${startLine}:${startColumn}`;
diff --git a/lighthouse-core/audits/themed-omnibox.js b/lighthouse-core/audits/themed-omnibox.js
index 50534ecac..b3760b7ca 100644
--- a/lighthouse-core/audits/themed-omnibox.js
+++ b/lighthouse-core/audits/themed-omnibox.js
@@ -6,8 +6,8 @@
 'use strict';
 
 const MultiCheckAudit = require('./multi-check-audit');
-const validColor = require('../lib/web-inspector').Color.parse;
 const ManifestValues = require('../gather/computed/manifest-values');
+const cssParsers = require('cssstyle/lib/parsers');
 
 /**
  * @fileoverview
@@ -34,6 +34,14 @@ class ThemedOmnibox extends MultiCheckAudit {
     };
   }
 
+  /**
+   * @param {string} color
+   * @return {boolean}
+   */
+  static isValidColor(color) {
+    return cssParsers.valueType(color) === cssParsers.TYPES.COLOR;
+  }
+
   /**
    * @param {LH.Artifacts['ThemeColor']} themeColorMeta
    * @param {Array<string>} failures
@@ -41,7 +49,7 @@ class ThemedOmnibox extends MultiCheckAudit {
   static assessMetaThemecolor(themeColorMeta, failures) {
     if (themeColorMeta === null) {
       failures.push('No `<meta name="theme-color">` tag found');
-    } else if (!validColor(themeColorMeta)) {
+    } else if (!ThemedOmnibox.isValidColor(themeColorMeta)) {
       failures.push('The theme-color meta tag did not contain a valid CSS color');
     }
   }
diff --git a/lighthouse-core/config/default-config.js b/lighthouse-core/config/default-config.js
index dd6088a9c..2087e6f5f 100644
--- a/lighthouse-core/config/default-config.js
+++ b/lighthouse-core/config/default-config.js
@@ -197,6 +197,7 @@ const defaultConfig = {
     'dobetterweb/geolocation-on-start',
     'dobetterweb/no-document-write',
     'dobetterweb/no-vulnerable-libraries',
+    'dobetterweb/js-libraries',
     'dobetterweb/no-websql',
     'dobetterweb/notification-on-start',
     'dobetterweb/password-inputs-can-be-pasted-into',
@@ -408,6 +409,7 @@ const defaultConfig = {
         {id: 'geolocation-on-start', weight: 1},
         {id: 'doctype', weight: 1},
         {id: 'no-vulnerable-libraries', weight: 1},
+        {id: 'js-libraries', weight: 0},
         {id: 'notification-on-start', weight: 1},
         {id: 'deprecations', weight: 1},
         {id: 'password-inputs-can-be-pasted-into', weight: 1},
diff --git a/lighthouse-core/gather/driver.js b/lighthouse-core/gather/driver.js
index 7ed571c60..e55b2cfaa 100644
--- a/lighthouse-core/gather/driver.js
+++ b/lighthouse-core/gather/driver.js
@@ -311,15 +311,23 @@ class Driver {
         contextId,
       };
 
-      this.sendCommand('Runtime.evaluate', evaluationParams).then(result => {
+      this.sendCommand('Runtime.evaluate', evaluationParams).then(response => {
         clearTimeout(asyncTimeout);
-        const value = result.result.value;
 
-        if (result.exceptionDetails) {
+        if (response.exceptionDetails) {
           // An error occurred before we could even create a Promise, should be *very* rare
-          reject(new Error('an unexpected driver error occurred'));
-        } if (value && value.__failedInBrowser) {
-          reject(Object.assign(new Error(), value));
+          return reject(new Error(`Evaluation exception: ${response.exceptionDetails.text}`));
+        }
+
+        // Protocol should always return a 'result' object, but it is sometimes undefined.  See #6026.
+        if (response.result === undefined) {
+          return reject(new Error('Runtime.evaluate response did not contain a "result" object'));
+        }
+
+        const value = response.result.value;
+
+        if (value && value.__failedInBrowser) {
+          return reject(Object.assign(new Error(), value));
         } else {
           resolve(value);
         }
diff --git a/lighthouse-core/gather/gather-runner.js b/lighthouse-core/gather/gather-runner.js
index 07b988094..918aa0e13 100644
--- a/lighthouse-core/gather/gather-runner.js
+++ b/lighthouse-core/gather/gather-runner.js
@@ -156,22 +156,19 @@ class GatherRunner {
       return URL.equalWithExcludedFragments(record.url, url);
     });
 
-    let errorCode;
-    let errorReason;
+    let errorDef;
     if (!mainRecord) {
-      errorCode = LHError.errors.NO_DOCUMENT_REQUEST;
+      errorDef = LHError.errors.NO_DOCUMENT_REQUEST;
     } else if (mainRecord.failed) {
-      errorCode = LHError.errors.FAILED_DOCUMENT_REQUEST;
-      errorReason = mainRecord.localizedFailDescription;
+      errorDef = {...LHError.errors.FAILED_DOCUMENT_REQUEST};
+      errorDef.message += ` ${mainRecord.localizedFailDescription}.`;
     } else if (mainRecord.hasErrorStatusCode()) {
-      errorCode = LHError.errors.ERRORED_DOCUMENT_REQUEST;
-      errorReason = `Status code: ${mainRecord.statusCode}`;
+      errorDef = {...LHError.errors.ERRORED_DOCUMENT_REQUEST};
+      errorDef.message += ` Status code: ${mainRecord.statusCode}.`;
     }
 
-    if (errorCode) {
-      const error = new LHError(errorCode, {reason: errorReason});
-      log.error('GatherRunner', error.message, url);
-      return error;
+    if (errorDef) {
+      return new LHError(errorDef);
     }
   }
 
@@ -279,9 +276,8 @@ class GatherRunner {
     if (!driver.online) pageLoadError = undefined;
 
     if (pageLoadError) {
-      passContext.LighthouseRunWarnings.push('Lighthouse was unable to reliably load the ' +
-        'page you requested. Make sure you are testing the correct URL and that the server is ' +
-        'properly responding to all requests.');
+      log.error('GatherRunner', pageLoadError.message, passContext.url);
+      passContext.LighthouseRunWarnings.push(pageLoadError.friendlyMessage);
     }
 
     // Expose devtoolsLog, networkRecords, and trace (if present) to gatherers
@@ -333,7 +329,6 @@ class GatherRunner {
     /** @type {Partial<LH.GathererArtifacts>} */
     const gathererArtifacts = {};
 
-    const pageLoadFailures = [];
     const resultsEntries = /** @type {GathererResultsEntries} */ (Object.entries(gathererResults));
     for (const [gathererName, phaseResultsPromises] of resultsEntries) {
       if (gathererArtifacts[gathererName] !== undefined) continue;
@@ -349,18 +344,11 @@ class GatherRunner {
         // An error result must be non-fatal to not have caused an exit by now,
         // so return it to runner to handle turning it into an error audit.
         gathererArtifacts[gathererName] = err;
-        // Track page load errors separately, so we can fail loudly if needed.
-        if (LHError.isPageLoadError(err)) pageLoadFailures.push(err);
       }
 
       if (gathererArtifacts[gathererName] === undefined) {
         throw new Error(`${gathererName} failed to provide an artifact.`);
       }
-
-      // Fail the run if more than 50% of all artifacts failed due to page load failure.
-      if (pageLoadFailures.length > Object.keys(gathererArtifacts).length * 0.5) {
-        throw LHError.fromLighthouseError(pageLoadFailures[0]);
-      }
     }
 
     // Take only unique LighthouseRunWarnings.
diff --git a/lighthouse-core/gather/gatherers/dobetterweb/js-libraries.js b/lighthouse-core/gather/gatherers/dobetterweb/js-libraries.js
index e2b940f38..2d6a0c7b3 100644
--- a/lighthouse-core/gather/gatherers/dobetterweb/js-libraries.js
+++ b/lighthouse-core/gather/gatherers/dobetterweb/js-libraries.js
@@ -1,5 +1,5 @@
 /**
- * @license Copyright 2017 Google Inc. All Rights Reserved.
+ * @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.
  */
diff --git a/lighthouse-core/lib/asset-saver.js b/lighthouse-core/lib/asset-saver.js
index d3ae67d07..ceda847fd 100644
--- a/lighthouse-core/lib/asset-saver.js
+++ b/lighthouse-core/lib/asset-saver.js
@@ -12,7 +12,6 @@ const stream = require('stream');
 const Simulator = require('./dependency-graph/simulator/simulator');
 const lanternTraceSaver = require('./lantern-trace-saver');
 const Metrics = require('./traces/pwmetrics-events');
-const TraceParser = require('./traces/trace-parser');
 const rimraf = require('rimraf');
 const mkdirp = require('mkdirp');
 
@@ -94,7 +93,7 @@ async function loadArtifacts(basePath) {
 
   // load devtoolsLogs
   artifacts.devtoolsLogs = {};
-  filenames.filter(f => f.endsWith(devtoolsLogSuffix)).map(filename => {
+  filenames.filter(f => f.endsWith(devtoolsLogSuffix)).forEach(filename => {
     const passName = filename.replace(devtoolsLogSuffix, '');
     const devtoolsLog = JSON.parse(fs.readFileSync(path.join(basePath, filename), 'utf8'));
     artifacts.devtoolsLogs[passName] = devtoolsLog;
@@ -102,22 +101,12 @@ async function loadArtifacts(basePath) {
 
   // load traces
   artifacts.traces = {};
-  const promises = filenames.filter(f => f.endsWith(traceSuffix)).map(filename => {
-    return new Promise(resolve => {
-      const passName = filename.replace(traceSuffix, '');
-      const readStream = fs.createReadStream(path.join(basePath, filename), {
-        encoding: 'utf-8',
-        highWaterMark: 4 * 1024 * 1024, // TODO benchmark to find the best buffer size here
-      });
-      const parser = new TraceParser();
-      readStream.on('data', chunk => parser.parseChunk(chunk));
-      readStream.on('end', _ => {
-        artifacts.traces[passName] = parser.getTrace();
-        resolve();
-      });
-    });
+  filenames.filter(f => f.endsWith(traceSuffix)).forEach(filename => {
+    const file = fs.readFileSync(path.join(basePath, filename), {encoding: 'utf-8'});
+    const trace = JSON.parse(file);
+    const passName = filename.replace(traceSuffix, '');
+    artifacts.traces[passName] = Array.isArray(trace) ? {traceEvents: trace} : trace;
   });
-  await Promise.all(promises);
 
   return artifacts;
 }
diff --git a/lighthouse-core/lib/lh-error.js b/lighthouse-core/lib/lh-error.js
index 4adeee996..557fcc746 100644
--- a/lighthouse-core/lib/lh-error.js
+++ b/lighthouse-core/lib/lh-error.js
@@ -31,15 +31,6 @@ class LighthouseError extends Error {
     Error.captureStackTrace(this, LighthouseError);
   }
 
-  /**
-   * @param {{code?: string}} err
-   * @return {err is LighthouseError}
-   */
-  static isPageLoadError(err) {
-    return err.code === ERRORS.NO_DOCUMENT_REQUEST.code ||
-      err.code === ERRORS.FAILED_DOCUMENT_REQUEST.code;
-  }
-
   /**
    * @param {LighthouseError} err
    * @return {LighthouseError}
diff --git a/lighthouse-core/lib/manifest-parser.js b/lighthouse-core/lib/manifest-parser.js
index 17b208af8..7468583b8 100644
--- a/lighthouse-core/lib/manifest-parser.js
+++ b/lighthouse-core/lib/manifest-parser.js
@@ -6,7 +6,7 @@
 'use strict';
 
 const URL = require('./url-shim');
-const validateColor = require('./web-inspector').Color.parse;
+const cssParsers = require('cssstyle/lib/parsers');
 
 const ALLOWED_DISPLAY_VALUES = [
   'fullscreen',
@@ -31,6 +31,14 @@ const ALLOWED_ORIENTATION_VALUES = [
   'landscape-secondary',
 ];
 
+/**
+ * @param {string} color
+ * @return {boolean}
+ */
+function isValidColor(color) {
+  return cssParsers.valueType(color) === cssParsers.TYPES.COLOR;
+}
+
 /**
  * @param {*} raw
  * @param {boolean=} trim
@@ -66,9 +74,8 @@ function parseColor(raw) {
     return color;
   }
 
-  // Use DevTools's color parser to check CSS3 Color parsing.
-  const validatedColor = validateColor(color.raw);
-  if (!validatedColor) {
+  // Use color parser to check CSS3 Color parsing.
+  if (!isValidColor(color.raw)) {
     color.value = undefined;
     color.warning = 'ERROR: color parsing failed.';
   }
diff --git a/lighthouse-core/lib/network-recorder.js b/lighthouse-core/lib/network-recorder.js
index 4098fc371..d7511d833 100644
--- a/lighthouse-core/lib/network-recorder.js
+++ b/lighthouse-core/lib/network-recorder.js
@@ -5,7 +5,6 @@
  */
 'use strict';
 
-const NetworkManager = require('./web-inspector').NetworkManager;
 const NetworkRequest = require('./network-request');
 const EventEmitter = require('events').EventEmitter;
 const log = require('lighthouse-logger');
@@ -47,10 +46,6 @@ class NetworkRecorder extends EventEmitter {
     return super.once(event, listener);
   }
 
-  get EventTypes() {
-    return NetworkManager.Events;
-  }
-
   isIdle() {
     return !!this._getActiveIdlePeriod(0);
   }
diff --git a/lighthouse-core/lib/strings.js b/lighthouse-core/lib/strings.js
index d6f56f5a1..24bf4d91c 100644
--- a/lighthouse-core/lib/strings.js
+++ b/lighthouse-core/lib/strings.js
@@ -10,7 +10,7 @@ module.exports = {
   didntCollectScreenshots: `Chrome didn't collect any screenshots during the page load. Please make sure there is content visible on the page, and then try re-running Lighthouse.`,
   badTraceRecording: `Something went wrong with recording the trace over your page load. Please run Lighthouse again.`,
   pageLoadTookTooLong: `Your page took too long to load. Please follow the opportunities in the report to reduce your page load time, and then try re-running Lighthouse.`,
-  pageLoadFailed: `Your page failed to load. Verify that the URL is valid and re-run Lighthouse.`,
+  pageLoadFailed: `Lighthouse was unable to reliably load the page you requested. Make sure you are testing the correct URL and that the server is properly responding to all requests.`,
   internalChromeError: `An internal Chrome error occurred. Please restart Chrome and try re-running Lighthouse.`,
   requestContentTimeout: 'Fetching resource content has exceeded the allotted time',
   urlInvalid: `The URL you have provided appears to be invalid.`,
diff --git a/lighthouse-core/lib/traces/trace-parser.js b/lighthouse-core/lib/traces/trace-parser.js
deleted file mode 100644
index d5a3f0dfd..000000000
--- a/lighthouse-core/lib/traces/trace-parser.js
+++ /dev/null
@@ -1,74 +0,0 @@
-/**
- * @license Copyright 2017 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';
-
-const WebInspector = require('../web-inspector');
-
-/**
- * Traces > 256MB hit limits in V8, so TraceParser will parse the trace events stream as it's
- * received. We use DevTools' TimelineLoader for the heavy lifting, as it has a fast trace-specific
- * streaming JSON parser.
- * The resulting trace doesn't include the "metadata" property, as it's excluded via DevTools'
- * implementation.
- * FIXME: This can be removed once Node 8 support is dropped. https://stackoverflow.com/a/47781288/89484
- */
-class TraceParser {
-  constructor() {
-    /** @type {Array<LH.TraceEvent>} */
-    this.traceEvents = [];
-
-    this.tracingModel = {
-      reset: () => this._reset(),
-      /** @param {Array<LH.TraceEvent>} evts */
-      addEvents: evts => this._addEvents(evts),
-    };
-
-    const delegateMock = {
-      loadingProgress: () => {},
-      loadingStarted: () => {},
-      /** @param {boolean} success */
-      loadingComplete: success => {
-        if (!success) throw new Error('Parsing problem');
-      },
-    };
-    this.loader = new WebInspector.TimelineLoader(this.tracingModel, delegateMock);
-  }
-
-  /**
-   * Reset the trace events array
-   */
-  _reset() {
-    this.traceEvents = [];
-  }
-
-  /**
-   * Adds parsed trace events to array
-   * @param {Array<LH.TraceEvent>} evts
-   */
-  _addEvents(evts) {
-    this.traceEvents.push(...evts);
-  }
-
-  /**
-   * Receive chunk of streamed trace
-   * @param {string} data
-   */
-  parseChunk(data) {
-    this.loader.write(data);
-  }
-
-  /**
-   * Returns entire trace
-   * @return {{traceEvents: Array<LH.TraceEvent>}}
-   */
-  getTrace() {
-    return {
-      traceEvents: this.traceEvents,
-    };
-  }
-}
-
-module.exports = TraceParser;
diff --git a/lighthouse-core/lib/web-inspector.js b/lighthouse-core/lib/web-inspector.js
index c181fef2c..b9817cf58 100644
--- a/lighthouse-core/lib/web-inspector.js
+++ b/lighthouse-core/lib/web-inspector.js
@@ -21,10 +21,6 @@ module.exports = (function() {
     global.self = global;
   }
 
-  if (typeof global.window === 'undefined') {
-    global.window = global;
-  }
-
   global.Node = {
     ELEMENT_NODE: 1,
     TEXT_NODE: 3,
@@ -32,10 +28,10 @@ module.exports = (function() {
 
   global.CSSAgent = {};
   global.CSSAgent.StyleSheetOrigin = {
-    INJECTED: 'injected',
-    USER_AGENT: 'user-agent',
-    INSPECTOR: 'inspector',
-    REGULAR: 'regular',
+    Injected: 'injected',
+    UserAgent: 'user-agent',
+    Inspector: 'inspector',
+    Regular: 'regular',
   };
 
   global.CSS = {};
@@ -45,112 +41,11 @@ module.exports = (function() {
   // See https://github.com/GoogleChrome/lighthouse/issues/73
   const _setImmediate = global.setImmediate;
 
-  global.Runtime = global.Runtime || {};
-  global.Runtime.experiments = global.Runtime.experiments || {};
-  // DevTools runtime doesn't know about some experiments that DTM looks for
-  // To avoid exceptions, we assume all experiments are disabled
-  global.Runtime.experiments.isEnabled = (_ => false);
-
-  const _queryParam = global.Runtime.queryParam;
-  global.Runtime.queryParam = function(arg) {
-    switch (arg) {
-      case 'remoteFrontend':
-        return false;
-      case 'ws':
-        return false;
-      default: {
-        if (_queryParam) {
-          return _queryParam.call(global.Runtime, arg);
-        }
-        throw new Error('Mock queryParam case not implemented.');
-      }
-    }
-  };
-
-  global.TreeElement = {};
-  global.WorkerRuntime = {};
-
-  global.Protocol = {
-    Agents() {},
-  };
-
   global.WebInspector = {};
   const WebInspector = global.WebInspector;
-  WebInspector._moduleSettings = {
-    cacheDisabled: {
-      addChangeListener() {},
-      get() {
-        return false;
-      },
-    },
-    monitoringXHREnabled: {
-      addChangeListener() {},
-      get() {
-        return false;
-      },
-    },
-    showNativeFunctionsInJSProfile: {
-      addChangeListener() {},
-      get() {
-        return true;
-      },
-    },
-  };
-  WebInspector.moduleSetting = function(settingName) {
-    return this._moduleSettings[settingName];
-  };
 
   // Shared Dependencies
-  require('chrome-devtools-frontend/front_end/common/Object.js');
-  require('chrome-devtools-frontend/front_end/common/ParsedURL.js');
-  require('chrome-devtools-frontend/front_end/common/UIString.js');
-  require('chrome-devtools-frontend/front_end/common/SegmentedRange.js');
   require('chrome-devtools-frontend/front_end/platform/utilities.js');
-  require('chrome-devtools-frontend/front_end/sdk/Target.js');
-  require('chrome-devtools-frontend/front_end/sdk/TargetManager.js');
-
-  // Dependencies for timeline-model
-  WebInspector.targetManager = {
-    observeTargets() { },
-    addEventListener() { },
-  };
-  WebInspector.settings = {
-    createSetting() {
-      return {
-        get() {
-          return false;
-        },
-        addChangeListener() {},
-      };
-    },
-  };
-  WebInspector.console = {
-    error() {},
-  };
-  WebInspector.VBox = function() {};
-  WebInspector.HBox = function() {};
-  WebInspector.ViewportDataGrid = function() {};
-  WebInspector.ViewportDataGridNode = function() {};
-  global.WorkerRuntime.Worker = function() {};
-
-  // used for streaming json parsing
-  require('chrome-devtools-frontend/front_end/common/TextUtils.js');
-  require('chrome-devtools-frontend/front_end/timeline/TimelineLoader.js');
-
-  // Mock for WebInspector code that writes to console.
-  WebInspector.ConsoleMessage = function() {};
-  WebInspector.ConsoleMessage.MessageSource = {
-    Network: 'network',
-  };
-  WebInspector.ConsoleMessage.MessageLevel = {
-    Log: 'log',
-  };
-  WebInspector.ConsoleMessage.MessageType = {
-    Log: 'log',
-  };
-
-  // Dependencies for color parsing.
-  require('chrome-devtools-frontend/front_end/common/Color.js');
 
   // Dependencies for effective CSS rule calculation.
   require('chrome-devtools-frontend/front_end/common/TextRange.js');
diff --git a/lighthouse-core/runner.js b/lighthouse-core/runner.js
index ff134fc02..096ed4d39 100644
--- a/lighthouse-core/runner.js
+++ b/lighthouse-core/runner.js
@@ -311,9 +311,7 @@ class Runner {
   static getArtifactRuntimeError(artifacts) {
     for (const possibleErrorArtifact of Object.values(artifacts)) {
       if (possibleErrorArtifact instanceof LHError && possibleErrorArtifact.lhrRuntimeError) {
-        const errorMessage = possibleErrorArtifact.friendlyMessage ?
-            `${possibleErrorArtifact.friendlyMessage} (${possibleErrorArtifact.message})` :
-            possibleErrorArtifact.message;
+        const errorMessage = possibleErrorArtifact.friendlyMessage || possibleErrorArtifact.message;
 
         return {
           code: possibleErrorArtifact.code,
diff --git a/lighthouse-extension/app/src/lighthouse-ext-background.js b/lighthouse-extension/app/src/lighthouse-ext-background.js
index fcb92a09a..9041b9ae1 100644
--- a/lighthouse-extension/app/src/lighthouse-ext-background.js
+++ b/lighthouse-extension/app/src/lighthouse-ext-background.js
@@ -231,8 +231,8 @@ function isRunning() {
   return lighthouseIsRunning;
 }
 
-// Run when in extension context, but not in devtools.
-if ('chrome' in window && chrome.runtime) {
+// Run when in extension context, but not in devtools or unit tests.
+if (typeof window !== 'undefined' && 'chrome' in window && chrome.runtime) {
   chrome.runtime.onInstalled.addListener(details => {
     if (details.previousVersion) {
       // eslint-disable-next-line no-console
diff --git a/package.json b/package.json
index f98a3bdec..76574c69c 100644
--- a/package.json
+++ b/package.json
@@ -8,7 +8,7 @@
     "chrome-debug": "./lighthouse-core/scripts/manual-chrome-launcher.js"
   },
   "engines": {
-    "node": ">=8.9"
+    "node": ">=8.10"
   },
   "scripts": {
     "install-all": "npm-run-posix-or-windows install-all:task",
@@ -115,6 +115,7 @@
     "chrome-devtools-frontend": "1.0.422034",
     "chrome-launcher": "^0.10.4",
     "configstore": "^3.1.1",
+    "cssstyle": "1.1.1",
     "devtools-timeline-model": "1.1.6",
     "esprima": "^4.0.1",
     "http-link-header": "^0.8.0",
diff --git a/typings/audit.d.ts b/typings/audit.d.ts
index cbde9faa7..aa3191f66 100644
--- a/typings/audit.d.ts
+++ b/typings/audit.d.ts
@@ -95,7 +95,7 @@ declare global {
 
     export type DetailsItem = string | number | DetailsRendererNodeDetailsJSON |
       DetailsRendererLinkDetailsJSON | DetailsRendererCodeDetailJSON | undefined |
-      boolean | DetailsRendererUrlDetailsJSON;
+      boolean | DetailsRendererUrlDetailsJSON | null;
 
     export interface DetailsRendererNodeDetailsJSON {
       type: 'node';
diff --git a/typings/cssstyle/index.d.ts b/typings/cssstyle/index.d.ts
new file mode 100644
index 000000000..06b9a631c
--- /dev/null
+++ b/typings/cssstyle/index.d.ts
@@ -0,0 +1,23 @@
+/**
+ * @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.
+ */
+
+declare module 'cssstyle/lib/parsers' {
+  interface TYPES {
+    INTEGER: 1;
+    NUMBER: 2;
+    LENGTH: 3;
+    PERCENT: 4;
+    URL: 5;
+    COLOR: 6;
+    STRING: 7;
+    ANGLE: 8;
+    KEYWORD: 9;
+    NULL_OR_EMPTY_STR: 10;
+  }
+
+  export var TYPES: TYPES;
+  export function valueType(val: any): TYPES[keyof TYPES] | undefined;
+}
diff --git a/yarn.lock b/yarn.lock
index 05e7ae2b3..836946eac 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1482,6 +1482,12 @@ cssom@0.3.x, "cssom@>= 0.3.2 < 0.4.0":
   version "0.3.2"
   resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.3.2.tgz#b8036170c79f07a90ff2f16e22284027a243848b"
 
+cssstyle@1.1.1:
+  version "1.1.1"
+  resolved "https://registry.yarnpkg.com/cssstyle/-/cssstyle-1.1.1.tgz#18b038a9c44d65f7a8e428a653b9f6fe42faf5fb"
+  dependencies:
+    cssom "0.3.x"
+
 "cssstyle@>= 0.2.37 < 0.3.0":
   version "0.2.37"
   resolved "https://registry.yarnpkg.com/cssstyle/-/cssstyle-0.2.37.tgz#541097234cb2513c83ceed3acddc27ff27987d54"


Test patch:
diff --git a/lighthouse-core/test/audits/dobetterweb/js-libraries-test.js b/lighthouse-core/test/audits/dobetterweb/js-libraries-test.js
new file mode 100644
index 000000000..65bb77641
--- /dev/null
+++ b/lighthouse-core/test/audits/dobetterweb/js-libraries-test.js
@@ -0,0 +1,64 @@
+/**
+ * @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';
+
+const JsLibrariesAudit = require('../../../audits/dobetterweb/js-libraries.js');
+const assert = require('assert');
+
+/* eslint-env jest */
+describe('Returns detected front-end JavaScript libraries', () => {
+  it('always passes', () => {
+    // no libraries
+    const auditResult1 = JsLibrariesAudit.audit({
+      JSLibraries: [],
+    });
+    assert.equal(auditResult1.rawValue, true);
+
+    // duplicates. TODO: consider failing in this case
+    const auditResult2 = JsLibrariesAudit.audit({
+      JSLibraries: [
+        {name: 'lib1', version: '3.10.1', npmPkgName: 'lib1'},
+        {name: 'lib2', version: null, npmPkgName: 'lib2'},
+      ],
+    });
+    assert.equal(auditResult2.rawValue, true);
+
+    // LOTS of frontend libs
+    const auditResult3 = JsLibrariesAudit.audit({
+      JSLibraries: [
+        {name: 'React', version: null, npmPkgName: 'react'},
+        {name: 'Polymer', version: null, npmPkgName: 'polymer-core'},
+        {name: 'Preact', version: null, npmPkgName: 'preact'},
+        {name: 'Angular', version: null, npmPkgName: 'angular'},
+        {name: 'jQuery', version: null, npmPkgName: 'jquery'},
+      ],
+    });
+    assert.equal(auditResult3.rawValue, true);
+  });
+
+  it('generates expected details', () => {
+    const auditResult = JsLibrariesAudit.audit({
+      JSLibraries: [
+        {name: 'lib1', version: '3.10.1', npmPkgName: 'lib1'},
+        {name: 'lib2', version: null, npmPkgName: 'lib2'},
+      ],
+    });
+    const expected = [
+      {
+        name: 'lib1',
+        npm: 'lib1',
+        version: '3.10.1',
+      },
+      {
+        name: 'lib2',
+        npm: 'lib2',
+        version: null,
+      },
+    ];
+    assert.equal(auditResult.rawValue, true);
+    assert.deepStrictEqual(auditResult.details.items, expected);
+  });
+});
diff --git a/lighthouse-core/test/audits/seo/font-size-test.js b/lighthouse-core/test/audits/seo/font-size-test.js
index 12bdad2f7..6f819bffd 100644
--- a/lighthouse-core/test/audits/seo/font-size-test.js
+++ b/lighthouse-core/test/audits/seo/font-size-test.js
@@ -7,7 +7,6 @@
 
 const FontSizeAudit = require('../../../audits/seo/font-size.js');
 const assert = require('assert');
-const CSSStyleDeclaration = require('../../../lib/web-inspector').CSSStyleDeclaration;
 
 const URL = 'https://example.com';
 const validViewport = 'width=device-width';
@@ -89,7 +88,7 @@ describe('SEO: Font size audit', () => {
   it('groups entries with same source, sorts them by coverage', () => {
     const style1 = {
       styleSheetId: 1,
-      type: CSSStyleDeclaration.Type.Regular,
+      type: 'Regular',
       range: {
         startLine: 123,
         startColumn: 10,
@@ -97,7 +96,7 @@ describe('SEO: Font size audit', () => {
     };
     const style2 = {
       styleSheetId: 1,
-      type: CSSStyleDeclaration.Type.Regular,
+      type: 'Regular',
       range: {
         startLine: 0,
         startColumn: 10,
diff --git a/lighthouse-core/test/gather/gather-runner-test.js b/lighthouse-core/test/gather/gather-runner-test.js
index 6e8ae8efe..8afb23fee 100644
--- a/lighthouse-core/test/gather/gather-runner-test.js
+++ b/lighthouse-core/test/gather/gather-runner-test.js
@@ -627,7 +627,7 @@ describe('GatherRunner', function() {
       mainRecord.localizedFailDescription = 'foobar';
       const error = GatherRunner.getPageLoadError(url, [mainRecord]);
       assert.equal(error.message, 'FAILED_DOCUMENT_REQUEST');
-      assert.ok(/Your page failed to load/.test(error.friendlyMessage));
+      assert.ok(/^Lighthouse was unable to reliably load/.test(error.friendlyMessage));
     });
 
     it('fails when page times out', () => {
@@ -635,7 +635,7 @@ describe('GatherRunner', function() {
       const records = [];
       const error = GatherRunner.getPageLoadError(url, records);
       assert.equal(error.message, 'NO_DOCUMENT_REQUEST');
-      assert.ok(/Your page failed to load/.test(error.friendlyMessage));
+      assert.ok(/^Lighthouse was unable to reliably load/.test(error.friendlyMessage));
     });
 
     it('fails when page returns with a 404', () => {
@@ -645,7 +645,7 @@ describe('GatherRunner', function() {
       mainRecord.statusCode = 404;
       const error = GatherRunner.getPageLoadError(url, [mainRecord]);
       assert.equal(error.message, 'ERRORED_DOCUMENT_REQUEST');
-      assert.ok(/Your page failed to load/.test(error.friendlyMessage));
+      assert.ok(/^Lighthouse was unable to reliably load/.test(error.friendlyMessage));
     });
 
     it('fails when page returns with a 500', () => {
@@ -655,7 +655,7 @@ describe('GatherRunner', function() {
       mainRecord.statusCode = 500;
       const error = GatherRunner.getPageLoadError(url, [mainRecord]);
       assert.equal(error.message, 'ERRORED_DOCUMENT_REQUEST');
-      assert.ok(/Your page failed to load/.test(error.friendlyMessage));
+      assert.ok(/^Lighthouse was unable to reliably load/.test(error.friendlyMessage));
     });
   });
 
diff --git a/lighthouse-core/test/lib/asset-saver-test.js b/lighthouse-core/test/lib/asset-saver-test.js
index 014ec4490..92242f4a2 100644
--- a/lighthouse-core/test/lib/asset-saver-test.js
+++ b/lighthouse-core/test/lib/asset-saver-test.js
@@ -170,4 +170,15 @@ describe('asset-saver helper', () => {
         });
     }, 40 * 1000);
   });
+
+  describe('loadArtifacts', () => {
+    it('loads artifacts from disk', async () => {
+      const artifactsPath = __dirname + '/../fixtures/artifacts/perflog/';
+      const artifacts = await assetSaver.loadArtifacts(artifactsPath);
+      assert.strictEqual(artifacts.LighthouseRunWarnings.length, 2);
+      assert.strictEqual(artifacts.URL.requestedUrl, 'https://www.reddit.com/r/nba');
+      assert.strictEqual(artifacts.devtoolsLogs.defaultPass.length, 555);
+      assert.strictEqual(artifacts.traces.defaultPass.traceEvents.length, 12);
+    });
+  });
 });
diff --git a/lighthouse-core/test/lib/traces/trace-parser-test.js b/lighthouse-core/test/lib/traces/trace-parser-test.js
deleted file mode 100644
index 4e770e4b2..000000000
--- a/lighthouse-core/test/lib/traces/trace-parser-test.js
+++ /dev/null
@@ -1,93 +0,0 @@
-/**
- * @license Copyright 2017 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';
-
-const TraceParser = require('../../../lib/traces/trace-parser');
-const fs = require('fs');
-const assert = require('assert');
-
-
-/* eslint-env jest */
-describe('traceParser parser', () => {
-  it('returns preact trace data the same as JSON.parse', (done) => {
-    const filename = `${__dirname}/../../fixtures/traces/progressive-app-m60.json`;
-    const readStream = fs.createReadStream(filename, {
-      encoding: 'utf-8',
-      // devtools sends traces in 10mb chunks, but this trace is 12MB so we'll do a few chunks
-      highWaterMark: 4 * 1024 * 1024,
-    });
-    const parser = new TraceParser();
-
-    readStream.on('data', (chunk) => {
-      parser.parseChunk(chunk);
-    });
-    readStream.on('end', () => {
-      const streamedTrace = parser.getTrace();
-      const readTrace = JSON.parse(fs.readFileSync(filename));
-
-      assert.equal(streamedTrace.traceEvents.length, readTrace.traceEvents.length);
-      streamedTrace.traceEvents.forEach((evt, i) => {
-        assert.deepStrictEqual(evt, readTrace.traceEvents[i]);
-      });
-
-      done();
-    });
-  }, 10000);
-
-
-  it('parses a trace > 256mb (slow)', () => {
-    const parser = new TraceParser();
-    let bytesRead = 0;
-    // FYI: this trace doesn't have a traceEvents property ;)
-    const filename = '/../../fixtures/traces/devtools-homepage-w-screenshots-trace.json';
-    const events = JSON.parse(fs.readFileSync(__dirname + filename));
-
-    /**
-     * This function will synthesize a trace that's over 256 MB. To do that, we'll take an existing
-     * trace and repeat the same events again and again until we've gone over 256 MB.
-     * Note: We repeat all but the last event, as it's the CpuProfile event, and it triggers
-     * specific handling in the devtools streaming parser.
-     * Once we reach > 256 MB, we add in the CpuProfile event.
-     */
-    function buildAndParse256mbTrace() {
-      const stripOuterBrackets = str => str.replace(/^\[/, '').replace(/\]$/, '');
-      const partialEventsStr = events => stripOuterBrackets(JSON.stringify(events));
-      const traceEventsStr = partialEventsStr(events.slice(0, events.length-2)) + ',';
-
-      // read the trace intro
-      parser.parseChunk(`{"traceEvents": [${traceEventsStr}`);
-      bytesRead += traceEventsStr.length;
-
-      // just keep reading until we've gone over 256 MB
-      // 256 MB is hard limit of a string in v8
-      // https://mobile.twitter.com/bmeurer/status/879276976523157505
-      while (bytesRead <= (Math.pow(2, 28)) - 16) {
-        parser.parseChunk(traceEventsStr);
-        bytesRead += traceEventsStr.length;
-      }
-
-      // the CPU Profiler event is last (and big), inject it just once
-      const lastEventStr = partialEventsStr(events.slice(-1));
-      parser.parseChunk(lastEventStr + ']}');
-      bytesRead += lastEventStr.length;
-    }
-
-    buildAndParse256mbTrace();
-    const streamedTrace = parser.getTrace();
-
-    assert.ok(bytesRead > 256 * 1024 * 1024, `${bytesRead} bytes read`);
-    assert.strictEqual(bytesRead, 270128965, `${bytesRead} bytes read`);
-
-    // if > 256 MB are read we should have ~480,000 trace events
-    assert.ok(streamedTrace.traceEvents.length > 400 * 1000, 'not >400,000 trace events');
-    assert.ok(streamedTrace.traceEvents.length > events.length * 5, 'not way more trace events');
-    assert.strictEqual(streamedTrace.traceEvents.length, 480151);
-
-    assert.deepStrictEqual(
-        streamedTrace.traceEvents[events.length - 2],
-        events[0]);
-  }, 40 * 1000);
-});
diff --git a/lighthouse-core/test/lib/web-inspector-test.js b/lighthouse-core/test/lib/web-inspector-test.js
index 46761aaee..6e5f41e99 100644
--- a/lighthouse-core/test/lib/web-inspector-test.js
+++ b/lighthouse-core/test/lib/web-inspector-test.js
@@ -12,7 +12,6 @@ const assert = require('assert');
 describe('Web Inspector lib', function() {
   it('WebInspector exported is the real one', () => {
     assert.equal(typeof WebInspector, 'object');
-    assert.ok(WebInspector.Color);
     assert.ok(WebInspector.CSSMetadata);
   });
 
diff --git a/lighthouse-core/test/results/sample_v2.json b/lighthouse-core/test/results/sample_v2.json
index 691428277..284058e3f 100644
--- a/lighthouse-core/test/results/sample_v2.json
+++ b/lighthouse-core/test/results/sample_v2.json
@@ -2394,6 +2394,37 @@
         "summary": {}
       }
     },
+    "js-libraries": {
+      "id": "js-libraries",
+      "title": "Detected JavaScript libraries",
+      "description": "All front-end JavaScript libraries detected on the page.",
+      "score": 1,
+      "scoreDisplayMode": "binary",
+      "rawValue": true,
+      "details": {
+        "type": "table",
+        "headings": [
+          {
+            "key": "name",
+            "itemType": "text",
+            "text": "Name"
+          },
+          {
+            "key": "version",
+            "itemType": "text",
+            "text": "Version"
+          }
+        ],
+        "items": [
+          {
+            "name": "jQuery",
+            "version": "2.1.1",
+            "npm": "jquery"
+          }
+        ],
+        "summary": {}
+      }
+    },
     "no-websql": {
       "id": "no-websql",
       "title": "Uses WebSQL DB",
@@ -3260,6 +3291,10 @@
           "id": "no-vulnerable-libraries",
           "weight": 1
         },
+        {
+          "id": "js-libraries",
+          "weight": 0
+        },
         {
           "id": "notification-on-start",
           "weight": 1
diff --git a/lighthouse-core/test/runner-test.js b/lighthouse-core/test/runner-test.js
index 3bc17cea5..068b2990a 100644
--- a/lighthouse-core/test/runner-test.js
+++ b/lighthouse-core/test/runner-test.js
@@ -639,7 +639,7 @@ describe('Runner', () => {
     assert.ok(lhr.audits['test-audit'].errorMessage.includes(NO_FCP.code));
     // And it bubbled up to the runtimeError.
     assert.strictEqual(lhr.runtimeError.code, NO_FCP.code);
-    assert.ok(lhr.runtimeError.message.includes(NO_FCP.code));
+    assert.ok(lhr.runtimeError.message.includes(NO_FCP.message));
   });
 
   it('can handle array of outputs', async () => {