diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/LocatorImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/LocatorImpl.java index 82b790319..c1d23c8b9 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/LocatorImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/LocatorImpl.java @@ -26,8 +26,7 @@ public LocatorImpl(FrameImpl frame, String selector, LocatorOptions options) { this.frame = frame; if (options != null) { if (options.hasText != null) { - String textSelector = "text=" + escapeForTextSelector(options.hasText, false); - selector += " >> internal:has=" + gson().toJson(textSelector); + selector += " >> internal:has-text=" + escapeForTextSelector(options.hasText, false); } if (options.has != null) { LocatorImpl locator = (LocatorImpl) options.has; diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/LocatorUtils.java b/playwright/src/main/java/com/microsoft/playwright/impl/LocatorUtils.java index c351886a1..1487f6266 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/LocatorUtils.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/LocatorUtils.java @@ -14,14 +14,9 @@ static void setTestIdAttributeName(String name) { testIdAttributeName = name; } - static String getByTextSelector(String text, Locator.GetByTextOptions options) { + static String getByTextSelector(Object text, Locator.GetByTextOptions options) { boolean exact = options != null && options.exact != null && options.exact; - return "text=" + escapeForTextSelector(text, exact); - } - - static String getByTextSelector(Pattern text, Locator.GetByTextOptions options) { - boolean exact = options != null && options.exact != null && options.exact; - return "text=" + escapeForTextSelector(text, exact); + return "internal:text=" + escapeForTextSelector(text, exact); } static String getByLabelSelector(Object text, Locator.GetByLabelOptions options) { @@ -61,7 +56,7 @@ private static void addAttr(StringBuilder result, String name, String value) { static String getByRoleSelector(AriaRole role, Locator.GetByRoleOptions options) { StringBuilder result = new StringBuilder(); - result.append("role=").append(role.name().toLowerCase()); + result.append("internal:role=").append(role.name().toLowerCase()); if (options != null) { if (options.checked != null) addAttr(result, "checked", options.checked.toString()); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestSelectorsText.java b/playwright/src/test/java/com/microsoft/playwright/TestSelectorsText.java new file mode 100644 index 000000000..328954d5b --- /dev/null +++ b/playwright/src/test/java/com/microsoft/playwright/TestSelectorsText.java @@ -0,0 +1,28 @@ +package com.microsoft.playwright; + +import org.junit.jupiter.api.Test; + +import java.util.regex.Pattern; + +import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; +import static java.util.Arrays.asList; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestSelectorsText extends TestBase { + @Test + void hasTextAndInternalTextShouldMatchFullNodeTextInStrictMode() { + page.setContent("
helloworld
\n" + + "
hello
"); + assertThat(page.getByText("helloworld", new Page.GetByTextOptions().setExact(true))).hasId("div1"); + assertThat(page.getByText("hello", new Page.GetByTextOptions().setExact(true))).hasId("div2"); + assertThat(page.locator("div", new Page.LocatorOptions().setHasText(Pattern.compile("^helloworld$")))).hasId("div1"); + assertThat(page.locator("div", new Page.LocatorOptions().setHasText(Pattern.compile("^hello$")))).hasId("div2"); + + page.setContent("
helloworld
\n" + + "
hello
"); + assertThat(page.getByText("helloworld", new Page.GetByTextOptions().setExact(true))).hasId("div1"); + assertEquals(asList("span1", "span2"), page.getByText("hello", new Page.GetByTextOptions().setExact(true)).evaluateAll("els => els.map(e => e.id)")); + assertThat(page.locator("div", new Page.LocatorOptions().setHasText(Pattern.compile("^helloworld$")))).hasId("div1"); + assertThat(page.locator("div", new Page.LocatorOptions().setHasText(Pattern.compile("^hello$")))).hasId("div2"); + } +}