feat(browser): Add browser.isConnected() (#4403)

Add `browser.isConnected()` to the public api to be able to tell when the browser is connected

fixes https://github.com/smooth-code/jest-puppeteer/pull/237#issuecomment-490260041
This commit is contained in:
Tony Brix 2019-05-09 18:24:52 -05:00 committed by Andrey Lushnikov
parent 13f73b5b3a
commit 511dcf9d32
4 changed files with 31 additions and 0 deletions

View File

@ -48,6 +48,7 @@
* [browser.createIncognitoBrowserContext()](#browsercreateincognitobrowsercontext)
* [browser.defaultBrowserContext()](#browserdefaultbrowsercontext)
* [browser.disconnect()](#browserdisconnect)
* [browser.isConnected()](#browserisconnected)
* [browser.newPage()](#browsernewpage)
* [browser.pages()](#browserpages)
* [browser.process()](#browserprocess)
@ -700,6 +701,12 @@ Returns the default browser context. The default browser context can not be clos
Disconnects Puppeteer from the browser, but leaves the Chromium process running. After calling `disconnect`, the [Browser] object is considered disposed and cannot be used anymore.
#### browser.isConnected()
- returns: <[boolean]>
Indicates that the browser is connected.
#### browser.newPage()
- returns: <[Promise]<[Page]>>

View File

@ -57,6 +57,13 @@ class Browser extends EventEmitter {
this._connection.dispose();
}
/**
* @return {boolean}
*/
isConnected() {
return !this._connection._closed;
}
/**
* @return {!BrowserContext}
*/

View File

@ -260,6 +260,13 @@ class Browser extends EventEmitter {
this._connection.dispose();
}
/**
* @return {boolean}
*/
isConnected() {
return !this._connection._closed;
}
/**
* @return {!Promise<!Object>}
*/

View File

@ -60,4 +60,14 @@ module.exports.addTests = function({testRunner, expect, headless, puppeteer, CHR
await remoteBrowser.disconnect();
});
});
describe('Browser.isConnected', () => {
it('should set the browser connected state', async({browser}) => {
const browserWSEndpoint = browser.wsEndpoint();
const newBrowser = await puppeteer.connect({browserWSEndpoint});
expect(newBrowser.isConnected()).toBe(true);
await newBrowser.disconnect();
expect(newBrowser.isConnected()).toBe(false);
});
});
};