Skip to content

Commit

Permalink
Add keepAlive and browser options
Browse files Browse the repository at this point in the history
  • Loading branch information
kevva committed Sep 24, 2017
1 parent 74864f4 commit ccfe06e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = async (url, opts) => {

const uri = isUrl(url) ? url : fileUrl(url);
const {
cookies, crop, format, headers, height, hide, password, scale,
cookies, crop, format, headers, height, hide, keepAlive, password, scale,
script, selector, timeout, transparent, userAgent, username, width
} = opts;

Expand All @@ -52,7 +52,7 @@ module.exports = async (url, opts) => {
opts.omitBackground = true;
}

const browser = await puppeteer.launch();
const browser = opts.browser || await puppeteer.launch();
const page = await browser.newPage();
const viewport = {
height,
Expand Down Expand Up @@ -96,7 +96,11 @@ module.exports = async (url, opts) => {
}

const buf = await page.screenshot(opts);
await browser.close();
await page.close();

if (keepAlive !== true) {
await browser.close();
}

return buf;
};
18 changes: 17 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,24 @@ import isJpg from 'is-jpg';
import isPng from 'is-png';
import pify from 'pify';
import PNG from 'png-js';
import puppeteer from 'puppeteer';
import server from './fixtures/server';
import m from '.';
import screenshotStream from '.';

let browser;
let m;

test.before(async () => {
browser = await puppeteer.launch();
m = (url, opts) => screenshotStream(url, Object.assign({}, opts, {
browser,
keepAlive: true
}));
});

test.after(async () => {
await browser.close();
});

test('generate screenshot', async t => {
t.true(isPng(await m('http://yeoman.io', {
Expand Down

0 comments on commit ccfe06e

Please sign in to comment.