Skip to content

Commit

Permalink
fix: do not encode unicodes with locator has/has_text (#1452)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Jul 26, 2022
1 parent 7d7e4ff commit 6145aff
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
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
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

0 comments on commit 6145aff

Please sign in to comment.