def launch(self) -> Browser: """Start chromium process.""" self.proc = subprocess.Popen( self.cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) atexit.register(self.killChrome) import time for _ in range(100): # wait for DevTools port to open for at least 10sec # setting timeout timer is bettter time.sleep(0.1) if self.proc.poll() is not None: raise BrowserError('Unexpectedly chrome process closed with ' f'return code: {self.proc.returncode}') msg = self.proc.stdout.readline().decode() if not msg: continue m = re.match(r'DevTools listening on (ws://.*)$', msg) if m is not None: break else: # This block called only when `for`-loop does not `break` raise BrowserError('Failed to connect DevTools port.') logger.debug(m.group(0)) connectionDelay = self.options.get('slowMo', 0) connection = Connection(m.group(1).strip(), connectionDelay) return Browser(connection, self.options.get('ignoreHTTPSErrors', False), self.killChrome)
async def remove_browser(self, browser: Browser) -> None: """Close browser and remove all references.""" del self.managed_browsers[browser] try: browser._connection._closeCallback = None await asyncio.wait_for(browser.close(), timeout=5) except Exception: self.logger.warning(f"Could not properly close browser.")
async def _shutdown_browser(self, browser: Browser) -> None: """Close browser and remove all references.""" logger.info(f"Shutting down browser: {browser}") # remove all pages from the browser. for page in await browser.pages(): await self._close_page(page) try: browser._connection._closeCallback = None await asyncio.wait_for(browser.close(), timeout=2) except asyncio.TimeoutError: logger.warning(f"Could not properly close browser: {browser}") del self.browsers[browser]
async def _shutdown_browser(self, browser: Browser) -> None: """Close browser and remove all references.""" logger.info(f"Removing browser: {browser}") # remove all pages from the browser. for page in await browser.pages(): await self._close_page(page) # disable self.__on_connection_close browser._connection._closeCallback = None # attempt to properly close browser. try: await asyncio.wait_for(browser.close(), timeout=2) except asyncio.TimeoutError: pass del self.browsers[browser]
async def ensureInitialPage(self, browser: Browser) -> None: 'Wait for initial page target to be created.' for target in browser.targets(): if (target.type == 'page'): return initialPagePromise = self._loop.create_future() def initialPageCallback() -> None: initialPagePromise.set_result(True) def check_target(target: Target) -> None: if (target.type == 'page'): initialPageCallback() listeners = [addEventListener(browser, 'targetcreated', check_target)] (await initialPagePromise) removeEventListeners(listeners)
async def ensureInitialPage(self, browser: Browser) -> None: """Wait for initial page target to be created.""" for target in browser.targets(): if target.type == 'page': return initialPagePromise = self._loop.create_future() def initialPageCallback() -> None: initialPagePromise.set_result(True) def check_target(target: Target) -> None: if target.type == 'page': initialPageCallback() listeners = [addEventListener(browser, 'targetcreated', check_target)] await initialPagePromise removeEventListeners(listeners)
def launch(self) -> Browser: """Start chromium process.""" self.proc = subprocess.Popen( self.cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) atexit.register(self.killChrome) import time while True: time.sleep(0.1) msg = self.proc.stdout.readline().decode() if not msg: continue m = re.match(r'DevTools listening on (ws://.*)$', msg) if m is not None: break logger.debug(m.group(0)) self.url = m.group(1).strip() connectionDelay = self.options.get('slowMo', 0) connection = Connection(self.url, connectionDelay) return Browser(connection, self.options.get('ignoreHTTPSErrors', False), self.killChrome)
async def launch(options={}, loop=asyncio_loop): global browser_id browser_id += 1 user_data_dir = '-'.join( (CHROME_PROFILE_PATH, str(os.getpid()), str(browser_id), str(bytearray(randbytes(4)).hex()))) chrome_arguments = list(DEFAULT_ARGS) chrome_arguments.append('--user-data-dir={}'.format(user_data_dir)) chrome_executable = ('/Users/brijesh/projects/cms/frontend/' 'node_modules/puppeteer/.local-chromium/' 'mac-494755/chrome-mac/Chromium.app/Contents/' 'MacOS/Chromium') if 'headless' not in options or options['headless']: chrome_arguments.append('--headless') chrome_arguments.append('--disable-gpu') chrome_arguments.append('--hide-scrollbars') chrome_arguments.append('--mute-audio') if 'args' in options and isinstance(options['args'], (list, tuple)): chrome_arguments = chrome_arguments + options['args'] # if 'executablePath' in options and \ # isinstance(options['executablePath'], (str, unicode)): # if 'dumpio' in options and options.dumpio: # chrome_process = subprocess.Popen( # [chrome_executable, *chrome_arguments], # # stdin=sys.stdin, # # stdout=sys.stdout, # # stderr=sys.stderr, # stdin=subprocess.PIPE, # stdout=subprocess.PIPE, # stderr=subprocess.PIPE, # shell=True # ) # else: # chrome_process = subprocess.Popen( # [chrome_executable, *chrome_arguments], # stderr=sys.stderr, # shell=True # ) async def get_process(exe, args): process = await asyncio.create_subprocess_exec( *[exe, *args], stderr=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE) return process chrome_process = await get_process(chrome_executable, chrome_arguments) proc_list = [chrome_process] def chrome_kill(): print('Killing chrome') proc_list[0].kill() shutil.rmtree(user_data_dir) signal.signal(signal.SIGTERM, chrome_kill) terminated = False browser_ws_endpoint = await wait_for_ws_endpoint(chrome_process) if not browser_ws_endpoint: chrome_process.kill() shutil.rmtree(user_data_dir) raise Exception('Failed to connect to chrome') connection_delay = options['sloMo'] if 'sloMo' in options else 0 connection = await Connection.create(browser_ws_endpoint, connection_delay, loop=asyncio_loop) ignore_https_errors = True if ('ignoreHTTPSErrors' in options and options['ignoreHTTPSErrors']) else False return Browser(connection, ignore_https_errors, chrome_kill)
async def connect(browser_ws_endpoint, ignore_https_errors=False): connection = await Connection.create(browser_ws_endpoint) return Browser(connection, ignore_https_errors)
def exit_if_wrong_url(page: Page, browser: Browser, url_to_check: str): if not page.url == url_to_check: __logger.error('Exit due to Wrong URL!') browser.close() exit()