From 25c6b6329a14e3dc2e5d765cbc02e1a34430c0d2 Mon Sep 17 00:00:00 2001 From: labkey-tchad Date: Tue, 12 Aug 2025 11:14:02 -0700 Subject: [PATCH] Wait for window to have the expected name --- src/org/labkey/test/WebDriverWrapper.java | 4 +- .../test/util/LabKeyExpectedConditions.java | 100 ++++++------------ 2 files changed, 35 insertions(+), 69 deletions(-) diff --git a/src/org/labkey/test/WebDriverWrapper.java b/src/org/labkey/test/WebDriverWrapper.java index 83bd5dd672..04511d35d9 100644 --- a/src/org/labkey/test/WebDriverWrapper.java +++ b/src/org/labkey/test/WebDriverWrapper.java @@ -167,6 +167,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; @@ -2151,7 +2152,8 @@ public long doAndWaitForWindow(Runnable action, String windowName) action.run(); - getDriver().switchTo().window(windowName); + new WebDriverWait(getDriver(), Duration.ofSeconds(5)).until(windowIsPresent(windowName)); + return targetWindowExists; }); } diff --git a/src/org/labkey/test/util/LabKeyExpectedConditions.java b/src/org/labkey/test/util/LabKeyExpectedConditions.java index fc87aedf33..89a0960d32 100644 --- a/src/org/labkey/test/util/LabKeyExpectedConditions.java +++ b/src/org/labkey/test/util/LabKeyExpectedConditions.java @@ -21,8 +21,8 @@ import org.openqa.selenium.Dimension; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.NoSuchElementException; +import org.openqa.selenium.NoSuchWindowException; import org.openqa.selenium.Point; -import org.openqa.selenium.SearchContext; import org.openqa.selenium.StaleElementReferenceException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; @@ -33,6 +33,7 @@ import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.function.Function; public class LabKeyExpectedConditions @@ -42,32 +43,6 @@ private LabKeyExpectedConditions() // Utility class } - /** - * An expectation for checking child WebElement as a part of parent element to be present - * - * @param context SearchContext to find element within - * @param childLocator used to find child element. For example Locator.xpath("./tr/td") - * @return subelement - */ - public static ExpectedCondition presenceOfNestedElementLocatedBy(final SearchContext context, final By childLocator) - { - return new ExpectedCondition<>() - { - - @Override - public WebElement apply(WebDriver webDriver) - { - return context.findElement(childLocator); - } - - @Override - public String toString() - { - return String.format("visibility of element located by %s", childLocator); - } - }; - } - /** * An expectation for checking that an element has stopped moving * @@ -101,7 +76,7 @@ public String toString() /** * Another expectation for checking that an element has stopped moving * - * @param el the element who's position changes + * @param el the element whose position changes * @return the element when animation is complete */ public static ExpectedCondition animationIsDone(final WebElement el) { @@ -165,7 +140,7 @@ public WebElement apply(WebDriver driver) return null; } - if (el.isEnabled() && !el.getAttribute("class").contains("disabled")) + if (el.isEnabled() && !Objects.requireNonNullElse(el.getAttribute("class"), "").contains("disabled")) return el; else return null; @@ -202,43 +177,7 @@ public Boolean apply(WebDriver ignored) @Override public String toString() { - return staleCheck.toString() + " after clicking"; - } - }; - } - - /** - * Wraps {@link ExpectedConditions#visibilityOfAllElements(WebElement...)} - * This expectations accounts for the behavior of LabKey WebElement wrappers, which will throw if you attempt to - * inspect them before the element has appeared. - * - * @param elements list of WebElements - * @return the list of WebElements once they are located - * @see org.labkey.test.selenium.LazyWebElement - */ - public static ExpectedCondition> visibilityOfAllElements(WebElement... elements) - { - return new ExpectedCondition<>() - { - final ExpectedCondition> wrapped = ExpectedConditions.visibilityOfAllElements(elements); - - @Override - public List apply(WebDriver driver) - { - try - { - return wrapped.apply(driver); - } - catch (StaleElementReferenceException | NoSuchElementException ignore) - { - return null; - } - } - - @Override - public String toString() - { - return wrapped.toString(); + return staleCheck + " after clicking"; } }; } @@ -246,10 +185,10 @@ public String toString() /** * Wraps {@link ExpectedConditions#stalenessOf(WebElement)} * Firefox occasionally throws "NoSuchElementException: Web element reference not seen before" - * for short lived elements. + * for short-lived elements. * * @param element WebElement that should go stale. - * @return false if the element is still attached to the DOM, true otherwise. + * @return ExpectedCondition that returns false if the element is still attached to the DOM, true otherwise. */ public static ExpectedCondition stalenessOf(WebElement element) { @@ -292,6 +231,31 @@ public static ExpectedCondition visibilityOf(WebElement element, boolean visi : ExpectedConditions.invisibilityOf(element); } + /** + * An expectation for checking whether a window or tab with the give name is present. + * If the window/tab is available it switches the given driver to the specified window/tab. + * @param windowName The name of the window + * @return An expected condition that returns the driver with focus switched to the specified window or tab. + * @see WebDriver.TargetLocator#window(String) + */ + public static ExpectedCondition windowIsPresent(final String windowName) { + return new ExpectedCondition<>() { + @Override + public WebDriver apply(WebDriver driver) { + try { + return driver.switchTo().window(windowName); + } catch (NoSuchWindowException e) { + return null; + } + } + + @Override + public String toString() { + return "window named " + windowName; + } + }; + } + /** * Wraps a 'Wait' to terminate after function return a non-null value. * Normally, 'Wait' expects a non-null, non-false return value