diff --git a/src/org/labkey/test/LabKeySiteWrapper.java b/src/org/labkey/test/LabKeySiteWrapper.java index 03f757301c..de4a1135f7 100644 --- a/src/org/labkey/test/LabKeySiteWrapper.java +++ b/src/org/labkey/test/LabKeySiteWrapper.java @@ -431,7 +431,7 @@ public void signInShouldFail(String email, String password, String... expectedMe errorText = getText(Locator.tagWithClass("div", "auth-form-body").childTag("p")); } - List missingErrors = getMissingTexts(new TextSearcher(errorText), expectedMessages); + List missingErrors = new TextSearcher(errorText).getMissingTexts(expectedMessages); assertTrue(String.format("Wrong errors.\nExpected: ['%s']\nActual: '%s'", String.join("',\n'", expectedMessages), errorText), missingErrors.isEmpty()); } diff --git a/src/org/labkey/test/WebDriverWrapper.java b/src/org/labkey/test/WebDriverWrapper.java index fe0077f074..6024c761ba 100644 --- a/src/org/labkey/test/WebDriverWrapper.java +++ b/src/org/labkey/test/WebDriverWrapper.java @@ -18,7 +18,6 @@ import org.apache.commons.collections4.MultiValuedMap; import org.apache.commons.collections4.multimap.HashSetValuedHashMap; import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.mutable.MutableBoolean; import org.apache.commons.lang3.mutable.MutableInt; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; @@ -61,6 +60,7 @@ import org.labkey.test.util.RelativeUrl; import org.labkey.test.util.TestLogger; import org.labkey.test.util.TextSearcher; +import org.labkey.test.util.TextSearcher.TextTransformers; import org.labkey.test.util.Timer; import org.labkey.test.util.selenium.ScrollUtils; import org.labkey.test.util.selenium.WebDriverUtils; @@ -1614,29 +1614,27 @@ public void assertLabKeyErrorPresent() fail("No errors found"); } - public static String encodeText(String unencodedText) + /** + * Check whether all the specified text is present on the page + * @param texts un-encoded text to search for + * @return true if all the specified texts are present on the page + */ + public boolean isTextPresent(String... texts) { - return unencodedText - .replace("&", "&") - .replace("<", "<") - .replace(">", ">"); + return new TextSearcher(this).areAllTextsPresent(texts); } - public boolean isTextPresent(String... texts) + /** + * Check whether the HTML-encoded text is in the page source + * @param htmlFragments encoded html fragments to search for + * @return true if all the specified texts are present on the page + */ + public boolean isHtmlPresent(String... htmlFragments) { - final MutableBoolean present = new MutableBoolean(true); - - TextSearcher.TextHandler handler = (htmlSource, text) -> { - // Not found... stop enumerating and return false - if (htmlSource == null || !htmlSource.contains(text)) - present.setFalse(); - - return present.get(); - }; TextSearcher searcher = new TextSearcher(this); - searcher.searchForTexts(handler, Arrays.asList(texts)); + searcher.setSearchTransformer(TextTransformers.IDENTITY); - return present.get(); + return searcher.areAllTextsPresent(htmlFragments); } public List getTextOrder(TextSearcher searcher, String... texts) @@ -1659,11 +1657,6 @@ public List getTextOrder(TextSearcher searcher, String... texts) return orderedTexts; } - public List getMissingTexts(TextSearcher searcher, String... texts) - { - return searcher.getMissingTexts(Arrays.asList(texts)); - } - public String getText(Locator elementLocator) { WebElement el = elementLocator.findElement(getDriver()); @@ -1692,7 +1685,7 @@ public void assertTextPresent(String... texts) public void assertTextPresent(TextSearcher searcher, String... texts) { - List missingTexts = getMissingTexts(searcher, texts); + List missingTexts = searcher.getMissingTexts(texts); if (!missingTexts.isEmpty()) { @@ -1709,7 +1702,7 @@ public void assertTextPresentCaseInsensitive(String... texts) { TextSearcher searcher = new TextSearcher(this); - searcher.setSearchTransformer((text) -> encodeText(text).toLowerCase()); + searcher.setSearchTransformer(TextTransformers.ENCODE_HTML.andThen(String::toLowerCase)); searcher.setSourceTransformer(String::toLowerCase); @@ -1721,18 +1714,7 @@ public void assertTextPresentCaseInsensitive(String... texts) */ public boolean isAnyTextPresent(String... texts) { - final MutableBoolean found = new MutableBoolean(false); - - TextSearcher.TextHandler handler = (htmlSource, text) -> { - if (htmlSource.contains(text)) - found.setTrue(); - - return !found.get(); // stop searching if any value is found - }; - TextSearcher searcher = new TextSearcher(this); - searcher.searchForTexts(handler, Arrays.asList(texts)); - - return found.get(); + return new TextSearcher(this).isAnyTextPresent(texts); } /** @@ -1892,7 +1874,7 @@ public static void assertTextNotPresent(TextSearcher searcher, String... texts) public void assertTextNotPresent(String... texts) { TextSearcher searcher = new TextSearcher(this); - searcher.setSearchTransformer((text) -> encodeText(text).replace(" ", " ")); + searcher.setSearchTransformer(TextTransformers.ENCODE_HTML.andThen(t -> t.replace(" ", " "))); assertTextNotPresent(searcher, texts); } diff --git a/src/org/labkey/test/tests/announcements/ModeratorReviewTest.java b/src/org/labkey/test/tests/announcements/ModeratorReviewTest.java index d79161b04e..70fdb06842 100644 --- a/src/org/labkey/test/tests/announcements/ModeratorReviewTest.java +++ b/src/org/labkey/test/tests/announcements/ModeratorReviewTest.java @@ -163,7 +163,7 @@ private String addResponse(String user, String title, boolean expectAutoApproved // commonmark-java auto-linking turns all email addresses into mailto: links String formattedResponse = replaceEmailAddressesWithMailToLinks(response); - boolean responseAdded = isTextPresent(formattedResponse); + boolean responseAdded = isHtmlPresent(formattedResponse); if (expectAutoApproved && !responseAdded) { checker().fatal().error(String.format("Expected response '%s' was not present on the thread page.", formattedResponse)); diff --git a/src/org/labkey/test/util/EscapeUtil.java b/src/org/labkey/test/util/EscapeUtil.java index 8e7241219e..ff1a098c6c 100644 --- a/src/org/labkey/test/util/EscapeUtil.java +++ b/src/org/labkey/test/util/EscapeUtil.java @@ -150,4 +150,5 @@ public static String escapeForNameExpression(String name) { return nameExpressionNeedsEscaping.matcher(name).replaceAll("\\\\$1"); } + } diff --git a/src/org/labkey/test/util/TextSearcher.java b/src/org/labkey/test/util/TextSearcher.java index 3ed4a79f06..a36368ba1b 100644 --- a/src/org/labkey/test/util/TextSearcher.java +++ b/src/org/labkey/test/util/TextSearcher.java @@ -15,12 +15,13 @@ */ package org.labkey.test.util; -import org.labkey.test.BaseWebDriverTest; +import org.apache.commons.lang3.mutable.MutableBoolean; import org.labkey.test.TestFileUtils; import org.labkey.test.WebDriverWrapper; import java.io.File; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.function.Function; @@ -98,6 +99,11 @@ public final void searchForTexts(TextHandler textHandler, List texts) } } + public List getMissingTexts(String... texts) + { + return getMissingTexts(Arrays.asList(texts)); + } + public List getMissingTexts(List texts) { final List missingTexts = new ArrayList<>(); @@ -113,6 +119,46 @@ public List getMissingTexts(List texts) return missingTexts; } + /** + * Checks whether any of the texts are present + * @return true if any of the specified text is found + */ + public boolean isAnyTextPresent(String... texts) + { + final MutableBoolean found = new MutableBoolean(false); + + TextSearcher.TextHandler handler = (htmlSource, text) -> { + if (htmlSource.contains(text)) + found.setTrue(); + + return !found.get(); // stop searching if any value is found + }; + searchForTexts(handler, Arrays.asList(texts)); + + return found.get(); + } + + /** + * Checks whether all the specified texts are present + * @return true if all the specified texts are present on the page + */ + public boolean areAllTextsPresent(String... texts) + { + final MutableBoolean present = new MutableBoolean(true); + + TextSearcher.TextHandler handler = (htmlSource, text) -> { + // Not found... stop enumerating and return false + if (htmlSource == null || !htmlSource.contains(text)) + present.setFalse(); + + return present.get(); + }; + + searchForTexts(handler, Arrays.asList(texts)); + + return present.get(); + } + /** * @return source text from the last search attempt */ @@ -129,7 +175,10 @@ public interface TextHandler public static abstract class TextTransformers { - public static final Function ENCODE_HTML = BaseWebDriverTest::encodeText; + public static final Function ENCODE_HTML = t -> t + .replace("&", "&") + .replace("<", "<") + .replace(">", ">"); public static final Function IDENTITY = text -> text; //Inserts spaces between camel-cased words