Last active
January 18, 2023 07:48
-
-
Save dstyle0210/f43a7e43859da90f619a53f51f989699 to your computer and use it in GitHub Desktop.
playwright 프로세스 pid 구하기 ( launchServer )
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
| // 참고 : https://playwright.dev/docs/next/api/class-browsertype#browser-type-launch-server | |
| // v1.29.0 기준 정상동작 확인 됨. | |
| import { chromium , device } from "playwright"; | |
| import type { Browser , Page } from "playwright"; | |
| // 기본형태 (간단버전?) | |
| const createBrowserBasic = async () => { | |
| let option = { // https://playwright.dev/docs/api/class-browsertype#browser-type-launch | |
| // 주로 쓰는것만 정리함 | |
| headless:false, // {boolean} [headless=true] 브라우저를 보여줄것인지 안보여줄것(headless)인지 설정 | |
| // {string} [executablePath=playwright번들브라우저실행경로] 별도로 브라우저를 띄울꺼라면 설정 | |
| // 프록시설정이라던가, 보안상으로 외부번들브라우저는 동작 안하는 경우가 많음(특히 내부 보안으로 번들브라우저 익명모드일때 접근불가 케이스) | |
| executablePath:"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" | |
| }; | |
| const browser = await chromium.launch(option); | |
| const page = browser.newPage(); // 페이지 생성 | |
| } | |
| // 프로세스를 통해서 pid값을 구할때 사용. (나중에 precess kill 할려고) | |
| const createBrowserForPid = async ():Promise<void> => { | |
| let option = {}; // chromium.launch 옵션과 같은듯 함. | |
| const browserServer = await chromium.launchServer(option); | |
| const pid = browserServer.process().pid; // browserServer.process() 의 리턴은 ChildProcess 임. | |
| const wsEndPoint = browserServer.wsEndpoint(); | |
| const browser = await chromium.connect(wsEndPoint); | |
| const page = browser.newPage(); // 페이지 생성 | |
| } | |
| // pid도 구하고, 모바일로 띄우고 | |
| const createBrowserForMobile = async () => { | |
| let 옵션 = {}; // chromium.launch 옵션과 같은듯 함. | |
| const browserServer = await chromium.launchServer(옵션); | |
| const pid = browserServer.process().pid; // browserServer.process() 의 리턴은 ChildProcess 임. | |
| const wsEndPoint = browserServer.wsEndpoint(); | |
| const browser = await chromium.connect(wsEndPoint); | |
| const Mobile = device["iPhone 6"]; // key명 리스트는 playwright types 안에 device 확인하면 됨. | |
| const context = await browser.newContext({...Mobile}); | |
| const page = context.newPage(); // 디바이스 변경해서 페이지 생성 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment