-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed: manage browser instances to improve performance of tests and…
… using this library as a JS module
- Loading branch information
Showing
9 changed files
with
122 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const puppeteer = require( 'puppeteer' ); | ||
|
||
/** | ||
* This class approach makes it easy to open multiple browser instances with | ||
* different arguments in case that is ever required. | ||
*/ | ||
class BrowserHandler { | ||
constructor() { | ||
const launchBrowser = async() => { | ||
this.browser = false; | ||
this.browser = await puppeteer.launch( { | ||
headless: 'new', | ||
devtools: false | ||
} ); | ||
this.browser.on( 'disconnected', launchBrowser ); | ||
}; | ||
|
||
this.exit = ()=>{ | ||
this.browser.off( 'disconnected', launchBrowser ); | ||
this.browser.close(); | ||
}; | ||
|
||
( async() => { | ||
await launchBrowser(); | ||
} )(); | ||
} | ||
} | ||
|
||
const getBrowser = ( handler ) => | ||
new Promise( ( resolve ) => { | ||
const browserCheck = setInterval( () => { | ||
if ( handler.browser !== false ) { | ||
clearInterval( browserCheck ); | ||
resolve( handler.browser ); | ||
} | ||
}, 100 ); | ||
} ); | ||
|
||
const closeBrowser = ( handler ) => { | ||
return handler.exit(); | ||
}; | ||
|
||
module.exports = { BrowserHandler, getBrowser, closeBrowser }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.