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
6 changes: 4 additions & 2 deletions playwright/_impl/_locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,17 @@ def __init__(
if has_text:
if isinstance(has_text, Pattern):
js_regex = f"/{has_text.pattern}/{escape_regex_flags(has_text)}"
self._selector += f' >> has={json.dumps("text=" + js_regex)}'
self._selector += (
f' >> has={json.dumps("text=" + js_regex, ensure_ascii=False)}'
)
else:
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)
self._selector += " >> has=" + json.dumps(has._selector, ensure_ascii=False)

def __repr__(self) -> str:
return f"<Locator frame={self._frame!r} selector={self._selector!r}>"
Expand Down
2 changes: 1 addition & 1 deletion playwright/_impl/_str_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


def escape_with_quotes(text: str, char: str = "'") -> str:
stringified = json.dumps(text)
stringified = json.dumps(text, ensure_ascii=False)
escaped_text = stringified[1:-1].replace('\\"', '"')
if char == "'":
return char + escaped_text.replace("'", "\\'") + char
Expand Down
13 changes: 13 additions & 0 deletions tests/async/test_locators.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,3 +809,16 @@ async def test_should_filter_by_regex_with_special_symbols(page):
await expect(
page.locator("div", has_text=re.compile(r'^first\/".*"second\\$', re.S | re.I))
).to_have_class("test")


async def test_locators_has_does_not_encode_unicode(page: Page, server: Server):
await page.goto(server.EMPTY_PAGE)
locators = [
page.locator("button", has_text="Драматург"),
page.locator("button", has_text=re.compile("Драматург")),
page.locator("button", has=page.locator("text=Драматург")),
]
for locator in locators:
with pytest.raises(Error) as exc_info:
await locator.click(timeout=1_000)
assert "Драматург" in exc_info.value.message
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you run this test before the change and made sure it failed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

13 changes: 13 additions & 0 deletions tests/sync/test_locators.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,3 +711,16 @@ def test_should_support_locator_filter(page: Page) -> None:
has_text="world",
)
).to_have_count(1)


def test_locators_has_does_not_encode_unicode(page: Page, server: Server) -> None:
page.goto(server.EMPTY_PAGE)
locators = [
page.locator("button", has_text="Драматург"),
page.locator("button", has_text=re.compile("Драматург")),
page.locator("button", has=page.locator("text=Драматург")),
]
for locator in locators:
with pytest.raises(Error) as exc_info:
locator.click(timeout=1_000)
assert "Драматург" in exc_info.value.message