async def set_view_port_size(self, width, height): await self.library_ctx.get_current_page().get_page().setViewport({ "width": str2int(width), "height": str2int(height) })
async def start_server(self, options: dict = {}): default_args = [] merged_options = { 'slowMo': 0, 'headless': True, 'devtools': False, 'defaultViewport': { 'width': 1366, 'height': 768 } } merged_options = {**merged_options, **options} for key in merged_options.keys(): if key in ['headless', 'devtools', 'ignoreHTTPSErrors']: merged_options[key] = str2bool(merged_options[key]) elif key in ['slowMo']: merged_options[key] = str2int(merged_options[key]) elif key == 'defaultViewport': merged_options[key] = { 'width': str2int(merged_options[key]['width']), 'height': str2int(merged_options[key]['height']) } if self.debug_mode is True: merged_options = {**merged_options, **self.debug_mode_options} if 'win' not in sys.platform.lower(): default_args = ['--no-sandbox', '--disable-setuid-sandbox'] for support_key in self.page_support_options: if support_key in options: merged_options[support_key] = options[support_key] self.browser = await launch(**merged_options, args=default_args)
async def create_new_page(self, options: dict = {}) -> BasePage: device_options = { 'accept_downloads': True, 'viewport': { 'width': 1366, 'height': 768 } } for support_key in self.page_support_options: if support_key in options: device_options[support_key] = options[support_key] if support_key in ['accept_downloads', 'ignore_https_errors']: device_options[support_key] = str2bool( device_options[support_key]) # Force support viewport if support_key == 'viewport': if 'width' in options.keys(): device_options['viewport']['width'] = str2int( options['width']) if 'height' in device_options.keys(): device_options['viewport']['height'] = str2int( options['height']) if 'emulate' in options: device_options = self.playwright.devices[options['emulate']] if 'state_ref' in options: device_options['storage_state'] = './states/state-' + \ options['state_ref'] + '.json' new_page = await self.browser.new_page(**device_options) self.current_page = PlaywrightPage(new_page) return self.current_page
async def wait_for_response_url(self, url, status=200, body=None, timeout=None): url = str2str(url) status = str2int(status) res = await self.library_ctx.get_current_page().get_page( ).waitForResponse( lambda res: re.search(url, res.url ) is not None and res.status == status, options={ 'timeout': self.timestr_to_secs_for_default_timeout(timeout) * 1000 }) try: res_text = (await res.text()) except: res_text = '' if body is None or re.search(body, res_text.replace('\n', '')): log_str = 'Wait for request url: ' + res.url if res_text != '': log_str += '\n' + res_text self.info(log_str) else: raise Exception('Can\'t match response body with ' + body + ' \n ' + res_text) return DotDict({ 'url': res.url, 'status': res.status, 'body': res_text })
async def start_server(self, options: dict = {}): default_options = { 'slowMo': 0, 'headless': True, 'devtools': False, 'viewport': { 'width': 1366, 'height': 768, }, 'accept_downloads': True } merged_options = default_options merged_options = {**merged_options, **options} for key in merged_options.keys(): if key in ['headless', 'devtools', 'accept_downloads', 'is_mobile']: merged_options[key] = str2bool(merged_options[key]) elif key == 'width': merged_options['viewport']['width'] = str2int( merged_options[key]) elif key == 'height': merged_options['viewport']['height'] = str2int( merged_options[key]) elif key in ['slowMo']: merged_options[key] = str2int(merged_options[key]) # Only start new Playwright server. If server didn't start before if self.playwright is None: self.playwright = await async_playwright().start() if self.browser_type == "chrome" or self.browser_type == "pwchrome": proxy = None if 'proxy' in merged_options: proxy = merged_options['proxy'] # Chromium can use proxy only via global launch del merged_options['proxy'] self.browser = await self.playwright.chromium.launch( headless=merged_options['headless'], proxy=proxy) elif self.browser_type == "webkit": self.browser = await self.playwright.webkit.launch( headless=merged_options['headless']) elif self.browser_type == "firefox": self.browser = await self.playwright.firefox.launch( headless=merged_options['headless']) self.browser.accept_downloads = True
async def mouse_move(self, x, y): x = str2int(x) y = str2int(y) await self.library_ctx.get_current_page().get_page().mouse.move(x, y)