1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | 1× 1× 1× 7× 7× 7× 1× 6× 1× 5× 5× 5× 1× 7× 7× 1× 6× 7× 7× 1× 6× 6× 1× 6× 6× 6× 6× 6× 1× 15× 34× 54× 1× 10× 20× 1× 5× 7× 5× 7× 5× 7× 5× 7× 1× 5× 5× 3× 2× 4× 4× 2× 4× 1× 5× 3× 2× 2× 2× 4× 1× 8× 8× 8× 3× 5× 5× 5× 5× 5× 5× 5× 7× 5× 7× 7× 5× 1× 5× 1× 5× 1× | /** * @license * Copyright 2016 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 assets = require('./lib/save-assets.js'); const Gather = require('./gatherers/gather.js'); function loadPage(driver, gatherers, options) { const loadPage = options.flags.loadPage; const url = options.url; if (loadPage) { return driver.gotoURL(url, {waitForLoad: true}); } return Promise.resolve(); } function reloadPage(driver, options) { // Since a Page.reload command does not let a service worker take over, we // navigate away and then come back to reload. We do not `waitForLoad` on // about:blank since a page load event is never fired on it. return driver.gotoURL('about:blank') // Wait a bit for about:blank to "take hold" before switching back to the page. .then(_ => new Promise((resolve, reject) => setTimeout(resolve, 300))) .then(_ => driver.gotoURL(options.url, {waitForLoad: true})); } function setupDriver(driver, gatherers, options) { return new Promise((resolve, reject) => { // Enable emulation. if (options.flags.mobile) { return resolve(driver.beginEmulation()); } // noop if no mobile emulation resolve(); }).then(_ => { return driver.cleanAndDisableBrowserCaches(); }).then(_ => { // Force SWs to update on load. return driver.forceUpdateServiceWorkers(); }); } // Enable tracing and network record collection. function beginPassiveCollection(driver) { return driver.beginTrace() .then(_ => driver.beginNetworkCollect()); } function endPassiveCollection(options, tracingData) { const driver = options.driver; return driver.endNetworkCollect().then(networkRecords => { tracingData.networkRecords = networkRecords; }).then(_ => { return driver.endTrace(); }).then(traceContents => { tracingData.traceContents = traceContents; }); } function phaseRunner(gatherers) { return function runPhase(gatherFun) { return gatherers.reduce((chain, gatherer) => { return chain.then(_ => gatherFun(gatherer)); }, Promise.resolve()); }; } function shouldRunPass(gatherers, phases) { return phases.reduce((shouldRun, phase) => { return shouldRun || gatherers.some(gatherer => gatherer[phase] !== Gather.prototype[phase]); }, false); } function firstPass(driver, gatherers, options, tracingData) { const runPhase = phaseRunner(gatherers); return runPhase(gatherer => gatherer.setup(options)) .then(_ => beginPassiveCollection(driver)) .then(_ => runPhase(gatherer => gatherer.beforePageLoad(options))) // Load page, gather from browser, stop profilers. .then(_ => loadPage(driver, gatherers, options)) .then(_ => runPhase(gatherer => gatherer.profiledPostPageLoad(options))) .then(_ => endPassiveCollection(options, tracingData)) .then(_ => runPhase(gatherer => gatherer.postProfiling(options, tracingData))); } function secondPass(driver, gatherers, options) { const phases = ['reloadSetup', 'beforeReloadPageLoad', 'afterReloadPageLoad']; if (!shouldRunPass(gatherers, phases)) { return Promise.resolve(); } const runPhase = phaseRunner(gatherers); // Reload page for SW, etc. return runPhase(gatherer => gatherer.reloadSetup(options)) .then(_ => runPhase(gatherer => gatherer.beforeReloadPageLoad(options))) .then(_ => reloadPage(driver, options)) .then(_ => runPhase(gatherer => gatherer.afterReloadPageLoad(options))); } function thirdPass(driver, gatherers, options) { if (!shouldRunPass(gatherers, ['afterSecondReloadPageLoad'])) { return Promise.resolve(); } const runPhase = phaseRunner(gatherers); // Reload page again for HTTPS redirect options.url = options.url.replace(/^https/, 'http'); return reloadPage(driver, options) .then(_ => runPhase(gatherer => gatherer.afterSecondReloadPageLoad(options))); } function run(gatherers, options) { const driver = options.driver; const tracingData = {}; if (typeof options.url !== 'string' || options.url.length === 0) { return Promise.reject(new Error('You must provide a url to scheduler')); } const runPhase = phaseRunner(gatherers); return driver.connect() .then(_ => setupDriver(driver, gatherers, options)) .then(_ => firstPass(driver, gatherers, options, tracingData)) .then(_ => secondPass(driver, gatherers, options)) .then(_ => thirdPass(driver, gatherers, options)) // Finish and teardown. .then(_ => driver.disconnect()) .then(_ => runPhase(gatherer => gatherer.tearDown(options))) .then(_ => { // Collate all the gatherer results. const artifacts = gatherers.reduce((artifacts, gatherer) => { artifacts[gatherer.name] = gatherer.artifact; return artifacts; }, { networkRecords: tracingData.networkRecords, traceContents: tracingData.traceContents }); // Ignoring these two flags since this functionality is not exposed by the module. /* istanbul ignore if */ Iif (options.flags.saveArtifacts) { assets.saveArtifacts(artifacts); } /* istanbul ignore if */ if (options.flags.saveAssets) { assets.saveAssets(options, artifacts); } return artifacts; }); } module.exports = { loadPage, reloadPage, setupDriver, beginPassiveCollection, endPassiveCollection, phaseRunner, run }; |