Skip to content
This repository has been archived by the owner on Jun 6, 2019. It is now read-only.

Commit

Permalink
Merge 9338297 into 0c7a28c
Browse files Browse the repository at this point in the history
  • Loading branch information
csabapalfi committed Jun 21, 2018
2 parents 0c7a28c + 9338297 commit 022237b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 20 deletions.
49 changes: 39 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,51 @@ npm install puppeteer-network-stats
Requires puppeteer to be installed globally, then:

```sh
puppeteer-network-stats <url>
puppeteer-network-stats <url> <device>
```

e.g.

```sh
npm install --global puppeteer puppeteer-network-stats
```

```sh
puppeteer-network-stats https://www.google.com | jq .
[
{
"type": "Document",
"url": "https://www.google.com/",
"status": 200,
"size": 72313
{
"url": "https://www.google.com"
"requests": [
{
"type": "Document",
"url": "https://www.google.com/",
"status": 200,
"size": 72313
},
...
]
}
```

You can also specify a device to emulate. See the list of [all available devices here](https://github.com/GoogleChrome/puppeteer/blob/master/DeviceDescriptors.js).

```sh
puppeteer-network-stats https://www.google.com "iPhone X" | jq .
{
"url": "https://www.google.com"
"device": {
"name": "iPhone X",
...
},
...
"requests": [
{
"type": "Document",
"url": "https://www.google.com/",
"status": 200,
"size": 72313
},
...
]
}
```

## Usage - module
Expand All @@ -42,10 +71,10 @@ You can just require the run function (that's also used for the cli).

```js
const run = require('puppeteer-network-stats/run');
console.log(await run('https://www.google.com'));
console.log(await run('https://www.google.com', 'iPhone X'));
```

If you want to interact with your page, etc:
If you want to interact with your page and customize even more:

```js
const PuppeteerNetworkStats = require('puppeteer-network-stats');
Expand Down
17 changes: 13 additions & 4 deletions run.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#!/usr/bin/env node

const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const PuppeteerNetworkStats = require('./index');

async function run(url) {
async function run(url, deviceName) {
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const page = await browser.newPage();
const device = devices[deviceName];
if (device) {
page.emulate(device);
}

await page.setCacheEnabled(false);
const networkStats = new PuppeteerNetworkStats();
Expand All @@ -18,13 +23,17 @@ async function run(url) {
await networkStats.detach();
await browser.close();

return networkStats.getStats();
return {
url,
device,
requests: networkStats.getStats()
};
}

if (require.main === module) {
(async () => {
const [,,url] = process.argv;
console.log(JSON.stringify(await run(url), null, 2));
const [,,url, deviceName] = process.argv;
console.log(JSON.stringify(await run(url, deviceName), null, 2));
})();
}

Expand Down
39 changes: 33 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,40 @@ test('PuppeteerNetworkStats', async (t) => {
t.ok(mockClient.detach.calledOnce, 'detach calls client.detach');
});

test('run', async (t) => {
t.plan(2);
const url = 'https://www.google.com';

const scriptOutput = JSON.parse(exec('node run.js https://www.google.com'));
t.ok(scriptOutput.length > 0, 'runs as a script');
test('cli', (t) => {
t.plan(6);

const runCli = (device = '') =>
JSON.parse(exec(`node run.js ${url} "${device}"`));

const result = runCli();
t.equal(result.url, url, 'url logged when no emulation');
t.equal(result.device, undefined, 'no device logged when no emulation');
t.ok(result.requests.length > 0, 'has results when no emulation');

const mobileResult = runCli('iPhone X');
t.equal(mobileResult.url, url, 'url logged when emulating device');
t.equal(mobileResult.device.name, 'iPhone X', 'device logged when emulating device');
t.ok(mobileResult.requests.length > 0, 'has results when emulating device');

});

test('module', async (t) => {
t.plan(6);

const run = require('./run');
const moduleOutput = await run('https://www.google.com');
t.ok(moduleOutput.length > 0, 'runs as a module');
const runModule = async (device) => await run(url, device);

const result = await runModule();
t.equal(result.url, url, 'url logged when no emulation');
t.equal(result.device, undefined, 'no device logged when no emulation');
t.ok(result.requests.length > 0, 'has results when no emulation');

const mobileResult = await runModule('iPhone X');
t.equal(mobileResult.url, url, 'url logged when emulating device');
t.equal(mobileResult.device.name, 'iPhone X', 'device logged when emulating device');
t.ok(mobileResult.requests.length > 0, 'has results when emulating device');

});

0 comments on commit 022237b

Please sign in to comment.