Skip to content

Commit

Permalink
chore: roll Playwright to 1.19.0 (#1157)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Feb 14, 2022
1 parent c98fda5 commit 54b4bdf
Show file tree
Hide file tree
Showing 31 changed files with 505 additions and 89 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Playwright is a Python library to automate [Chromium](https://www.chromium.org/H

| | Linux | macOS | Windows |
| :--- | :---: | :---: | :---: |
| Chromium <!-- GEN:chromium-version -->99.0.4837.0<!-- GEN:stop --> ||||
| Chromium <!-- GEN:chromium-version -->100.0.4863.0<!-- GEN:stop --> ||||
| WebKit <!-- GEN:webkit-version -->15.4<!-- GEN:stop --> ||||
| Firefox <!-- GEN:firefox-version -->96.0.1<!-- GEN:stop --> ||||

Expand Down
7 changes: 5 additions & 2 deletions playwright/_impl/_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ async def _expect_impl(
del expect_options["useInnerText"]
result = await self._actual_locator._expect(expression, expect_options)
if result["matches"] == self._is_not:
actual = result.get("received")
log = "\n".join(result.get("log", "")).strip()
if log:
log = "\nCall log:\n" + log
if expected is not None:
raise AssertionError(f"{message} '{expected}' {log}")
raise AssertionError(f"{message} {log}")
raise AssertionError(
f"{message} '{expected}'\nActual value: {actual} {log}"
)
raise AssertionError(f"{message}\nActual value: {actual} {log}")


class PageAssertions(AssertionsBase):
Expand Down
11 changes: 7 additions & 4 deletions playwright/_impl/_browser_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,13 @@ def _on_page(self, page: Page) -> None:
def _on_route(self, route: Route, request: Request) -> None:
for handler_entry in self._routes:
if handler_entry.matches(request.url):
if handler_entry.handle(route, request):
self._routes.remove(handler_entry)
if not len(self._routes) == 0:
asyncio.create_task(self._disable_interception())
try:
handler_entry.handle(route, request)
finally:
if not handler_entry.is_active:
self._routes.remove(handler_entry)
if not len(self._routes) == 0:
asyncio.create_task(self._disable_interception())
break
route._internal_continue()

Expand Down
6 changes: 2 additions & 4 deletions playwright/_impl/_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ def compute_driver_executable() -> Path:

def get_driver_env() -> dict:
env = os.environ.copy()
env["PW_CLI_TARGET_LANG"] = "python"
env[
"PW_CLI_TARGET_LANG_VERSION"
] = f"{sys.version_info.major}.{sys.version_info.minor}"
env["PW_LANG_NAME"] = "python"
env["PW_LANG_NAME_VERSION"] = f"{sys.version_info.major}.{sys.version_info.minor}"
env["PW_CLI_DISPLAY_VERSION"] = version
return env
7 changes: 4 additions & 3 deletions playwright/_impl/_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ async def body(self) -> bytes:
result = await self._request._channel.send_return_as_dict(
"fetchResponseBody",
{
"fetchUid": self._fetch_uid(),
"fetchUid": self._fetch_uid,
},
)
if result is None:
Expand All @@ -400,18 +400,19 @@ async def dispose(self) -> None:
await self._request._channel.send(
"disposeAPIResponse",
{
"fetchUid": self._fetch_uid(),
"fetchUid": self._fetch_uid,
},
)

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

async def _fetch_log(self) -> List[str]:
return await self._request._channel.send(
"fetchLog",
{
"fetchUid": self._fetch_uid(),
"fetchUid": self._fetch_uid,
},
)

Expand Down
6 changes: 4 additions & 2 deletions playwright/_impl/_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,10 @@ async def fill(
) -> None:
await self._channel.send("fill", locals_to_params(locals()))

def locator(self, selector: str, has_text: Union[str, Pattern] = None) -> Locator:
return Locator(self, selector, has_text=has_text)
def locator(
self, selector: str, has_text: Union[str, Pattern] = None, has: Locator = None
) -> Locator:
return Locator(self, selector, has_text=has_text, has=has)

def frame_locator(self, selector: str) -> FrameLocator:
return FrameLocator(self, selector)
Expand Down
21 changes: 11 additions & 10 deletions playwright/_impl/_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,17 @@ def __init__(
def matches(self, request_url: str) -> bool:
return self.matcher.matches(request_url)

def handle(self, route: "Route", request: "Request") -> bool:
try:
result = cast(
Callable[["Route", "Request"], Union[Coroutine, Any]], self.handler
)(route, request)
if inspect.iscoroutine(result):
asyncio.create_task(result)
finally:
self._handled_count += 1
return self._handled_count >= self._times
def handle(self, route: "Route", request: "Request") -> None:
self._handled_count += 1
result = cast(
Callable[["Route", "Request"], Union[Coroutine, Any]], self.handler
)(route, request)
if inspect.iscoroutine(result):
asyncio.create_task(result)

@property
def is_active(self) -> bool:
return self._handled_count < self._times


def is_safe_close_error(error: Exception) -> bool:
Expand Down
28 changes: 25 additions & 3 deletions playwright/_impl/_locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import pathlib
import sys
from typing import (
Expand Down Expand Up @@ -53,13 +54,18 @@
if TYPE_CHECKING: # pragma: no cover
from playwright._impl._frame import Frame
from playwright._impl._js_handle import JSHandle
from playwright._impl._page import Page

T = TypeVar("T")


class Locator:
def __init__(
self, frame: "Frame", selector: str, has_text: Union[str, Pattern] = None
self,
frame: "Frame",
selector: str,
has_text: Union[str, Pattern] = None,
has: "Locator" = None,
) -> None:
self._frame = frame
self._selector = selector
Expand All @@ -75,6 +81,11 @@ def __init__(
escaped = escape_with_quotes(has_text, '"')
self._selector += f" >> :scope:has-text({escaped})"

if has:
if has._frame != frame:
raise Error('Inner "has" locator must belong to the same frame.')
self._selector += " >> has=" + json.dumps(has._selector)

def __repr__(self) -> str:
return f"<Locator frame={self._frame!r} selector={self._selector!r}>"

Expand All @@ -96,6 +107,10 @@ async def _with_element(
finally:
await handle.dispose()

@property
def page(self) -> "Page":
return self._frame.page

async def bounding_box(self, timeout: float = None) -> Optional[FloatRect]:
return await self._with_element(
lambda h, _: h.bounding_box(),
Expand Down Expand Up @@ -184,9 +199,13 @@ def locator(
self,
selector: str,
has_text: Union[str, Pattern] = None,
has: "Locator" = None,
) -> "Locator":
return Locator(
self._frame, f"{self._selector} >> {selector}", has_text=has_text
self._frame,
f"{self._selector} >> {selector}",
has_text=has_text,
has=has,
)

def frame_locator(self, selector: str) -> "FrameLocator":
Expand Down Expand Up @@ -538,11 +557,14 @@ def __init__(self, frame: "Frame", frame_selector: str) -> None:
self._dispatcher_fiber = frame._connection._dispatcher_fiber
self._frame_selector = frame_selector

def locator(self, selector: str, has_text: Union[str, Pattern] = None) -> Locator:
def locator(
self, selector: str, has_text: Union[str, Pattern] = None, has: "Locator" = None
) -> Locator:
return Locator(
self._frame,
f"{self._frame_selector} >> control=enter-frame >> {selector}",
has_text=has_text,
has=has,
)

def frame_locator(self, selector: str) -> "FrameLocator":
Expand Down
20 changes: 19 additions & 1 deletion playwright/_impl/_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from playwright._impl._wait_helper import WaitHelper

if TYPE_CHECKING: # pragma: no cover
from playwright._impl._fetch import APIResponse
from playwright._impl._frame import Frame


Expand Down Expand Up @@ -124,7 +125,7 @@ def post_data_json(self) -> Optional[Any]:
@property
def post_data_buffer(self) -> Optional[bytes]:
b64_content = self._initializer.get("postData")
if not b64_content:
if b64_content is None:
return None
return base64.b64decode(b64_content)

Expand Down Expand Up @@ -200,8 +201,25 @@ async def fulfill(
body: Union[str, bytes] = None,
path: Union[str, Path] = None,
contentType: str = None,
response: "APIResponse" = None,
) -> None:
params = locals_to_params(locals())
if response:
del params["response"]
params["status"] = (
params["status"] if params.get("status") else response.status
)
params["headers"] = (
params["headers"] if params.get("headers") else response.headers
)
from playwright._impl._fetch import APIResponse

if body is None and path is None and isinstance(response, APIResponse):
if response._request._connection is self._connection:
params["fetchResponseUid"] = response._fetch_uid
else:
body = await response.body()

length = 0
if isinstance(body, str):
params["body"] = body
Expand Down
14 changes: 9 additions & 5 deletions playwright/_impl/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,13 @@ def _on_frame_detached(self, frame: Frame) -> None:
def _on_route(self, route: Route, request: Request) -> None:
for handler_entry in self._routes:
if handler_entry.matches(request.url):
if handler_entry.handle(route, request):
self._routes.remove(handler_entry)
if len(self._routes) == 0:
asyncio.create_task(self._disable_interception())
try:
handler_entry.handle(route, request)
finally:
if not handler_entry.is_active:
self._routes.remove(handler_entry)
if len(self._routes) == 0:
asyncio.create_task(self._disable_interception())
return
self._browser_context._on_route(route, request)

Expand Down Expand Up @@ -686,8 +689,9 @@ def locator(
self,
selector: str,
has_text: Union[str, Pattern] = None,
has: "Locator" = None,
) -> "Locator":
return self._main_frame.locator(selector, has_text=has_text)
return self._main_frame.locator(selector, has_text=has_text, has=has)

def frame_locator(self, selector: str) -> "FrameLocator":
return self.main_frame.frame_locator(selector)
Expand Down

0 comments on commit 54b4bdf

Please sign in to comment.