Skip to content

Commit

Permalink
Enable device emulation, network throttling, and disable disk cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Mar 27, 2016
1 parent 79b1101 commit 2586e69
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
15 changes: 14 additions & 1 deletion helpers/browser/driver.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
});
}
Expand Down Expand Up @@ -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) {
Expand Down
90 changes: 90 additions & 0 deletions 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
};
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -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');
Expand Down

0 comments on commit 2586e69

Please sign in to comment.