Skip to content

Commit 1825c13

Browse files
authored
fix: use internal:text* selectors (#1108)
1 parent 48d9528 commit 1825c13

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

playwright/src/main/java/com/microsoft/playwright/impl/LocatorImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public LocatorImpl(FrameImpl frame, String selector, LocatorOptions options) {
2626
this.frame = frame;
2727
if (options != null) {
2828
if (options.hasText != null) {
29-
String textSelector = "text=" + escapeForTextSelector(options.hasText, false);
30-
selector += " >> internal:has=" + gson().toJson(textSelector);
29+
selector += " >> internal:has-text=" + escapeForTextSelector(options.hasText, false);
3130
}
3231
if (options.has != null) {
3332
LocatorImpl locator = (LocatorImpl) options.has;

playwright/src/main/java/com/microsoft/playwright/impl/LocatorUtils.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,9 @@ static void setTestIdAttributeName(String name) {
1414
testIdAttributeName = name;
1515
}
1616

17-
static String getByTextSelector(String text, Locator.GetByTextOptions options) {
17+
static String getByTextSelector(Object text, Locator.GetByTextOptions options) {
1818
boolean exact = options != null && options.exact != null && options.exact;
19-
return "text=" + escapeForTextSelector(text, exact);
20-
}
21-
22-
static String getByTextSelector(Pattern text, Locator.GetByTextOptions options) {
23-
boolean exact = options != null && options.exact != null && options.exact;
24-
return "text=" + escapeForTextSelector(text, exact);
19+
return "internal:text=" + escapeForTextSelector(text, exact);
2520
}
2621

2722
static String getByLabelSelector(Object text, Locator.GetByLabelOptions options) {
@@ -61,7 +56,7 @@ private static void addAttr(StringBuilder result, String name, String value) {
6156

6257
static String getByRoleSelector(AriaRole role, Locator.GetByRoleOptions options) {
6358
StringBuilder result = new StringBuilder();
64-
result.append("role=").append(role.name().toLowerCase());
59+
result.append("internal:role=").append(role.name().toLowerCase());
6560
if (options != null) {
6661
if (options.checked != null)
6762
addAttr(result, "checked", options.checked.toString());
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.microsoft.playwright;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.regex.Pattern;
6+
7+
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
8+
import static java.util.Arrays.asList;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
public class TestSelectorsText extends TestBase {
12+
@Test
13+
void hasTextAndInternalTextShouldMatchFullNodeTextInStrictMode() {
14+
page.setContent("<div id=div1>hello<span>world</span></div>\n" +
15+
" <div id=div2>hello</div>");
16+
assertThat(page.getByText("helloworld", new Page.GetByTextOptions().setExact(true))).hasId("div1");
17+
assertThat(page.getByText("hello", new Page.GetByTextOptions().setExact(true))).hasId("div2");
18+
assertThat(page.locator("div", new Page.LocatorOptions().setHasText(Pattern.compile("^helloworld$")))).hasId("div1");
19+
assertThat(page.locator("div", new Page.LocatorOptions().setHasText(Pattern.compile("^hello$")))).hasId("div2");
20+
21+
page.setContent("<div id=div1><span id=span1>hello</span>world</div>\n" +
22+
" <div id=div2><span id=span2>hello</span></div>");
23+
assertThat(page.getByText("helloworld", new Page.GetByTextOptions().setExact(true))).hasId("div1");
24+
assertEquals(asList("span1", "span2"), page.getByText("hello", new Page.GetByTextOptions().setExact(true)).evaluateAll("els => els.map(e => e.id)"));
25+
assertThat(page.locator("div", new Page.LocatorOptions().setHasText(Pattern.compile("^helloworld$")))).hasId("div1");
26+
assertThat(page.locator("div", new Page.LocatorOptions().setHasText(Pattern.compile("^hello$")))).hasId("div2");
27+
}
28+
}

0 commit comments

Comments
 (0)