From 855fd65a2f366d3039f8b1a09ace8d2ba463a019 Mon Sep 17 00:00:00 2001 From: "Atthaboon Sanurt (P'Art)" Date: Thu, 23 Sep 2021 09:35:31 +0700 Subject: [PATCH] Support Playwright v15 and trace log (#130) * Bump playwright from 1.11.1 to 1.15.0 Bumps [playwright](https://github.com/Microsoft/playwright-python) from 1.11.1 to 1.15.0. - [Release notes](https://github.com/Microsoft/playwright-python/releases) - [Commits](https://github.com/Microsoft/playwright-python/compare/v1.11.1...v1.15.0) --- updated-dependencies: - dependency-name: playwright dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] * Add missing playwright dependencies * Init support trace file * Update default browser for chrome * Update pythonpackage.yml * Update force tag Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: DESKTOP\atthaboon.s --- .github/workflows/pythonpackage.yml | 7 ++-- .gitignore | 4 ++- Examples/browser-management/tracing.robot | 33 +++++++++++++++++++ Examples/form-handler/checkbox-element.robot | 2 +- Examples/locator/chain-locator.robot | 4 +-- .../ikeywords/ibrowsermanagement_async.py | 11 +++++++ .../keywords/browsermanagement.py | 28 +++++++++++++++- .../library_context_factory.py | 8 +++-- .../playwright_browsermanagement.py | 11 +++++++ .../playwright/playwright_context.py | 13 +++++++- .../puppeteer_browsermanagement.py | 9 +++++ requirements.txt | 2 +- setup.py | 2 +- 13 files changed, 120 insertions(+), 14 deletions(-) create mode 100644 Examples/browser-management/tracing.robot diff --git a/.github/workflows/pythonpackage.yml b/.github/workflows/pythonpackage.yml index 7b9ff22..77a3a11 100644 --- a/.github/workflows/pythonpackage.yml +++ b/.github/workflows/pythonpackage.yml @@ -12,11 +12,11 @@ jobs: # python-version: [3.7, 3.8, 3.9] include: - python-version: 3.7 - browser: pwchrome + browser: chrome - python-version: 3.7 - browser: webkit + browser: ptchrome - python-version: 3.8 - browser: chrome + browser: webkit - python-version: 3.9 browser: firefox @@ -73,6 +73,7 @@ jobs: pip install -r demoapp/requirements.txt pyppeteer-install python -m playwright install + npx playwright install-deps python demoapp/server.py & - name: System test uses: GabrielBB/xvfb-action@v1 diff --git a/.gitignore b/.gitignore index 34b85f1..4aecaab 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,6 @@ Examples/logs/ Examples/tmp-download/test.csv Examples;tmp-download/test.csv Examples/test.csv -.venv/ \ No newline at end of file +.venv/ +Examples/trace.zip +Examples/traces/ diff --git a/Examples/browser-management/tracing.robot b/Examples/browser-management/tracing.robot new file mode 100644 index 0000000..7fb8742 --- /dev/null +++ b/Examples/browser-management/tracing.robot @@ -0,0 +1,33 @@ +*** Settings *** +Library PuppeteerLibrary +Test Teardown Close All Browser +Suite Teardown Close Puppeteer + +*** Variables *** +${DEFAULT_BROWSER} chrome + + +*** Test Cases *** +Tracing log without path + ${BROWSER} = Get variable value ${BROWSER} ${DEFAULT_BROWSER} + Open browser to test page http://127.0.0.1:7272/login-form-example.html + Run Keyword If '${BROWSER}' != 'ptchrome' Start tracing + Select Checkbox id=exampleCheck1 + Checkbox Should Be Selected id=exampleCheck1 + Run Keyword If '${BROWSER}' != 'ptchrome' Stop Tracing + +Tracing log with specific path + ${BROWSER} = Get variable value ${BROWSER} ${DEFAULT_BROWSER} + Open browser to test page http://127.0.0.1:7272/login-form-example.html + Run Keyword If '${BROWSER}' != 'ptchrome' Start tracing + Select Checkbox id=exampleCheck1 + Checkbox Should Be Selected id=exampleCheck1 + Run Keyword If '${BROWSER}' != 'ptchrome' Stop Tracing trace2.zip + +*** Keywords *** +Open browser to test page + [Arguments] ${url} + ${BROWSER} = Get variable value ${BROWSER} ${DEFAULT_BROWSER} + ${HEADLESS} Get variable value ${HEADLESS} ${False} + &{options} = create dictionary headless=${HEADLESS} + Open browser ${url} browser=${BROWSER} options=${options} diff --git a/Examples/form-handler/checkbox-element.robot b/Examples/form-handler/checkbox-element.robot index b989dbb..c5cbec7 100644 --- a/Examples/form-handler/checkbox-element.robot +++ b/Examples/form-handler/checkbox-element.robot @@ -4,7 +4,7 @@ Test Teardown Close All Browser Suite Teardown Close Puppeteer *** Variables *** -${DEFAULT_BROWSER} chrome +${DEFAULT_BROWSER} pwchrome *** Test Cases *** diff --git a/Examples/locator/chain-locator.robot b/Examples/locator/chain-locator.robot index 3ed9ddb..5dc7b43 100644 --- a/Examples/locator/chain-locator.robot +++ b/Examples/locator/chain-locator.robot @@ -1,12 +1,12 @@ *** Settings *** -Force Tags Ignore_chrome +Force Tags Ignore_ptchrome Library PuppeteerLibrary Test Setup Open browser to test page Test Teardown Close All Browser Suite Teardown Close Puppeteer *** Variables *** -${DEFAULT_BROWSER} pwchrome +${DEFAULT_BROWSER} chrome ${HOME_PAGE_URL} http://127.0.0.1:7272/register-form-example.html diff --git a/PuppeteerLibrary/ikeywords/ibrowsermanagement_async.py b/PuppeteerLibrary/ikeywords/ibrowsermanagement_async.py index fa343cc..f7cb986 100644 --- a/PuppeteerLibrary/ikeywords/ibrowsermanagement_async.py +++ b/PuppeteerLibrary/ikeywords/ibrowsermanagement_async.py @@ -28,6 +28,17 @@ async def wait_for_new_window_open(self, timeout=None): async def switch_window(self, locator='MAIN'): pass + ############################## + # Trace + ############################## + @abstractmethod + async def start_tracing(self): + pass + + @abstractmethod + async def stop_tracing(self, path=None): + pass + ############################## # Page ############################## diff --git a/PuppeteerLibrary/keywords/browsermanagement.py b/PuppeteerLibrary/keywords/browsermanagement.py index 1bc9871..dc45b61 100644 --- a/PuppeteerLibrary/keywords/browsermanagement.py +++ b/PuppeteerLibrary/keywords/browsermanagement.py @@ -1,6 +1,7 @@ from PuppeteerLibrary.base.librarycomponent import LibraryComponent from PuppeteerLibrary.base.robotlibcore import keyword from PuppeteerLibrary.ikeywords.ibrowsermanagement_async import iBrowserManagementAsync +from robot.libraries.BuiltIn import BuiltIn class BrowserManagementKeywords(LibraryComponent): @@ -18,8 +19,9 @@ def open_browser(self, url, browser="chrome", alias=None, options={}): The ``browser`` argument specifies which browser to use. | = Browser = | = Name(s) = | = Engine = | - | Google Chrome | chrome | Puppeteer | + | Google Chrome Default | chrome | Playwright | | Google Chrome Playwright | pwchrome | Playwright | + | Google Chrome Puppeteer | ptchrome | Puppeteer | | Webkit (Safari engine) | webkit | Playwright | | Firefox | firefox | Playwright | @@ -162,6 +164,30 @@ def enable_emulate_mode(self, emulate_name): """ return self.loop.run_until_complete(self.get_async_keyword_group().enable_emulate_mode_async(emulate_name)) + ############################## + # Trace + ############################## + @keyword + def start_tracing(self): + """Create trace log file + + # View the trace by running following command + + playwright show-trace trace.zip + """ + return self.loop.run_until_complete(self.get_async_keyword_group().start_tracing()) + + @keyword + def stop_tracing(self, path=None): + """Stop trace and generate the trace file. + + Default will be ``traces/.zip`` + """ + if path is None: + test_name = BuiltIn().get_variable_value("${TEST_NAME}") + path = 'traces/'+test_name+'.zip' + return self.loop.run_until_complete(self.get_async_keyword_group().stop_tracing(path)) + ############################## # Page ############################## diff --git a/PuppeteerLibrary/library_context/library_context_factory.py b/PuppeteerLibrary/library_context/library_context_factory.py index dd4e2e1..127e8e8 100644 --- a/PuppeteerLibrary/library_context/library_context_factory.py +++ b/PuppeteerLibrary/library_context/library_context_factory.py @@ -7,12 +7,14 @@ class LibraryContextFactory: def create(self, browser_type: str) -> iLibraryContext: """Return iLibraryContext based on specific browser_type - Create context based on browser type (chrome, pwchrome, safari, webkit) + Create context based on browser type (chrome, pwchrome, ptchrome, safari, webkit) """ if browser_type.lower() == 'chrome': - return PuppeteerContext(browser_type) - if browser_type.lower() == 'pwchrome': return PlaywrightContext(browser_type) + elif browser_type.lower() == 'pwchrome': + return PlaywrightContext(browser_type) + elif browser_type.lower() == 'ptchrome': + return PuppeteerContext(browser_type) elif browser_type.lower() == 'safari': return PlaywrightContext(browser_type) elif browser_type.lower() == 'webkit': diff --git a/PuppeteerLibrary/playwright/async_keywords/playwright_browsermanagement.py b/PuppeteerLibrary/playwright/async_keywords/playwright_browsermanagement.py index 80d932a..de39970 100644 --- a/PuppeteerLibrary/playwright/async_keywords/playwright_browsermanagement.py +++ b/PuppeteerLibrary/playwright/async_keywords/playwright_browsermanagement.py @@ -79,6 +79,17 @@ async def switch_window(self, locator='MAIN'): raise Exception('Sorry Switch window support only NEW, MAIN, title and url') raise Exception('Can\'t find specify page locator.') + ############################## + # Trace + ############################## + async def start_tracing(self): + await self.library_ctx.start_tracing() + + async def stop_tracing(self, path=None): + if path is None: + path = 'trace.zip' + await self.library_ctx.stop_tracing(path) + ############################## # Page ############################## diff --git a/PuppeteerLibrary/playwright/playwright_context.py b/PuppeteerLibrary/playwright/playwright_context.py index dff06fb..cb71c35 100644 --- a/PuppeteerLibrary/playwright/playwright_context.py +++ b/PuppeteerLibrary/playwright/playwright_context.py @@ -28,6 +28,7 @@ class PlaywrightContext(iLibraryContext): playwright: any = None browser: any = None + current_context: any = None current_page: any = None current_iframe = None @@ -63,7 +64,7 @@ async def start_server(self, options: dict={}): if self.playwright is None: self.playwright = await async_playwright().start() - if self.browser_type == "pwchrome": + if self.browser_type == "chrome" or self.browser_type == "pwchrome": self.browser = await self.playwright.chromium.launch( headless=merged_options['headless']) elif self.browser_type == "webkit": @@ -73,6 +74,7 @@ async def start_server(self, options: dict={}): self.browser = await self.playwright.firefox.launch( headless=merged_options['headless']) self.browser.accept_downloads = True + # self.current_context = await self.browser.new_context() async def stop_server(self): await self.playwright.stop() @@ -155,8 +157,17 @@ def get_async_keyword_group(self, keyword_group_name: str): } return switcher.get(keyword_group_name) + async def start_tracing(self): + if len(self.browser.contexts) > 0: + await self.browser.contexts[0].tracing.start(screenshots=True, snapshots=True) + + async def stop_tracing(self, path): + if len(self.browser.contexts) > 0: + await self.browser.contexts[0].tracing.stop(path=path) + def _reset_context(self): self.browser = None + self.current_context = None self.current_page = None self.current_iframe = None diff --git a/PuppeteerLibrary/puppeteer/async_keywords/puppeteer_browsermanagement.py b/PuppeteerLibrary/puppeteer/async_keywords/puppeteer_browsermanagement.py index 92d40a1..de4d171 100644 --- a/PuppeteerLibrary/puppeteer/async_keywords/puppeteer_browsermanagement.py +++ b/PuppeteerLibrary/puppeteer/async_keywords/puppeteer_browsermanagement.py @@ -88,6 +88,15 @@ async def switch_window(self, locator='MAIN'): raise Exception('Sorry Switch window support only NEW, MAIN, title and url') raise Exception('Can\'t find specify page locator.') + ############################## + # Trace + ############################## + async def start_tracing(self): + raise Exception('Not support') + + async def stop_tracing(self, path=None): + raise Exception('Not support') + ############################## # Page ############################## diff --git a/requirements.txt b/requirements.txt index 31c0d9b..0cc4040 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ versioneer>=0.18 robotframework>=3.2.1 -playwright==1.11.1 +playwright==1.15.0 pyppeteer==0.2.5 # git+https://github.com/qahive/pyppeteer.git@dev2 robotframework-libdoc2json>=0.4 diff --git a/setup.py b/setup.py index b10f6ab..5abbfc9 100644 --- a/setup.py +++ b/setup.py @@ -39,7 +39,7 @@ platforms='any', install_requires=[ 'robotframework>=3.2.1', - 'playwright==1.11.1', + 'playwright==1.15.0', 'pyppeteer==0.2.5', ], python_requires='>3.6',