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
2 changes: 1 addition & 1 deletion src/org/labkey/test/LabKeySiteWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ public void signInShouldFail(String email, String password, String... expectedMe
errorText = getText(Locator.tagWithClass("div", "auth-form-body").childTag("p"));
}

List<String> missingErrors = getMissingTexts(new TextSearcher(errorText), expectedMessages);
List<String> missingErrors = new TextSearcher(errorText).getMissingTexts(expectedMessages);
assertTrue(String.format("Wrong errors.\nExpected: ['%s']\nActual: '%s'", String.join("',\n'", expectedMessages), errorText), missingErrors.isEmpty());
}

Expand Down
58 changes: 20 additions & 38 deletions src/org/labkey/test/WebDriverWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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("&", "&amp;")
.replace("<", "&lt;")
.replace(">", "&gt;");
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<String> getTextOrder(TextSearcher searcher, String... texts)
Expand All @@ -1659,11 +1657,6 @@ public List<String> getTextOrder(TextSearcher searcher, String... texts)
return orderedTexts;
}

public List<String> getMissingTexts(TextSearcher searcher, String... texts)
{
return searcher.getMissingTexts(Arrays.asList(texts));
}

public String getText(Locator elementLocator)
{
WebElement el = elementLocator.findElement(getDriver());
Expand Down Expand Up @@ -1692,7 +1685,7 @@ public void assertTextPresent(String... texts)

public void assertTextPresent(TextSearcher searcher, String... texts)
{
List<String> missingTexts = getMissingTexts(searcher, texts);
List<String> missingTexts = searcher.getMissingTexts(texts);

if (!missingTexts.isEmpty())
{
Expand All @@ -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);

Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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("&nbsp;", " "));
searcher.setSearchTransformer(TextTransformers.ENCODE_HTML.andThen(t -> t.replace("&nbsp;", " ")));

assertTextNotPresent(searcher, texts);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions src/org/labkey/test/util/EscapeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,5 @@ public static String escapeForNameExpression(String name)
{
return nameExpressionNeedsEscaping.matcher(name).replaceAll("\\\\$1");
}

}
53 changes: 51 additions & 2 deletions src/org/labkey/test/util/TextSearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,6 +99,11 @@ public final void searchForTexts(TextHandler textHandler, List<String> texts)
}
}

public List<String> getMissingTexts(String... texts)
{
return getMissingTexts(Arrays.asList(texts));
}

public List<String> getMissingTexts(List<String> texts)
{
final List<String> missingTexts = new ArrayList<>();
Expand All @@ -113,6 +119,46 @@ public List<String> getMissingTexts(List<String> 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
*/
Expand All @@ -129,7 +175,10 @@ public interface TextHandler

public static abstract class TextTransformers
{
public static final Function<String, String> ENCODE_HTML = BaseWebDriverTest::encodeText;
public static final Function<String, String> ENCODE_HTML = t -> t
.replace("&", "&amp;")
.replace("<", "&lt;")
.replace(">", "&gt;");
public static final Function<String, String> IDENTITY = text -> text;

//Inserts spaces between camel-cased words
Expand Down