From 2586e69e91e2269f77af58bf470418444d45d9f3 Mon Sep 17 00:00:00 2001 From: Paul Irish Date: Sun, 27 Mar 2016 12:17:07 -0700 Subject: [PATCH] Enable device emulation, network throttling, and disable disk cache. --- helpers/browser/driver.js | 15 ++++++- helpers/emulation.js | 90 +++++++++++++++++++++++++++++++++++++++ index.js | 2 +- 3 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 helpers/emulation.js diff --git a/helpers/browser/driver.js b/helpers/browser/driver.js index 778952d80c78..1746788b23d1 100644 --- a/helpers/browser/driver.js +++ b/helpers/browser/driver.js @@ -18,6 +18,8 @@ const chromeRemoteInterface = require('chrome-remote-interface'); const NetworkRecorder = require('../network-recorder'); +const emulation = require('../emulation'); + const npmlog = require('npmlog'); const port = process.env.PORT || 9222; @@ -68,7 +70,9 @@ class ChromeProtocol { chromeRemoteInterface({port: port}, chrome => { this._chrome = chrome; this.beginLogging(); - resolve(null); + this.beginEmulation().then(_ => { + resolve(null); + }); }).on('error', e => reject(e)); }); } @@ -226,6 +230,15 @@ class ChromeProtocol { }); }); } + + beginEmulation() { + return new Promise((resolve, reject) => { + emulation.enableNexus5X(this); + emulation.enableNetworkThrottling(this); + emulation.disableCache(this); + this.pendingCommandsComplete().then(_ => resolve()); + }); + } } function _log(level, prefix, data) { diff --git a/helpers/emulation.js b/helpers/emulation.js new file mode 100644 index 000000000000..7ae52e57f5bb --- /dev/null +++ b/helpers/emulation.js @@ -0,0 +1,90 @@ +/** + * @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'; + +// Nexus5X metrics adaparted from emulated_devices/module.json +const DEVICE_EMULATION_METRICS = { + mobile: true, + screenWidth: 412, + screenHeight: 732, + width: 412, + height: 732, + positionX: 0, + positionY: 0, + scale: 1, + deviceScaleFactor: 2.625, + fitWindow: false, + screenOrientation: { + angle: 0, + type: 'portraitPrimary' + } +}; + +const DEVICE_EMULATION_USERAGENT = { + userAgent: 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36' + + '(KHTML, like Gecko) Chrome/51.0.2690.0 Mobile Safari/537.36' +}; + +const NETWORK_THROTTLING_CONFIG = { + latency: 150, // 150ms + downloadThroughput: 1.6 * 1024 * 1024 / 8, // 1.6Mbps + uploadThroughput: 750 * 1024 / 8, // 750Kbps + offline: false +}; + +function enableNexus5X(driver) { + driver.sendCommand('Emulation.setDeviceMetricsOverride', DEVICE_EMULATION_METRICS); + driver.sendCommand('Network.setUserAgentOverride', DEVICE_EMULATION_USERAGENT); + driver.sendCommand('Emulation.setTouchEmulationEnabled', { + enabled: true, + configuration: 'mobile' + }); + + // from emulation/TouchModel.js + /* eslint-disable no-proto */ /* global window, document */ + const injectedFunction = function() { + const touchEvents = ['ontouchstart', 'ontouchend', 'ontouchmove', 'ontouchcancel']; + var recepients = [window.__proto__, document.__proto__]; + for (var i = 0; i < touchEvents.length; ++i) { + for (var j = 0; j < recepients.length; ++j) { + if (!(touchEvents[i] in recepients[j])) { + Object.defineProperty(recepients[j], touchEvents[i], { + value: null, writable: true, configurable: true, enumerable: true + }); + } + } + } + }; + /* eslint-enable */ + driver.sendCommand('Page.addScriptToEvaluateOnLoad', { + scriptSource: '(' + injectedFunction.toString() + ')()' + }); +} + +function disableCache(driver) { + driver.sendCommand('Network.setCacheDisabled', {cacheDisabled: true}); +} + +function enableNetworkThrottling(driver) { + driver.sendCommand('Network.emulateNetworkConditions', NETWORK_THROTTLING_CONFIG); +} + +module.exports = { + enableNexus5X: enableNexus5X, + enableNetworkThrottling: enableNetworkThrottling, + disableCache: disableCache +}; diff --git a/index.js b/index.js index aa1301f46119..9a13149e495e 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ */ 'use strict'; -const defaultUrl = 'https://voice-memos.appspot.com'; +const defaultUrl = 'https://m.flipkart.com'; const ChromeProtocol = require('./helpers/browser/driver'); const Auditor = require('./auditor');