Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ puppeteer.launch().then(async browser => {
```

#### browser.close()
- returns: <[Promise]>

Closes browser with all the pages (if any were opened). The browser object itself is considered to be disposed and could not be used anymore.

Expand Down
10 changes: 6 additions & 4 deletions lib/Browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@

const {helper} = require('./helper');
const Page = require('./Page');
const EventEmitter = require('events');

class Browser {
class Browser extends EventEmitter {
/**
* @param {!Connection} connection
* @param {boolean} ignoreHTTPSErrors
* @param {function()=} closeCallback
* @param {function():Promise=} closeCallback
*/
constructor(connection, ignoreHTTPSErrors, closeCallback) {
super();
this._ignoreHTTPSErrors = ignoreHTTPSErrors;
this._screenshotTaskQueue = new TaskQueue();
this._connection = connection;
Expand Down Expand Up @@ -54,9 +56,9 @@ class Browser {
return version.product;
}

close() {
async close() {
this._connection.dispose();
this._closeCallback.call(null);
await this._closeCallback.call(null);
}
}

Expand Down
36 changes: 29 additions & 7 deletions lib/Launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,14 @@ class Launcher {
chromeProcess.stderr.pipe(process.stderr);
}

// Cleanup as processes exit.
if (temporaryUserDataDir)
chromeProcess.once('close', () => removeSync(temporaryUserDataDir));
const waitForChromeToClose = new Promise(fulfill => {
chromeProcess.once('close', () => {
// Cleanup as processes exit.
if (temporaryUserDataDir)
removeSync(temporaryUserDataDir);
fulfill();
});
});

const listeners = [ helper.addEventListener(process, 'exit', killChrome) ];
if (options.handleSIGINT !== false)
Expand All @@ -102,12 +107,29 @@ class Launcher {
throw e;
}

/**
* @return {Promise}
*/
function killChrome() {
helper.removeEventListeners(listeners);
if (process.platform === 'win32')
childProcess.execSync(`taskkill /pid ${chromeProcess.pid} /T /F`);
else
process.kill(-chromeProcess.pid);
if (temporaryUserDataDir) {
// Force kill chrome.
if (process.platform === 'win32')
childProcess.execSync(`taskkill /pid ${chromeProcess.pid} /T /F`);
else
process.kill(-chromeProcess.pid, 'SIGKILL');
// Attempt to remove temporary profile directory to avoid littering.
try {
removeSync(temporaryUserDataDir);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aslushnikov I copied this from your previous PR. I'm not sure if we need it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's to handle "process.exit " case.

} catch (e) { }
} else {
// Terminate chrome gracefully.
if (process.platform === 'win32')
chromeProcess.kill();
else
process.kill(-chromeProcess.pid, 'SIGTERM');
}
return waitForChromeToClose;
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('Puppeteer', function() {
const options = Object.assign({userDataDir}, defaultBrowserOptions);
const browser = await puppeteer.launch(options);
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
browser.close();
await browser.close();
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
rm(userDataDir);
}));
Expand All @@ -117,7 +117,7 @@ describe('Puppeteer', function() {
options.args = [`--user-data-dir=${userDataDir}`].concat(options.args);
const browser = await puppeteer.launch(options);
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
browser.close();
await browser.close();
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
rm(userDataDir);
}));
Expand Down
6 changes: 3 additions & 3 deletions utils/doclint/check_public_api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ function checkDuplicates(doc) {
classes.add(cls.name);
const members = new Set();
for (const member of cls.membersArray) {
if (members.has(member.name))
errors.push(`Duplicate declaration of method ${cls.name}.${member.name}()`);
members.add(member.name);
if (members.has(member.type + ' ' + member.name))
errors.push(`Duplicate declaration of ${member.type} ${cls.name}.${member.name}()`);
members.add(member.type + ' ' + member.name);
const args = new Set();
for (const arg of member.argsArray) {
if (args.has(arg.name))
Expand Down