Use puppeteer.launch instead of browser constructor (#255)

This patch:
- split browser launching logic from Browser into `lib/Launcher.js`
- introduce `puppeteer` namespace which currently has a single `launch`
  method to start a browser

With this patch, the browser is no longer created with the `new
Browser(..)` command. Instead, it should be "launched" via the
`puppeteer.launch` method:

```js
const puppeteer = require('puppeteer');
puppeteer.launch().then(async browser => {
  ...
});
```

With this approach browser instance lifetime matches the lifetime of
actual browser process. This helps us:
- remove proxy streams, e.g. browser.stderr and browser.stdout
- cleanup browser class and make it possible to connect to remote
  browser
- introduce events on the browser instance, e.g. 'page' event. In case
  of lazy-launching browser, we should've launch browser when an event
  listener is added, which is unneded comlpexity.
This commit is contained in:
Andrey Lushnikov
2017-08-14 18:08:06 -07:00
committed by GitHub
parent 0a3dd4e727
commit 13e8580a34
19 changed files with 301 additions and 291 deletions

View File

@@ -17,7 +17,7 @@
const fs = require('fs');
const rm = require('rimraf').sync;
const path = require('path');
const Browser = require('../../../../lib/Browser');
const puppeteer = require('../../../..');
const checkPublicAPI = require('..');
const SourceFactory = require('../../SourceFactory');
const GoldenUtils = require('../../../../test/golden-utils');
@@ -25,7 +25,7 @@ const GoldenUtils = require('../../../../test/golden-utils');
const OUTPUT_DIR = path.join(__dirname, 'output');
const GOLDEN_DIR = path.join(__dirname, 'golden');
const browser = new Browser({args: ['--no-sandbox']});
let browser;
let page;
let specName;
@@ -34,6 +34,7 @@ jasmine.getEnv().addReporter({
});
beforeAll(SX(async function() {
browser = await puppeteer.launch({args: ['--no-sandbox']});
page = await browser.newPage();
if (fs.existsSync(OUTPUT_DIR))
rm(OUTPUT_DIR);