Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Playwright v15 and trace log #130

Merged
merged 6 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ Examples/logs/
Examples/tmp-download/test.csv
Examples;tmp-download/test.csv
Examples/test.csv
.venv/
.venv/
Examples/trace.zip
Examples/traces/
33 changes: 33 additions & 0 deletions Examples/browser-management/tracing.robot
Original file line number Diff line number Diff line change
@@ -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}
2 changes: 1 addition & 1 deletion Examples/form-handler/checkbox-element.robot
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Test Teardown Close All Browser
Suite Teardown Close Puppeteer

*** Variables ***
${DEFAULT_BROWSER} chrome
${DEFAULT_BROWSER} pwchrome


*** Test Cases ***
Expand Down
4 changes: 2 additions & 2 deletions Examples/locator/chain-locator.robot
Original file line number Diff line number Diff line change
@@ -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


Expand Down
11 changes: 11 additions & 0 deletions PuppeteerLibrary/ikeywords/ibrowsermanagement_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
##############################
Expand Down
28 changes: 27 additions & 1 deletion PuppeteerLibrary/keywords/browsermanagement.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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 |

Expand Down Expand Up @@ -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/<testcasename>.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
##############################
Expand Down
8 changes: 5 additions & 3 deletions PuppeteerLibrary/library_context/library_context_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
##############################
Expand Down
13 changes: 12 additions & 1 deletion PuppeteerLibrary/playwright/playwright_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class PlaywrightContext(iLibraryContext):

playwright: any = None
browser: any = None
current_context: any = None
current_page: any = None
current_iframe = None

Expand Down Expand Up @@ -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":
Expand All @@ -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()
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
##############################
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down