|
1 | 1 | /**
|
2 |
| - * @license Copyright 2017 The Lighthouse Authors. All Rights Reserved. |
| 2 | + * @license Copyright 2020 The Lighthouse Authors. All Rights Reserved. |
3 | 3 | * 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
|
4 | 4 | * 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.
|
5 | 5 | */
|
6 | 6 |
|
7 | 7 | /**
|
8 |
| - * @fileoverview Gathers console deprecation and intervention warnings logged by Chrome. |
| 8 | + * @fileoverview Gathers all entries logged to the console, including console API calls, |
| 9 | + * exceptions, and browser reports (on violations, interventions, deprecations, etc.). |
9 | 10 | */
|
10 | 11 |
|
11 | 12 | 'use strict';
|
12 | 13 |
|
13 | 14 | const Gatherer = require('./gatherer.js');
|
14 | 15 |
|
| 16 | +/** |
| 17 | + * @param {LH.Crdp.Runtime.RemoteObject} obj |
| 18 | + * @return {string} |
| 19 | + */ |
| 20 | +function remoteObjectToString(obj) { |
| 21 | + if (typeof obj.value !== 'undefined' || obj.type === 'undefined') { |
| 22 | + return String(obj.value); |
| 23 | + } |
| 24 | + if (typeof obj.description === 'string' && obj.description !== obj.className) { |
| 25 | + return obj.description; |
| 26 | + } |
| 27 | + const type = obj.subtype || obj.type; |
| 28 | + const className = obj.className || 'Object'; |
| 29 | + // Simulate calling String() on the object. |
| 30 | + return `[${type} ${className}]`; |
| 31 | +} |
| 32 | + |
15 | 33 | class ConsoleMessages extends Gatherer {
|
16 | 34 | constructor() {
|
17 | 35 | super();
|
18 |
| - /** @type {Array<LH.Crdp.Log.EntryAddedEvent>} */ |
| 36 | + /** @type {LH.Artifacts.ConsoleMessage[]} */ |
19 | 37 | this._logEntries = [];
|
20 |
| - this._onConsoleEntryAdded = this.onConsoleEntry.bind(this); |
| 38 | + |
| 39 | + this._onConsoleAPICalled = this.onConsoleAPICalled.bind(this); |
| 40 | + this._onExceptionThrown = this.onExceptionThrown.bind(this); |
| 41 | + this._onLogEntryAdded = this.onLogEntry.bind(this); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Handles events for when a script invokes a console API. |
| 46 | + * @param {LH.Crdp.Runtime.ConsoleAPICalledEvent} event |
| 47 | + */ |
| 48 | + onConsoleAPICalled(event) { |
| 49 | + const {type} = event; |
| 50 | + if (type !== 'warning' && type !== 'error') { |
| 51 | + // Only gather warnings and errors for brevity. |
| 52 | + return; |
| 53 | + } |
| 54 | + /** @type {LH.Crdp.Runtime.RemoteObject[]} */ |
| 55 | + const args = event.args || []; |
| 56 | + const text = args.map(remoteObjectToString).join(' '); |
| 57 | + if (!text && !event.stackTrace) { |
| 58 | + // No useful information from Chrome. Skip. |
| 59 | + return; |
| 60 | + } |
| 61 | + const {url, lineNumber, columnNumber} = |
| 62 | + event.stackTrace && event.stackTrace.callFrames[0] || {}; |
| 63 | + /** @type {LH.Artifacts.ConsoleMessage} */ |
| 64 | + const consoleMessage = { |
| 65 | + eventType: 'consoleAPI', |
| 66 | + source: type === 'warning' ? 'console.warn' : 'console.error', |
| 67 | + level: type, |
| 68 | + text, |
| 69 | + stackTrace: event.stackTrace, |
| 70 | + timestamp: event.timestamp, |
| 71 | + url, |
| 72 | + lineNumber, |
| 73 | + columnNumber, |
| 74 | + }; |
| 75 | + this._logEntries.push(consoleMessage); |
21 | 76 | }
|
22 | 77 |
|
23 | 78 | /**
|
24 |
| - * @param {LH.Crdp.Log.EntryAddedEvent} entry |
| 79 | + * Handles exception thrown events. |
| 80 | + * @param {LH.Crdp.Runtime.ExceptionThrownEvent} event |
25 | 81 | */
|
26 |
| - onConsoleEntry(entry) { |
27 |
| - this._logEntries.push(entry); |
| 82 | + onExceptionThrown(event) { |
| 83 | + const text = event.exceptionDetails.exception ? |
| 84 | + event.exceptionDetails.exception.description : event.exceptionDetails.text; |
| 85 | + if (!text) { |
| 86 | + return; |
| 87 | + } |
| 88 | + /** @type {LH.Artifacts.ConsoleMessage} */ |
| 89 | + const consoleMessage = { |
| 90 | + eventType: 'exception', |
| 91 | + source: 'exception', |
| 92 | + level: 'error', |
| 93 | + text, |
| 94 | + stackTrace: event.exceptionDetails.stackTrace, |
| 95 | + timestamp: event.timestamp, |
| 96 | + url: event.exceptionDetails.url, |
| 97 | + lineNumber: event.exceptionDetails.lineNumber, |
| 98 | + columnNumber: event.exceptionDetails.columnNumber, |
| 99 | + }; |
| 100 | + this._logEntries.push(consoleMessage); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Handles browser reports logged to the console, including interventions, |
| 105 | + * deprecations, violations, and more. |
| 106 | + * @param {LH.Crdp.Log.EntryAddedEvent} event |
| 107 | + */ |
| 108 | + onLogEntry(event) { |
| 109 | + const {source, level, text, stackTrace, timestamp, url, lineNumber} = event.entry; |
| 110 | + this._logEntries.push({ |
| 111 | + eventType: 'protocolLog', |
| 112 | + source, |
| 113 | + level, |
| 114 | + text, |
| 115 | + stackTrace, |
| 116 | + timestamp, |
| 117 | + url, |
| 118 | + lineNumber, |
| 119 | + }); |
28 | 120 | }
|
29 | 121 |
|
30 | 122 | /**
|
31 | 123 | * @param {LH.Gatherer.PassContext} passContext
|
32 | 124 | */
|
33 | 125 | async beforePass(passContext) {
|
34 | 126 | const driver = passContext.driver;
|
35 |
| - driver.on('Log.entryAdded', this._onConsoleEntryAdded); |
| 127 | + |
| 128 | + driver.on('Log.entryAdded', this._onLogEntryAdded); |
36 | 129 | await driver.sendCommand('Log.enable');
|
37 | 130 | await driver.sendCommand('Log.startViolationsReport', {
|
38 | 131 | config: [{name: 'discouragedAPIUse', threshold: -1}],
|
39 | 132 | });
|
| 133 | + |
| 134 | + driver.on('Runtime.consoleAPICalled', this._onConsoleAPICalled); |
| 135 | + driver.on('Runtime.exceptionThrown', this._onExceptionThrown); |
| 136 | + await driver.sendCommand('Runtime.enable'); |
40 | 137 | }
|
41 | 138 |
|
42 | 139 | /**
|
43 | 140 | * @param {LH.Gatherer.PassContext} passContext
|
44 | 141 | * @return {Promise<LH.Artifacts['ConsoleMessages']>}
|
45 | 142 | */
|
46 |
| - async afterPass(passContext) { |
47 |
| - await passContext.driver.sendCommand('Log.stopViolationsReport'); |
48 |
| - await passContext.driver.off('Log.entryAdded', this._onConsoleEntryAdded); |
49 |
| - await passContext.driver.sendCommand('Log.disable'); |
| 143 | + async afterPass({driver}) { |
| 144 | + await driver.sendCommand('Log.stopViolationsReport'); |
| 145 | + await driver.off('Log.entryAdded', this._onLogEntryAdded); |
| 146 | + await driver.sendCommand('Log.disable'); |
| 147 | + await driver.off('Runtime.consoleAPICalled', this._onConsoleAPICalled); |
| 148 | + await driver.off('Runtime.exceptionThrown', this._onExceptionThrown); |
| 149 | + await driver.sendCommand('Runtime.disable'); |
50 | 150 | return this._logEntries;
|
51 | 151 | }
|
52 | 152 | }
|
|
0 commit comments