Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
18 changes: 12 additions & 6 deletions playwright/_impl/_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,20 @@ async def to_contain_class(
expected, str
):
expected_text = to_expected_text_values(expected)
await self._expect_impl(
"to.contain.class.array",
FrameExpectOptions(expectedText=expected_text, timeout=timeout),
expected,
"Locator expected to contain class names",
)
else:
expected_text = to_expected_text_values([expected])
await self._expect_impl(
"to.contain.class",
FrameExpectOptions(expectedText=expected_text, timeout=timeout),
expected,
"Locator expected to contain class",
)
await self._expect_impl(
"to.contain.class",
FrameExpectOptions(expectedText=expected_text, timeout=timeout),
expected,
"Locator expected to contain class",
)

async def not_to_contain_class(
self,
Expand Down
52 changes: 52 additions & 0 deletions playwright/_impl/_js_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import base64
import collections.abc
import datetime
import math
import struct
import traceback
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
Expand Down Expand Up @@ -260,6 +262,56 @@ def parse_value(value: Any, refs: Optional[Dict[int, Any]] = None) -> Any:

if "b" in value:
return value["b"]

if "ta" in value:
encoded_bytes = value["ta"]["b"]
decoded_bytes = base64.b64decode(encoded_bytes)
array_type = value["ta"]["k"]
if array_type == "i8":
word_size = 1
fmt = "b"
elif array_type == "ui8" or array_type == "ui8c":
word_size = 1
fmt = "B"
elif array_type == "i16":
word_size = 2
fmt = "h"
elif array_type == "ui16":
word_size = 2
fmt = "H"
elif array_type == "i32":
word_size = 4
fmt = "i"
elif array_type == "ui32":
word_size = 4
fmt = "I"
elif array_type == "f32":
word_size = 4
fmt = "f"
elif array_type == "f64":
word_size = 8
fmt = "d"
elif array_type == "bi64":
word_size = 8
fmt = "q"
elif array_type == "bui64":
word_size = 8
fmt = "Q"
else:
raise ValueError(f"Unsupported array type: {array_type}")

byte_len = len(decoded_bytes)
if byte_len % word_size != 0:
raise ValueError(
f"Decoded bytes length {byte_len} is not a multiple of word size {word_size}"
)

if byte_len == 0:
return []
array_len = byte_len // word_size
# "<" denotes little-endian
format_string = f"<{array_len}{fmt}"
return list(struct.unpack(format_string, decoded_bytes))
return value


Expand Down
4 changes: 2 additions & 2 deletions tests/async/test_accessibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@


async def test_accessibility_should_work(
page: Page, is_firefox: bool, is_chromium: bool
page: Page, is_firefox: bool, is_chromium: bool, is_webkit: bool
) -> None:
if not is_firefox and not is_chromium and sys.platform == "darwin":
if is_webkit and sys.platform == "darwin":
pytest.skip("Test disabled on WebKit on macOS")
await page.set_content(
"""<head>
Expand Down
28 changes: 21 additions & 7 deletions tests/async/test_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,28 @@ async def test_assertions_locator_to_have_class(page: Page, server: Server) -> N

async def test_assertions_locator_to_contain_class(page: Page, server: Server) -> None:
await page.goto(server.EMPTY_PAGE)
await page.set_content("<div class='foo bar another foobar'>kek</div>")
await expect(page.locator("div.foobar")).to_contain_class("foobar")
await expect(page.locator("div.foobar")).to_contain_class(["another foobar"])
await expect(page.locator("div.foobar")).not_to_contain_class(
"kekstar", timeout=100
await page.set_content("<div class='foo bar baz'></div>")
locator = page.locator("div")
await expect(locator).to_contain_class("")
await expect(locator).to_contain_class("bar")
await expect(locator).to_contain_class("baz bar")
await expect(locator).to_contain_class(" bar foo ")
await expect(locator).not_to_contain_class(
" baz not-matching "
) # Strip whitespace and match individual classes
with pytest.raises(AssertionError) as excinfo:
await expect(locator).to_contain_class("does-not-exist", timeout=100)

assert excinfo.match("Locator expected to contain class 'does-not-exist'")
assert excinfo.match("Actual value: foo bar baz")
assert excinfo.match("LocatorAssertions.to_contain_class with timeout 100ms")

await page.set_content(
'<div class="foo"></div><div class="hello bar"></div><div class="baz"></div>'
)
with pytest.raises(AssertionError):
await expect(page.locator("div.foobar")).to_contain_class("oh-no", timeout=100)
await expect(locator).to_contain_class(["foo", "hello", "baz"])
await expect(locator).not_to_contain_class(["not-there", "hello", "baz"])
await expect(locator).not_to_contain_class(["foo", "hello"])


async def test_assertions_locator_to_have_count(page: Page, server: Server) -> None:
Expand Down
6 changes: 3 additions & 3 deletions tests/async/test_fetch_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,9 @@ async def test_should_follow_max_redirects(
def _handle_request(req: TestServerRequest) -> None:
nonlocal redirect_count
redirect_count += 1
req.setResponseCode(301),
req.setHeader("Location", server.EMPTY_PAGE),
req.finish(),
req.setResponseCode(301)
req.setHeader("Location", server.EMPTY_PAGE)
req.finish()

server.set_route("/empty.html", _handle_request)
request = await playwright.request.new_context(max_redirects=1)
Expand Down
23 changes: 23 additions & 0 deletions tests/async/test_page_evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ async def test_evaluate_transfer_arrays(page: Page) -> None:
assert result == [1, 2, 3]


async def test_evaluate_transfer_typed_arrays(page: Page) -> None:
async def test_typed_array(
typed_array: str, expected: list[float], value_suffix: Optional[str]
) -> None:
value_suffix = "" if value_suffix is None else value_suffix
result = await page.evaluate(
f"() => new {typed_array}([1{value_suffix}, 2{value_suffix}, 3{value_suffix}])"
)
assert result == expected

await test_typed_array("Int8Array", [1, 2, 3], None)
await test_typed_array("Uint8Array", [1, 2, 3], None)
await test_typed_array("Uint8ClampedArray", [1, 2, 3], None)
await test_typed_array("Int16Array", [1, 2, 3], None)
await test_typed_array("Uint16Array", [1, 2, 3], None)
await test_typed_array("Int32Array", [1, 2, 3], None)
await test_typed_array("Uint32Array", [1, 2, 3], None)
await test_typed_array("Float32Array", [1.5, 2.5, 3.5], ".5")
await test_typed_array("Float64Array", [1.5, 2.5, 3.5], ".5")
await test_typed_array("BigInt64Array", [1, 2, 3], "n")
await test_typed_array("BigUint64Array", [1, 2, 3], "n")


async def test_evaluate_transfer_bigint(page: Page) -> None:
assert await page.evaluate("() => 42n") == 42
assert await page.evaluate("a => a", 17) == 17
Expand Down
4 changes: 3 additions & 1 deletion tests/sync/test_accessibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@


def test_accessibility_should_work(
page: Page, is_firefox: bool, is_chromium: bool
page: Page, is_firefox: bool, is_chromium: bool, is_webkit: bool
) -> None:
if is_webkit and sys.platform == "darwin":
pytest.skip("Test disabled on WebKit on macOS")
page.set_content(
"""<head>
<title>Accessibility Test</title>
Expand Down
28 changes: 22 additions & 6 deletions tests/sync/test_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,28 @@ def test_assertions_locator_to_have_class(page: Page, server: Server) -> None:

def test_assertions_locator_to_contain_class(page: Page, server: Server) -> None:
page.goto(server.EMPTY_PAGE)
page.set_content("<div class='foo bar another foobar'>kek</div>")
expect(page.locator("div.foobar")).to_contain_class("foobar")
expect(page.locator("div.foobar")).to_contain_class(["another foobar"])
expect(page.locator("div.foobar")).not_to_contain_class("kekstar", timeout=100)
with pytest.raises(AssertionError):
expect(page.locator("div.foobar")).to_contain_class("oh-no", timeout=100)
page.set_content("<div class='foo bar baz'></div>")
locator = page.locator("div")
expect(locator).to_contain_class("")
expect(locator).to_contain_class("bar")
expect(locator).to_contain_class("baz bar")
expect(locator).to_contain_class(" bar foo ")
expect(locator).not_to_contain_class(
" baz not-matching "
) # Strip whitespace and match individual classes
with pytest.raises(AssertionError) as excinfo:
expect(locator).to_contain_class("does-not-exist", timeout=100)

assert excinfo.match("Locator expected to contain class 'does-not-exist'")
assert excinfo.match("Actual value: foo bar baz")
assert excinfo.match("LocatorAssertions.to_contain_class with timeout 100ms")

page.set_content(
'<div class="foo"></div><div class="hello bar"></div><div class="baz"></div>'
)
expect(locator).to_contain_class(["foo", "hello", "baz"])
expect(locator).not_to_contain_class(["not-there", "hello", "baz"])
expect(locator).not_to_contain_class(["foo", "hello"])


def test_assertions_locator_to_have_count(page: Page, server: Server) -> None:
Expand Down
6 changes: 3 additions & 3 deletions tests/sync/test_fetch_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,9 @@ def test_should_follow_max_redirects(playwright: Playwright, server: Server) ->
def _handle_request(req: TestServerRequest) -> None:
nonlocal redirect_count
redirect_count += 1
req.setResponseCode(301),
req.setHeader("Location", server.EMPTY_PAGE),
req.finish(),
req.setResponseCode(301)
req.setHeader("Location", server.EMPTY_PAGE)
req.finish()

server.set_route("/empty.html", _handle_request)
request = playwright.request.new_context(max_redirects=1)
Expand Down
Loading