Skip to content

Commit 6145aff

Browse files
authored
fix: do not encode unicodes with locator has/has_text (#1452)
1 parent 7d7e4ff commit 6145aff

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

playwright/_impl/_locator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,17 @@ def __init__(
7575
if has_text:
7676
if isinstance(has_text, Pattern):
7777
js_regex = f"/{has_text.pattern}/{escape_regex_flags(has_text)}"
78-
self._selector += f' >> has={json.dumps("text=" + js_regex)}'
78+
self._selector += (
79+
f' >> has={json.dumps("text=" + js_regex, ensure_ascii=False)}'
80+
)
7981
else:
8082
escaped = escape_with_quotes(has_text, '"')
8183
self._selector += f" >> :scope:has-text({escaped})"
8284

8385
if has:
8486
if has._frame != frame:
8587
raise Error('Inner "has" locator must belong to the same frame.')
86-
self._selector += " >> has=" + json.dumps(has._selector)
88+
self._selector += " >> has=" + json.dumps(has._selector, ensure_ascii=False)
8789

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

playwright/_impl/_str_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919

2020
def escape_with_quotes(text: str, char: str = "'") -> str:
21-
stringified = json.dumps(text)
21+
stringified = json.dumps(text, ensure_ascii=False)
2222
escaped_text = stringified[1:-1].replace('\\"', '"')
2323
if char == "'":
2424
return char + escaped_text.replace("'", "\\'") + char

tests/async/test_locators.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,3 +809,16 @@ async def test_should_filter_by_regex_with_special_symbols(page):
809809
await expect(
810810
page.locator("div", has_text=re.compile(r'^first\/".*"second\\$', re.S | re.I))
811811
).to_have_class("test")
812+
813+
814+
async def test_locators_has_does_not_encode_unicode(page: Page, server: Server):
815+
await page.goto(server.EMPTY_PAGE)
816+
locators = [
817+
page.locator("button", has_text="Драматург"),
818+
page.locator("button", has_text=re.compile("Драматург")),
819+
page.locator("button", has=page.locator("text=Драматург")),
820+
]
821+
for locator in locators:
822+
with pytest.raises(Error) as exc_info:
823+
await locator.click(timeout=1_000)
824+
assert "Драматург" in exc_info.value.message

tests/sync/test_locators.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,3 +711,16 @@ def test_should_support_locator_filter(page: Page) -> None:
711711
has_text="world",
712712
)
713713
).to_have_count(1)
714+
715+
716+
def test_locators_has_does_not_encode_unicode(page: Page, server: Server) -> None:
717+
page.goto(server.EMPTY_PAGE)
718+
locators = [
719+
page.locator("button", has_text="Драматург"),
720+
page.locator("button", has_text=re.compile("Драматург")),
721+
page.locator("button", has=page.locator("text=Драматург")),
722+
]
723+
for locator in locators:
724+
with pytest.raises(Error) as exc_info:
725+
locator.click(timeout=1_000)
726+
assert "Драматург" in exc_info.value.message

0 commit comments

Comments
 (0)