From 4d8502d8f8d2ceef594e65de1617eb2e21ba3092 Mon Sep 17 00:00:00 2001 From: "Atthaboon Sanurt (P'Art)" Date: Thu, 1 Apr 2021 22:46:50 +0700 Subject: [PATCH] Enhance support browser options (#105) * Fix open browser support option as None * Add demo other options * Clean up code * Update ubuntu lib version * Update libvpx to v6 * Add missing lib * support browser options * support all options * Add documents * Fix path issue * Fix puppeteer options --- .../open-close-browser.robot | 12 ++++++++- .../keywords/browsermanagement.py | 7 +++++ .../playwright/playwright_context.py | 13 ++++++--- .../puppeteer/puppeteer_context.py | 27 +++++++++---------- 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/Examples/browser-management/open-close-browser.robot b/Examples/browser-management/open-close-browser.robot index 836b686..639d026 100644 --- a/Examples/browser-management/open-close-browser.robot +++ b/Examples/browser-management/open-close-browser.robot @@ -12,7 +12,17 @@ ${DEFAULT_BROWSER} pwchrome Open browser without option ${BROWSER} = Get variable value ${BROWSER} ${DEFAULT_BROWSER} Open browser http://127.0.0.1:7272/basic-html-elements.html browser=${BROWSER} - + +Open ssl issue page + ${BROWSER} = Get variable value ${BROWSER} ${DEFAULT_BROWSER} + ${HEADLESS} = Get variable value ${HEADLESS} ${False} + &{options} = create dictionary + ... headless=${HEADLESS} + ... ignore_https_errors=${True} + ... ignoreHTTPSErrors=${True} + Open browser https://expired.badssl.com/ browser=${BROWSER} options=${options} + Capture Page Screenshot + Switch to new browser ${BROWSER} = Get variable value ${BROWSER} ${DEFAULT_BROWSER} ${HEADLESS} = Get variable value ${HEADLESS} ${False} diff --git a/PuppeteerLibrary/keywords/browsermanagement.py b/PuppeteerLibrary/keywords/browsermanagement.py index 8262185..c80f4e9 100644 --- a/PuppeteerLibrary/keywords/browsermanagement.py +++ b/PuppeteerLibrary/keywords/browsermanagement.py @@ -31,12 +31,19 @@ def open_browser(self, url, browser="chrome", alias=None, options={}): | height | default 768 | | emulate | iPhone 11 | + **Other options** + pwchrome, webkit and firefox please visit: https://playwright.dev/python/docs/api/class-browser?_highlight=new_page#browsernew_pagekwargs + chrome please visit: https://pptr.dev/#?product=Puppeteer&version=v8.0.0&show=api-puppeteerlaunchoptions + Example: | &{options} = | create dictionary | headless=${False} | | `Open browser` | https://www.w3schools.com/html/html_forms.asp | options=${options} | """ + if options is None: + options = {} + self.info(url) library_context = self.ctx.create_library_context(alias, browser) self.loop.run_until_complete(library_context.start_server(options)) diff --git a/PuppeteerLibrary/playwright/playwright_context.py b/PuppeteerLibrary/playwright/playwright_context.py index 3ed6795..5e8ac1c 100644 --- a/PuppeteerLibrary/playwright/playwright_context.py +++ b/PuppeteerLibrary/playwright/playwright_context.py @@ -29,6 +29,8 @@ class PlaywrightContext(iLibraryContext): browser: any = None current_page: any = None current_iframe = None + + page_support_options = ['accept_downloads', 'bypass_csp', 'color_scheme', 'device_scale_factor', 'extra_http_headers', 'geolocation', 'has_touch', 'http_credentials', 'ignore_https_errors', 'is_mobile', 'java_script_enabled', 'locale', 'no_viewport', 'offline', 'permissions', 'proxy', 'record_har_omit_content', 'record_har_path', 'record_video_dir', 'record_video_size', 'timezone_id', 'user_agent', 'viewport'] def __init__(self, browser_type: str): super().__init__(browser_type) @@ -43,8 +45,7 @@ async def start_server(self, options: dict={}): 'accept_downloads': True } merged_options = default_options - if options is not None: - merged_options = {**merged_options, **options} + merged_options = {**merged_options, **options} self.playwright = await async_playwright().start() if self.browser_type == "pwchrome": @@ -55,7 +56,7 @@ async def start_server(self, options: dict={}): headless=merged_options['headless']) elif self.browser_type == "firefox": self.browser = await self.playwright.firefox.launch( - headless=merged_options['headless']) + headless=merged_options['headless']) self.browser.accept_downloads = True async def stop_server(self): @@ -71,8 +72,14 @@ async def create_new_page(self, options: dict={}) -> BasePage: device_options = { 'accept_downloads': True } + + for support_key in self.page_support_options: + if support_key in options: + device_options[support_key] = options[support_key] + if 'emulate' in options: device_options = self.playwright.devices[options['emulate']] + new_page = await self.browser.new_page(**device_options) self.current_page = PlaywrightPage(new_page) return self.current_page diff --git a/PuppeteerLibrary/puppeteer/puppeteer_context.py b/PuppeteerLibrary/puppeteer/puppeteer_context.py index 2a5e68c..cc84615 100644 --- a/PuppeteerLibrary/puppeteer/puppeteer_context.py +++ b/PuppeteerLibrary/puppeteer/puppeteer_context.py @@ -32,37 +32,36 @@ class PuppeteerContext(iLibraryContext): 'devtools': False } + page_support_options = ['ignoreHTTPSErrors', 'headless', 'executablePath', 'slowMo', 'defaultViewport', 'handleSIGINT', 'handleSIGTERM', 'handleSIGHUP', 'userDataDir', 'env', 'devtools'] + def __init__(self, browser_type: str): super().__init__(browser_type) async def start_server(self, options: dict={}): default_args = [] - default_options = { + merged_options = { 'slowMo': 0, 'headless': True, 'devtools': False, - 'width': 1366, - 'height': 768 + 'defaultViewport': { + 'width': 1366, + 'height': 768 + } } - merged_options = default_options - - if options is not None: - merged_options = {**merged_options, **options} + merged_options = {**merged_options, **options} 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( - headless=merged_options['headless'], - slowMo=merged_options['slowMo'], - devtools=merged_options['devtools'], - defaultViewport={ - 'width': merged_options['width'], - 'height': merged_options['height'] - }, + **merged_options, args=default_args) async def stop_server(self):