Skip to content
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
91 changes: 40 additions & 51 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 @@ -62,6 +61,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 @@ -149,6 +149,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -168,6 +169,7 @@
import static org.labkey.test.TestProperties.isWebDriverLoggingEnabled;
import static org.labkey.test.WebTestHelper.makeRelativeUrl;
import static org.labkey.test.components.html.RadioButton.RadioButton;
import static org.labkey.test.util.LabKeyExpectedConditions.windowIsPresent;
import static org.openqa.selenium.chrome.ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY;
import static org.openqa.selenium.chrome.ChromeDriverService.CHROME_DRIVER_VERBOSE_LOG_PROPERTY;
import static org.openqa.selenium.firefox.GeckoDriverService.GECKO_DRIVER_LOG_PROPERTY;
Expand Down Expand Up @@ -1621,29 +1623,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 @@ -1666,11 +1666,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 @@ -1699,7 +1694,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 @@ -1716,7 +1711,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 @@ -1728,18 +1723,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 @@ -1899,7 +1883,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 Expand Up @@ -2141,24 +2125,29 @@ public long doAndMaybeWaitForPageToLoad(int msWait, Supplier<Boolean> action)

public long doAndWaitForWindow(Runnable action, String windowName)
{
String initialWindow = getDriver().getWindowHandle();
AtomicBoolean targetWindowExists = new AtomicBoolean(false);
try
{
getDriver().switchTo().window(windowName);
targetWindowExists.set(true);
}
catch (NoSuchWindowException e)
{
targetWindowExists.set(false);
}

// Call doAndMaybeWaitForPageToLoad with target window in focus (if present)
// Then it will correctly detect the page load in that window
return doAndMaybeWaitForPageToLoad(10_000, () -> {
String initialWindow = getDriver().getWindowHandle();
boolean targetWindowExists;
try
{
getDriver().switchTo().window(windowName);
if (targetWindowExists.get())
getDriver().switchTo().window(initialWindow);
targetWindowExists = true;
}
catch (NoSuchWindowException e)
{
targetWindowExists = false;
}

action.run();

getDriver().switchTo().window(windowName);
return targetWindowExists;
new WebDriverWait(getDriver(), Duration.ofSeconds(5)).until(windowIsPresent(windowName));

return targetWindowExists.get();
});
}

Expand Down
46 changes: 45 additions & 1 deletion src/org/labkey/test/pages/reports/ManageViewsPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@

import org.labkey.test.Locator;
import org.labkey.test.components.ChartQueryDialog;
import org.labkey.test.components.ext4.Window;
import org.labkey.test.components.html.BootstrapMenu;
import org.labkey.test.pages.LabKeyPage;
import org.labkey.test.util.LogMethod;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

import static org.labkey.test.components.ext4.Window.Window;

Expand All @@ -34,7 +40,7 @@ public ManageViewsPage(WebDriver driver)

public void clickAddReport(String reportType)
{
BootstrapMenu.find(getDriver(),"Add Report").clickSubMenu(true,reportType);
new BootstrapMenu.BootstrapMenuFinder(getDriver()).withButtonTextContaining("Add Report").find().clickSubMenu(true,reportType);
}

public ChartQueryDialog clickAddChart()
Expand Down Expand Up @@ -89,4 +95,42 @@ public void viewReport(String reportName)
mouseOver(reportLink);
clickAndWait(reportLink);
}

public void createCategory(String categoryName)
{
createCategories(categoryName);
}

public void createCategories(String... categoryNames)
{
Locator.linkWithText("Manage Categories").findElement(getDriver()).click();
Window<?> categoryWindow = new Window.WindowFinder(getDriver()).withTitle("Manage Categories").waitFor();
for (String categoryName : categoryNames)
addCategory(categoryName, categoryWindow);
clickButton("Done", 0);
categoryWindow.waitForClose();
}

private void addCategory(String categoryName, Window<?> categoryWindow)
{
int attempt = 0;
while (true)
{
categoryWindow.clickButton("New Category", 0);
WebElement newCategoryField = Locator.input("label").withAttributeContaining("id", "textfield").notHidden().waitForElement(categoryWindow, WAIT_FOR_JAVASCRIPT);
setFormElementJS(newCategoryField, categoryName);
fireEvent(newCategoryField, SeleniumEvent.blur);
new WebDriverWait(getDriver(), Duration.ofSeconds(2)).until(ExpectedConditions.invisibilityOf(newCategoryField));
try
{
Locator.tagWithText("div", categoryName).waitForElement(categoryWindow, 2_000);
break;
}
catch (NoSuchElementException e)
{
if (++attempt >= 3)
throw e;
}
}
}
}
Loading