Skip to content
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
31 changes: 26 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ on:
pull_request:
branches: [ master ]
jobs:
infra:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: microsoft/playwright-github-action@v1
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r local-requirements.txt
pip install .
- name: Lint
run: |
black --check .
mypy .
flake8 playwright tests
build:
timeout-minutes: 30
strategy:
Expand Down Expand Up @@ -40,16 +60,17 @@ jobs:
python -m pip install --upgrade pip
pip install -r local-requirements.txt
pip install .
- name: Lint
run: |
black --check .
mypy .
flake8 playwright tests
- name: Build driver
run: python build_driver.py
env:
PKG_CACHE_PATH: ${{ steps.node-pkg-cache.outputs.dir }}
- name: Build package
run: python build_package.py
- name: Test
if: ${{ matrix.os == 'windows-latest' }}
# pytest-xdist does not exit on Windows
# https://github.com/pytest-dev/pytest-xdist/issues/60
run: pytest -vv --browser=${{ matrix.browser }}
- name: Test
if: ${{ matrix.os != 'windows-latest' }}
run: pytest -vv --browser=${{ matrix.browser }} -n auto
3 changes: 2 additions & 1 deletion client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from playwright import browser_types


async def run():
async def run() -> None:
print("Launching browser...")
browser = await browser_types["webkit"].launch(headless=False)
print("Contexts in browser: %d" % len(browser.contexts))
Expand Down Expand Up @@ -59,6 +59,7 @@ async def run():

print("\nQuerying body...")
body1 = await page1.querySelector("body")
assert body1
print("Body text %s" % await body1.textContent())

print("Closing page1...")
Expand Down
4 changes: 2 additions & 2 deletions playwright/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
from types import SimpleNamespace
from typing import Dict, List, Union

if sys.version_info >= (3, 8):
if sys.version_info >= (3, 8): # pragma: no cover
from typing import Literal
else:
else: # pragma: no cover
from typing_extensions import Literal


Expand Down
4 changes: 2 additions & 2 deletions playwright/browser_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from types import SimpleNamespace
from typing import Any, Callable, Dict, List, Optional, Union, TYPE_CHECKING

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from playwright.browser import Browser


Expand Down Expand Up @@ -189,7 +189,7 @@ async def waitForEvent(
self, self._timeout_settings, event, predicate=predicate, timeout=timeout
)

def _on_close(self):
def _on_close(self) -> None:
if self._browser:
self._browser._contexts.remove(self)

Expand Down
15 changes: 8 additions & 7 deletions playwright/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ async def send(self, method: str, params: dict = None) -> Any:
params = dict()
return await self._scope.send_message_to_server(self._guid, method, params)

def _on_message(self, method: str, params: Dict):
self.emit(method, params)


class ChannelOwner(BaseEventEmitter):
def __init__(
self, scope: "ConnectionScope", guid: str, initializer: Dict, is_scope=False
self,
scope: "ConnectionScope",
guid: str,
initializer: Dict,
is_scope: bool = False,
) -> None:
super().__init__()
self._guid = guid
Expand All @@ -64,7 +65,7 @@ def create_child(self, guid: str) -> "ConnectionScope":
self._children.append(scope)
return scope

def dispose(self):
def dispose(self) -> None:
# Take care of hierarchy.
for child in self._children:
child.dispose()
Expand Down Expand Up @@ -98,7 +99,7 @@ def create_remote_object(self, type: str, guid: str, initializer: Dict) -> Any:


class ProtocolCallback:
def __init__(self, loop: asyncio.AbstractEventLoop):
def __init__(self, loop: asyncio.AbstractEventLoop) -> None:
self.stack_trace = "".join(traceback.format_stack()[-10:])
self.future = loop.create_future()

Expand Down Expand Up @@ -145,7 +146,7 @@ async def _send_message_to_server(
self._callbacks[id] = callback
return await callback.future

def _dispatch(self, msg: ParsedMessagePayload):
def _dispatch(self, msg: ParsedMessagePayload) -> None:

id = msg.get("id")
if id:
Expand Down
10 changes: 0 additions & 10 deletions playwright/console_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,10 @@ def type(self) -> str:
def text(self) -> str:
return self._initializer["text"]

@property
def defaultValue(self) -> str:
return self._initializer["defaultValue"]

@property
def args(self) -> List[JSHandle]:
return list(map(from_channel, self._initializer["args"]))

@property
def location(self) -> ConsoleMessageLocation:
return self._initializer["location"]

async def accept(self, prompt_text: str = None) -> None:
await self._channel.send("accept", dict(promptText=prompt_text))

async def dismiss(self) -> None:
await self._channel.send("dismiss")
6 changes: 3 additions & 3 deletions playwright/element_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
cast,
)

if sys.version_info >= (3, 8):
if sys.version_info >= (3, 8): # pragma: no cover
from typing import Literal
else:
else: # pragma: no cover
from typing_extensions import Literal

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from playwright.frame import Frame


Expand Down
2 changes: 1 addition & 1 deletion playwright/file_chooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from typing import List, Union, TYPE_CHECKING

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from playwright.page import Page
from playwright.element_handle import ElementHandle

Expand Down
6 changes: 3 additions & 3 deletions playwright/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
from playwright.serializers import normalize_file_payloads
from typing import Any, Awaitable, Dict, List, Optional, Union, TYPE_CHECKING, cast

if sys.version_info >= (3, 8):
if sys.version_info >= (3, 8): # pragma: no cover
from typing import Literal
else:
else: # pragma: no cover
from typing_extensions import Literal

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from playwright.page import Page


Expand Down
17 changes: 9 additions & 8 deletions playwright/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import fnmatch
import re
import traceback
from types import TracebackType

from typing import (
Any,
Expand All @@ -31,13 +32,13 @@

import sys

if sys.version_info >= (3, 8):
if sys.version_info >= (3, 8): # pragma: no cover
from typing import Literal, TypedDict
else:
else: # pragma: no cover
from typing_extensions import Literal, TypedDict


if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from playwright.network import Route, Request

Cookie = List[Dict[str, Union[str, int, bool]]]
Expand Down Expand Up @@ -98,7 +99,7 @@ class ParsedMessagePayload(TypedDict, total=False):


class URLMatcher:
def __init__(self, match: URLMatch):
def __init__(self, match: URLMatch) -> None:
self._callback: Optional[Callable[[str], bool]] = None
self._regex_obj: Optional[Pattern] = None
if isinstance(match, str):
Expand All @@ -124,7 +125,7 @@ def __init__(self, parent: Optional["TimeoutSettings"]) -> None:
self._timeout = 30000
self._navigation_timeout = 30000

def set_timeout(self, timeout: int):
def set_timeout(self, timeout: int) -> None:
self._timeout = timeout

def timeout(self) -> int:
Expand All @@ -134,7 +135,7 @@ def timeout(self) -> int:
return self._parent.timeout()
return 30000

def set_navigation_timeout(self, navigation_timeout: int):
def set_navigation_timeout(self, navigation_timeout: int) -> None:
self._navigation_timeout = navigation_timeout

def navigation_timeout(self) -> int:
Expand All @@ -155,7 +156,7 @@ class TimeoutError(Error):
pass


def serialize_error(ex: Exception, tb) -> ErrorPayload:
def serialize_error(ex: Exception, tb: Optional[TracebackType]) -> ErrorPayload:
return dict(message=str(ex), stack="".join(traceback.format_tb(tb)))


Expand Down Expand Up @@ -193,7 +194,7 @@ def __init__(
self.future = future
self.timeout_future = timeout_future

def reject(self, is_crash: bool, target: str):
def reject(self, is_crash: bool, target: str) -> None:
self.timeout_future.cancel()
if self.event == "close" and not is_crash:
return
Expand Down
4 changes: 2 additions & 2 deletions playwright/js_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from playwright.helper import Error, is_function_body
from typing import Any, Dict, List, Optional, TYPE_CHECKING

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from playwright.element_handle import ElementHandle


Expand Down Expand Up @@ -88,7 +88,7 @@ async def jsonValue(self) -> Any:
return parse_result(await self._channel.send("jsonValue"))


def is_primitive_value(value: Any):
def is_primitive_value(value: Any) -> bool:
return (
isinstance(value, bool)
or isinstance(value, int)
Expand Down
4 changes: 3 additions & 1 deletion playwright/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
playwright_object: Playwright


async def async_init():
async def async_init() -> None:
global playwright_object
package_path = os.path.dirname(os.path.abspath(__file__))
platform = sys.platform
Expand Down Expand Up @@ -61,6 +61,8 @@ async def async_init():
stderr=asyncio.subprocess.PIPE,
limit=32768,
)
assert proc.stdout
assert proc.stdin
connection = Connection(
proc.stdout, proc.stdin, create_remote_object, asyncio.get_event_loop()
)
Expand Down
4 changes: 2 additions & 2 deletions playwright/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from playwright.helper import Error, ContinueParameters
from typing import Any, Dict, List, Optional, Union, TYPE_CHECKING, cast

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from playwright.frame import Frame


Expand Down Expand Up @@ -104,7 +104,7 @@ async def fulfill(
response["body"] = body
response["isBase64"] = False
elif isinstance(body, bytes):
response["body"] = base64.b64encode(body)
response["body"] = base64.b64encode(body).decode()
response["isBase64"] = True
await self._channel.send("fulfill", response)

Expand Down
8 changes: 4 additions & 4 deletions playwright/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@
from types import SimpleNamespace
from typing import Any, Awaitable, Callable, Dict, List, Union, TYPE_CHECKING, cast

if sys.version_info >= (3, 8):
if sys.version_info >= (3, 8): # pragma: no cover
from typing import Literal
else:
else: # pragma: no cover
from typing_extensions import Literal

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from playwright.browser_context import BrowserContext


Expand Down Expand Up @@ -719,7 +719,7 @@ async def wait_for_event(

future = target._scope._loop.create_future()

def listener(e: Any = None):
def listener(e: Any = None) -> None:
if not predicate or predicate(e):
future.set_result(e)

Expand Down
4 changes: 2 additions & 2 deletions playwright/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def _run(self) -> None:
msg = buffer.decode("utf-8")
obj = json.loads(msg)

if "DEBUGP" in os.environ:
if "DEBUGP" in os.environ: # pragma: no cover
print("\x1b[33mRECV>\x1b[0m", json.dumps(obj, indent=2))
self.on_message(obj)
except asyncio.IncompleteReadError:
Expand All @@ -58,7 +58,7 @@ async def _run(self) -> None:

def send(self, message: Dict) -> None:
msg = json.dumps(message)
if "DEBUGP" in os.environ:
if "DEBUGP" in os.environ: # pragma: no cover
print("\x1b[32mSEND>\x1b[0m", json.dumps(message, indent=2))
data = bytes(msg, "utf-8")
self._output.write(len(data).to_bytes(4, byteorder="little", signed=False))
Expand Down
9 changes: 9 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ markers =
only_platform
[mypy]
ignore_missing_imports = True
python_version = 3.7
warn_unused_ignores = False
warn_redundant_casts = True
warn_unused_configs = True
check_untyped_defs = True
disallow_untyped_defs = True
[mypy-tests.*]
check_untyped_defs = False
disallow_untyped_defs = False
[flake8]
ignore =
E501
Expand Down
Loading