/** * First, verify that window.$A has been installed. Then, wait until {@link * #isAuraFrameworkReady()} returns true. We assume the document has finished loading at this * point: callers should have previously called {@link #waitForDocumentReady()}. */ public void waitForAuraFrameworkReady(final Set<String> expectedErrors) { String doNotAssign = "\nThis message means, you aren't even on Aura application at this point. " + "Please do not assign this test failure to Aura team/s unless, Aura team/s is the owner of this test. "; WebDriverWait waitAuraPresent = new WebDriverWait(driver, timeoutInSecs); waitAuraPresent .withMessage("Initialization error: Perhaps the initial GET failed." + doNotAssign) .until( new Function<WebDriver, Boolean>() { @Override public Boolean apply(WebDriver input) { return (Boolean) getRawEval("return !!window.$A"); } }); WebDriverWait waitFinishedInit = new WebDriverWait(driver, timeoutInSecs); waitFinishedInit .ignoring(StaleElementReferenceException.class) .withMessage("Initialization error: $A present but failed to initialize") .until( new Function<WebDriver, Boolean>() { @Override public Boolean apply(WebDriver input) { assertNoAuraErrorMessage(expectedErrors); return isAuraFrameworkReady(); } }); }
/** * Waits for element with matching locator to appear in dom. * * <p>This will wait for at least one element with the locator to appear in the dom, and it will * return the first element found. If there are more than one element that match the locator, this * will succeed when the first one appears. * * @param msg Error message on timeout. * @param locator By of element waiting for. */ public WebElement waitForElement(String msg, By locator) { WaitAndRetrieve war = new WaitAndRetrieve(locator); WebDriverWait wait = new WebDriverWait(driver, timeoutInSecs); wait.withMessage(msg); wait.ignoring(NoSuchElementException.class); wait.until(war); return war.getFound(); }
/** * Waits for element to be not present * * @param locator By of element waiting to disapear * @return */ public boolean waitForElementNotPresent(String msg, final By locator) { WebDriverWait wait = new WebDriverWait(driver, timeoutInSecs); return wait.withMessage(msg) .ignoring(StaleElementReferenceException.class) .until( new ExpectedCondition<Boolean>() { @Override public Boolean apply(WebDriver d) { WebElement element = driver.findElement(locator); return element == null ? true : false; } }); }
/** * Find matching elements in the DOM. * * @param locator * @return */ public List<WebElement> findDomElements(final By locator) { WebDriverWait wait = new WebDriverWait(driver, timeoutInSecs); return wait.withMessage("fail to find element in dom:" + locator.toString()) .ignoring(StaleElementReferenceException.class) .until( new ExpectedCondition<List<WebElement>>() { @Override public List<WebElement> apply(WebDriver d) { List<WebElement> elements = driver.findElements(locator); if (elements.size() > 0 && getBooleanEval( "return arguments[0].ownerDocument === document", elements.get(0))) { return elements; } return null; } }); }
/** * Finds the WebElement identified by locator and applies the provided Function to it, ignoring * StaleElementReferenceException. * * @param locator By locator to find WebElement in the DOM. * @param function Function to run on web * @param message Message to display to user on timeout. * @return */ public <R> R waitForElementFunction( final By locator, final Function<WebElement, R> function, String message) { WebDriverWait wait = new WebDriverWait(driver, timeoutInSecs); return wait.withMessage(message) .until( new ExpectedCondition<R>() { private WebElement element = null; @Override public R apply(WebDriver d) { if (element == null) { element = findDomElement(locator); } try { return function.apply(element); } catch (StaleElementReferenceException e) { element = null; } return null; } }); }
/** * Wait the specified number of seconds until the provided Function returns true or non-null. If * this does not occur, error out with passed in message. Any uncaught javascript errors will * trigger an AssertionFailedError. */ public <V> V waitUntil( Function<? super WebDriver, V> function, long timeoutInSecs, String message) { WebDriverWait wait = new WebDriverWait(driver, timeoutInSecs); return wait.withMessage(message).until(addErrorCheck(function)); }