Created
January 23, 2020 10:52
-
-
Save CrispusDH/dbaf39a7ce231faed2eb71b971d76ac6 to your computer and use it in GitHub Desktop.
selenoid + puppeteer
This file contains hidden or 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
| import { Browser } from 'puppeteer'; | |
| import * as puppeteer from 'puppeteer'; | |
| import axios from 'axios'; | |
| export const getBrowser = async (): Promise<Browser> => { | |
| switch (process.env.runningType) { | |
| case 'selenoid': { | |
| return await selenoidLaunch(); | |
| } | |
| case 'local': { | |
| return await localLaunch(); | |
| } | |
| } | |
| }; | |
| const localLaunch = async (): Promise<Browser> => { | |
| return puppeteer.launch( | |
| { | |
| headless: false, | |
| defaultViewport: { | |
| width: 1366, | |
| height:766 | |
| } | |
| }); | |
| }; | |
| const selenoidLaunch = async (): Promise<Browser> => { | |
| const selenoidUrl = '127.0.0.1'; | |
| const { data } = await axios.post( | |
| `http://${selenoidUrl}:4444/wd/hub/session`, | |
| { | |
| desiredCapabilities: | |
| { | |
| 'browserName': 'chrome', | |
| 'browserVersion': '77.0', | |
| 'selenoid:options': { | |
| sessionTimeout: '10m', | |
| enableVnc: true | |
| } | |
| } | |
| } | |
| ); | |
| const sessionId = data.sessionId; | |
| await new Promise((resolve) => setTimeout(resolve, 5000)); | |
| return await puppeteer.connect( | |
| { browserWSEndpoint: `ws://${selenoidUrl}:4444/devtools/${sessionId}` } | |
| ); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment