Skip to content

Commit e42d7bd

Browse files
authored
cherry-pick(#969): feat: support ignoreCase option (#970)
1 parent 3f60144 commit e42d7bd

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.microsoft.playwright.Locator;
2020
import com.microsoft.playwright.assertions.LocatorAssertions;
2121

22+
import java.lang.reflect.Field;
2223
import java.util.ArrayList;
2324
import java.util.List;
2425
import java.util.regex.Pattern;
@@ -39,6 +40,7 @@ private LocatorAssertionsImpl(Locator locator, boolean isNot) {
3940
public void containsText(String text, ContainsTextOptions options) {
4041
ExpectedTextValue expected = new ExpectedTextValue();
4142
expected.string = text;
43+
expected.ignoreCase = shouldIgnoreCase(options);
4244
expected.matchSubstring = true;
4345
expected.normalizeWhiteSpace = true;
4446
expectImpl("to.have.text", expected, text, "Locator expected to contain text", convertType(options, FrameExpectOptions.class));
@@ -47,6 +49,7 @@ public void containsText(String text, ContainsTextOptions options) {
4749
@Override
4850
public void containsText(Pattern pattern, ContainsTextOptions options) {
4951
ExpectedTextValue expected = expectedRegex(pattern);
52+
expected.ignoreCase = shouldIgnoreCase(options);
5053
expected.matchSubstring = true;
5154
expected.normalizeWhiteSpace = true;
5255
expectImpl("to.have.text", expected, pattern, "Locator expected to contain regex", convertType(options, FrameExpectOptions.class));
@@ -58,6 +61,7 @@ public void containsText(String[] strings, ContainsTextOptions options) {
5861
for (String text : strings) {
5962
ExpectedTextValue expected = new ExpectedTextValue();
6063
expected.string = text;
64+
expected.ignoreCase = shouldIgnoreCase(options);
6165
expected.matchSubstring = true;
6266
expected.normalizeWhiteSpace = true;
6367
list.add(expected);
@@ -70,6 +74,7 @@ public void containsText(Pattern[] patterns, ContainsTextOptions options) {
7074
List<ExpectedTextValue> list = new ArrayList<>();
7175
for (Pattern pattern : patterns) {
7276
ExpectedTextValue expected = expectedRegex(pattern);
77+
expected.ignoreCase = shouldIgnoreCase(options);
7378
expected.matchSubstring = true;
7479
expected.normalizeWhiteSpace = true;
7580
list.add(expected);
@@ -203,6 +208,7 @@ public void hasJSProperty(String name, Object value, HasJSPropertyOptions option
203208
public void hasText(String text, HasTextOptions options) {
204209
ExpectedTextValue expected = new ExpectedTextValue();
205210
expected.string = text;
211+
expected.ignoreCase = shouldIgnoreCase(options);
206212
expected.matchSubstring = false;
207213
expected.normalizeWhiteSpace = true;
208214
expectImpl("to.have.text", expected, text, "Locator expected to have text", convertType(options, FrameExpectOptions.class));
@@ -211,6 +217,7 @@ public void hasText(String text, HasTextOptions options) {
211217
@Override
212218
public void hasText(Pattern pattern, HasTextOptions options) {
213219
ExpectedTextValue expected = expectedRegex(pattern);
220+
expected.ignoreCase = shouldIgnoreCase(options);
214221
// Just match substring, same as containsText.
215222
expected.matchSubstring = true;
216223
expected.normalizeWhiteSpace = true;
@@ -223,6 +230,7 @@ public void hasText(String[] strings, HasTextOptions options) {
223230
for (String text : strings) {
224231
ExpectedTextValue expected = new ExpectedTextValue();
225232
expected.string = text;
233+
expected.ignoreCase = shouldIgnoreCase(options);
226234
expected.matchSubstring = false;
227235
expected.normalizeWhiteSpace = true;
228236
list.add(expected);
@@ -235,6 +243,7 @@ public void hasText(Pattern[] patterns, HasTextOptions options) {
235243
List<ExpectedTextValue> list = new ArrayList<>();
236244
for (Pattern pattern : patterns) {
237245
ExpectedTextValue expected = expectedRegex(pattern);
246+
expected.ignoreCase = shouldIgnoreCase(options);
238247
expected.matchSubstring = true;
239248
expected.normalizeWhiteSpace = true;
240249
list.add(expected);
@@ -327,5 +336,17 @@ private void expectTrue(String expression, String message, FrameExpectOptions op
327336
public LocatorAssertions not() {
328337
return new LocatorAssertionsImpl(actualLocator, !isNot);
329338
}
330-
}
331339

340+
private static Boolean shouldIgnoreCase(Object options) {
341+
if (options == null) {
342+
return null;
343+
}
344+
try {
345+
Field fromField = options.getClass().getDeclaredField("ignoreCase");
346+
Object value = fromField.get(options);
347+
return (Boolean) value;
348+
} catch (NoSuchFieldException | IllegalAccessException e) {
349+
return null;
350+
}
351+
}
352+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class ExpectedTextValue {
8585
String string;
8686
String regexSource;
8787
String regexFlags;
88+
Boolean ignoreCase;
8889
Boolean matchSubstring;
8990
Boolean normalizeWhiteSpace;
9091
}

playwright/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,39 @@ void containsTextWRegexFail() {
7272
}
7373
}
7474

75+
@Test
76+
void containsTextWTextPass() {
77+
page.setContent("<div id=node>Text content</div>");
78+
Locator locator = page.locator("#node");
79+
assertThat(locator).containsText("Text");
80+
// Should normalize whitespace.
81+
assertThat(locator).containsText(" ext cont\n ");
82+
// Should support ignoreCase.
83+
assertThat(locator).containsText("EXT", new LocatorAssertions.ContainsTextOptions().setIgnoreCase(true));
84+
// Should support falsy ignoreCase.
85+
assertThat(locator).not().containsText("TEXT", new LocatorAssertions.ContainsTextOptions().setIgnoreCase(false));
86+
}
87+
88+
@Test
89+
void containsTextWTextArrayPass() {
90+
page.setContent("<div>Text \n1</div><div>Text2</div><div>Text3</div>");
91+
Locator locator = page.locator("div");
92+
assertThat(locator).containsText(new String[] {"ext 1", "ext3"});
93+
// Should support ignoreCase.
94+
assertThat(locator).containsText(new String[] {"EXT 1", "eXt3"}, new LocatorAssertions.ContainsTextOptions().setIgnoreCase(true));
95+
}
96+
7597
@Test
7698
void hasTextWRegexPass() {
7799
page.setContent("<div id=node>Text content</div>");
78100
Locator locator = page.locator("#node");
79101
assertThat(locator).hasText(Pattern.compile("Te.t"));
80102
// Should not normalize whitespace.
81103
assertThat(locator).hasText(Pattern.compile("Text.+content"));
104+
// Should respect ignoreCase.
105+
assertThat(locator).hasText(Pattern.compile("text content"), new LocatorAssertions.HasTextOptions().setIgnoreCase(true));
106+
// Should override regex flag with ignoreCase.
107+
assertThat(locator).not().hasText(Pattern.compile("text content", Pattern.CASE_INSENSITIVE), new LocatorAssertions.HasTextOptions().setIgnoreCase(false));
82108
}
83109

84110
@Test
@@ -101,6 +127,10 @@ void hasTextWTextPass() {
101127
Locator locator = page.locator("#node");
102128
// Should normalize whitespace.
103129
assertThat(locator).hasText("Text content");
130+
// Should support ignoreCase.
131+
assertThat(locator).hasText("text CONTENT", new LocatorAssertions.HasTextOptions().setIgnoreCase(true));
132+
// Should support falsy ignoreCase.
133+
assertThat(locator).not().hasText("TEXT", new LocatorAssertions.HasTextOptions().setIgnoreCase(false));
104134
}
105135

106136
@Test
@@ -131,6 +161,8 @@ void hasTextWTextArrayPass() {
131161
Locator locator = page.locator("div");
132162
// Should normalize whitespace.
133163
assertThat(locator).hasText(new String[] {"Text 1", "Text 2a"});
164+
// Should support ignoreCase.
165+
assertThat(locator).hasText(new String[] {"tEXT 1", "TExt 2A"}, new LocatorAssertions.HasTextOptions().setIgnoreCase(true));
134166
}
135167

136168
@Test

0 commit comments

Comments
 (0)