From 484a255ec76e15ae812e13fd395a806deed4b592 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Tue, 28 Jun 2022 08:43:20 -0700 Subject: [PATCH] feat: support ignoreCase option (#969) --- .../impl/LocatorAssertionsImpl.java | 23 ++++++++++++- .../microsoft/playwright/impl/Protocol.java | 1 + .../playwright/TestLocatorAssertions.java | 32 +++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java index 4f14615f..1106f055 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java @@ -19,6 +19,7 @@ package com.microsoft.playwright.impl; import com.microsoft.playwright.Locator; import com.microsoft.playwright.assertions.LocatorAssertions; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; @@ -39,6 +40,7 @@ public class LocatorAssertionsImpl extends AssertionsBase implements LocatorAsse public void containsText(String text, ContainsTextOptions options) { ExpectedTextValue expected = new ExpectedTextValue(); expected.string = text; + expected.ignoreCase = shouldIgnoreCase(options); expected.matchSubstring = true; expected.normalizeWhiteSpace = true; expectImpl("to.have.text", expected, text, "Locator expected to contain text", convertType(options, FrameExpectOptions.class)); @@ -47,6 +49,7 @@ public class LocatorAssertionsImpl extends AssertionsBase implements LocatorAsse @Override public void containsText(Pattern pattern, ContainsTextOptions options) { ExpectedTextValue expected = expectedRegex(pattern); + expected.ignoreCase = shouldIgnoreCase(options); expected.matchSubstring = true; expected.normalizeWhiteSpace = true; expectImpl("to.have.text", expected, pattern, "Locator expected to contain regex", convertType(options, FrameExpectOptions.class)); @@ -58,6 +61,7 @@ public class LocatorAssertionsImpl extends AssertionsBase implements LocatorAsse for (String text : strings) { ExpectedTextValue expected = new ExpectedTextValue(); expected.string = text; + expected.ignoreCase = shouldIgnoreCase(options); expected.matchSubstring = true; expected.normalizeWhiteSpace = true; list.add(expected); @@ -70,6 +74,7 @@ public class LocatorAssertionsImpl extends AssertionsBase implements LocatorAsse List list = new ArrayList<>(); for (Pattern pattern : patterns) { ExpectedTextValue expected = expectedRegex(pattern); + expected.ignoreCase = shouldIgnoreCase(options); expected.matchSubstring = true; expected.normalizeWhiteSpace = true; list.add(expected); @@ -203,6 +208,7 @@ public class LocatorAssertionsImpl extends AssertionsBase implements LocatorAsse public void hasText(String text, HasTextOptions options) { ExpectedTextValue expected = new ExpectedTextValue(); expected.string = text; + expected.ignoreCase = shouldIgnoreCase(options); expected.matchSubstring = false; expected.normalizeWhiteSpace = true; expectImpl("to.have.text", expected, text, "Locator expected to have text", convertType(options, FrameExpectOptions.class)); @@ -211,6 +217,7 @@ public class LocatorAssertionsImpl extends AssertionsBase implements LocatorAsse @Override public void hasText(Pattern pattern, HasTextOptions options) { ExpectedTextValue expected = expectedRegex(pattern); + expected.ignoreCase = shouldIgnoreCase(options); // Just match substring, same as containsText. expected.matchSubstring = true; expected.normalizeWhiteSpace = true; @@ -223,6 +230,7 @@ public class LocatorAssertionsImpl extends AssertionsBase implements LocatorAsse for (String text : strings) { ExpectedTextValue expected = new ExpectedTextValue(); expected.string = text; + expected.ignoreCase = shouldIgnoreCase(options); expected.matchSubstring = false; expected.normalizeWhiteSpace = true; list.add(expected); @@ -235,6 +243,7 @@ public class LocatorAssertionsImpl extends AssertionsBase implements LocatorAsse List list = new ArrayList<>(); for (Pattern pattern : patterns) { ExpectedTextValue expected = expectedRegex(pattern); + expected.ignoreCase = shouldIgnoreCase(options); expected.matchSubstring = true; expected.normalizeWhiteSpace = true; list.add(expected); @@ -327,5 +336,17 @@ public class LocatorAssertionsImpl extends AssertionsBase implements LocatorAsse public LocatorAssertions not() { return new LocatorAssertionsImpl(actualLocator, !isNot); } -} + private static Boolean shouldIgnoreCase(Object options) { + if (options == null) { + return null; + } + try { + Field fromField = options.getClass().getDeclaredField("ignoreCase"); + Object value = fromField.get(options); + return (Boolean) value; + } catch (NoSuchFieldException | IllegalAccessException e) { + return null; + } + } +} diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/Protocol.java b/playwright/src/main/java/com/microsoft/playwright/impl/Protocol.java index 28a69650..58600b75 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/Protocol.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/Protocol.java @@ -85,6 +85,7 @@ class ExpectedTextValue { String string; String regexSource; String regexFlags; + Boolean ignoreCase; Boolean matchSubstring; Boolean normalizeWhiteSpace; } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java b/playwright/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java index ad2c5906..3a67d0bd 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java @@ -72,6 +72,28 @@ public class TestLocatorAssertions extends TestBase { } } + @Test + void containsTextWTextPass() { + page.setContent("
Text content
"); + Locator locator = page.locator("#node"); + assertThat(locator).containsText("Text"); + // Should normalize whitespace. + assertThat(locator).containsText(" ext cont\n "); + // Should support ignoreCase. + assertThat(locator).containsText("EXT", new LocatorAssertions.ContainsTextOptions().setIgnoreCase(true)); + // Should support falsy ignoreCase. + assertThat(locator).not().containsText("TEXT", new LocatorAssertions.ContainsTextOptions().setIgnoreCase(false)); + } + + @Test + void containsTextWTextArrayPass() { + page.setContent("
Text \n1
Text2
Text3
"); + Locator locator = page.locator("div"); + assertThat(locator).containsText(new String[] {"ext 1", "ext3"}); + // Should support ignoreCase. + assertThat(locator).containsText(new String[] {"EXT 1", "eXt3"}, new LocatorAssertions.ContainsTextOptions().setIgnoreCase(true)); + } + @Test void hasTextWRegexPass() { page.setContent("
Text content
"); @@ -79,6 +101,10 @@ public class TestLocatorAssertions extends TestBase { assertThat(locator).hasText(Pattern.compile("Te.t")); // Should not normalize whitespace. assertThat(locator).hasText(Pattern.compile("Text.+content")); + // Should respect ignoreCase. + assertThat(locator).hasText(Pattern.compile("text content"), new LocatorAssertions.HasTextOptions().setIgnoreCase(true)); + // Should override regex flag with ignoreCase. + assertThat(locator).not().hasText(Pattern.compile("text content", Pattern.CASE_INSENSITIVE), new LocatorAssertions.HasTextOptions().setIgnoreCase(false)); } @Test @@ -101,6 +127,10 @@ public class TestLocatorAssertions extends TestBase { Locator locator = page.locator("#node"); // Should normalize whitespace. assertThat(locator).hasText("Text content"); + // Should support ignoreCase. + assertThat(locator).hasText("text CONTENT", new LocatorAssertions.HasTextOptions().setIgnoreCase(true)); + // Should support falsy ignoreCase. + assertThat(locator).not().hasText("TEXT", new LocatorAssertions.HasTextOptions().setIgnoreCase(false)); } @Test @@ -131,6 +161,8 @@ public class TestLocatorAssertions extends TestBase { Locator locator = page.locator("div"); // Should normalize whitespace. assertThat(locator).hasText(new String[] {"Text 1", "Text 2a"}); + // Should support ignoreCase. + assertThat(locator).hasText(new String[] {"tEXT 1", "TExt 2A"}, new LocatorAssertions.HasTextOptions().setIgnoreCase(true)); } @Test