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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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("<div id=div1>hello<span>world</span></div>\n" +
" <div id=div2>hello</div>");
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("<div id=div1><span id=span1>hello</span>world</div>\n" +
" <div id=div2><span id=span2>hello</span></div>");
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");
}
}