Skip to content

Commit

Permalink
Network emulation, todos
Browse files Browse the repository at this point in the history
  • Loading branch information
mixu committed Oct 14, 2015
1 parent e00c31f commit 23d535d
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 47 deletions.
13 changes: 6 additions & 7 deletions bin/usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ OPTIONS - NETWORK EMULATION

--list-network-conditions Show the list of known network condition presets.

--latency <ms> RTT in ms.

--download <Bps> Download rate in Bps

--upload <Bps> Upload rate in Bps

OPTIONS - DEVICE EMULATION

--list-devices Show the list of known devices to emulate.
Expand Down Expand Up @@ -99,13 +105,6 @@ OPTIONS - NETWORK EMULATION

--emulate-network <profile>

--latency <ms> RTT in ms.

--download <Bps> Download rate in Bps

--upload <Bps> Upload rate in Bps


OPTIONS - CONTENT INJECTION

--css <path|str>
Expand Down
22 changes: 22 additions & 0 deletions electron/target-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ TargetWindow.prototype.initialize = function(task, onDone) {
});
}

var has = {
latency: typeof task.latency === 'number',
download: typeof task.download === 'number',
upload: typeof task.upload === 'number',
};

if (has.latency || has.download || has.upload) {
// when not set, default to "WiFi" profile
self.window.webContents.session.enableNetworkEmulation({
latency: has.latency ? task.latency : 2,
downloadThroughput: has.download ? task.download : 3932160,
uploadThroughput: has.upload ? task.upload : 3932160
});
}

// to work around https://github.com/atom/electron/issues/1580
this.window.webContents.executeJavaScript(fs.readFileSync(path.resolve(__dirname + '/preload.js'), 'utf8'));
};
Expand All @@ -103,6 +118,13 @@ TargetWindow.prototype.reset = function() {
self.window.webContents.session.cookies.remove(cookie, function() {});
});
}
// reset network emulation
var hasNetworkEmulation = (typeof task.latency === 'number' ||
typeof task.download === 'number' || typeof task.upload === 'number');

if (hasNetworkEmulation) {
self.window.webContents.session.disableNetworkEmulation();
}
};

TargetWindow.prototype.getDimensions = function(selector, onDone) {
Expand Down
3 changes: 3 additions & 0 deletions lib/args-to-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ function toTask(url, size, args, filename) {
format: args.format,
quality: args.quality,
'user-agent': args['user-agent'],
latency: args.latency,
download: args.download,
upload: args.upload,
};
var sizeStr = size.width + 'x' + size.height;
if (args.cookie) {
Expand Down
3 changes: 3 additions & 0 deletions lib/default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = {
quality: 75,
'user-agent': '',
'cookie': '',
latency: null,
download: null,
upload: null,

// these are mostly used to figure out what options are unknown and hence should be passed to Chrome
version: false,
Expand Down
21 changes: 10 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ See: https://github.com/atom/electron/issues/228

## Device and network emulation

Test site: http://speedof.me/

Test site: http://www.mydevice.io/

latency Double - RTT in ms
downloadThroughput Double - Download rate in Bps
uploadThroughput Double - Upload rate in Bps
Expand Down Expand Up @@ -139,17 +143,15 @@ enableDeviceEmulation: https://github.com/atom/electron/blob/master/docs/api/web

enableNetworkEmulation: https://github.com/atom/electron/blob/master/docs/api/session.md#sessionenablenetworkemulationoptions

## API

- resolve args
- resolve mountpoints
- run server
- run electron

# nonfeatures

- cropping a specific region and/or offset ({top}x{left}x{width}x{height} on CLI)
- transparent bg or color
- HTTP options
- CURL-like:
- https://code.google.com/p/chromium/codesearch#chromium/src/third_party/WebKit/Source/devtools/front_end/network/NetworkLogView.js&q=_generateCurlCommand&sq=package:chromium&type=cs&l=1725
- custom headers
- basic auth
- custom behavior on different HTTP codes
- thumbnails / montage
- diffing: https://github.com/Huddle/Resemble.js
- test suite definitions (mocha compatible assertions)
Expand All @@ -166,6 +168,3 @@ enableNetworkEmulation: https://github.com/atom/electron/blob/master/docs/api/se
- www_google_com_1024x768.jpeg
- www_google_com_768x1024.jpeg
- www_google_com_1280x768.jpeg
- streaming API
- chainable
- request-like .defaults
76 changes: 47 additions & 29 deletions test/args.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ argsToTasks = function(args) {
if (task['cookie'] === '') {
delete task['cookie'];
}
if (!task.upload) {
delete task.upload;
}
if (!task.download) {
delete task.download;
}
if (!task.latency) {
delete task.latency;
}
if (!task.selector) {
delete task.selector;
}
Expand Down Expand Up @@ -477,17 +486,44 @@ describe('args to tasks', function() {
]);
});

it('accepts multiple --delay(s)');
it('accepts --latency <ms> <url> <resolution>', function() {
assert.deepEqual(argsToTasks(['--latency', '2', 'http://google.com', '1024x768']), [
{
url: 'http://google.com/',
size: { width: 1024, height: 768 },
out: process.cwd() + '/google.com-1024x768.png',
latency: 2,
}
]);
});

it('accepts a file:// url');
it('accepts --download <bps> <url> <resolution>', function() {
assert.deepEqual(argsToTasks(['--download', '2', 'http://google.com', '1024x768']), [
{
url: 'http://google.com/',
size: { width: 1024, height: 768 },
out: process.cwd() + '/google.com-1024x768.png',
download: 2,
}
]);
});

it('accepts --emulate-network <profile>');
it('accepts --upload <bps> <url> <resolution>', function() {
assert.deepEqual(argsToTasks(['--upload', '2', 'http://google.com', '1024x768']), [
{
url: 'http://google.com/',
size: { width: 1024, height: 768 },
out: process.cwd() + '/google.com-1024x768.png',
upload: 2,
}
]);
});

it('accepts --latency <ms>');
it('accepts --emulate-network <profile>');

it('accepts --download <Bps>');
it('accepts multiple --delay(s)');

it('accepts --upload <Bps>');
it('accepts a file:// url');

it('accepts --js <str>');
it('accepts --js <path>');
Expand All @@ -498,38 +534,20 @@ describe('args to tasks', function() {

it('accepts --max-wait <ms>');
it('accepts --debug');
// --debug (pop open electron window, verbose logging)

it('accepts --device <json>');

// Capture strings of HTML
// --stdin-html
// smart filenames
// good looking messages V Generated 3 screenshots from 2 urls
it('accepts a file path option');
// for specific file names

// --parallel <n> (num windows)

// when given multiple pages on the same domain (??): use the full path + qs
// -> https://github.com/sindresorhus/filenamify-url
// good looking messages V Generated 3 screenshots from 2 urls

// options:
// --delay name of callback
// --filename <template>
// --keywords (http://viewportsizes.com/)
// maybe not:
// --hide
// other actions on elements ? custom script??
// --debug (pop open electron window, verbose logging)
// --parallel <n> (num windows)
// Remap --crop ???

// cool:
// - tiling (montage) use case
// - diffing

// web options:
// - cookies, headers, http auth, userAgent
// - ignore ssl errors (?)
// - pipe in content (?)


// special format strings
});
2 changes: 2 additions & 0 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ describe('integration tests', function() {

});

it('accepts --stdin-html + html');

describe('errors', function() {

it('exits with the right error code');
Expand Down

0 comments on commit 23d535d

Please sign in to comment.