Taking nilesh, take it a step further, you can also enable finely tuned search queries (for example, in the context of WebElement) using the SearchContext interface:
Function<SearchContext, WebElement> elementLocated(final By by) { return new Function<SearchContext, WebElement>() { public WebElement apply(SearchContext context) { return context.findElement(by); } }; }
Execution is performed using the FluentWait <SearchContext> instance (instead of WebDriverWait). Provide yourself with a good programming interface, wrapping up its execution and the necessary exception handling in the utility (the root of your PageObject type hierarchy is a good place):
protected WebElement findElement(SearchContext context, By by, long timeoutSeconds, long sleepMilliseconds) { @SuppressWarnings("unchecked") FluentWait<SearchContext> wait = new FluentWait<SearchContext>(context) .withTimeout(timeoutSeconds, TimeUnit.SECONDS) .pollingEvery(sleepMilliseconds, TimeUnit.MILLISECONDS) .ignoring(NotFoundException.class); WebElement element = null; try { element = wait.until(elementLocated(by)); } catch (TimeoutException te) { element = null; } return element; } protected WebElement findElement(SearchContext context, By by) { return findElement(context, by, DEFAULT_TIMEOUT, DEFAULT_POLL_SLEEP); } static long DEFAULT_TIMEOUT = 3;
Usage example:
WebElement div = this.findElement(driver, By.id("resultsContainer")); if (div != null) { asyncSubmit.click(); WebElement results = this.findElement(div, By.id("results"), 30, 500); if (results == null) {
Joe Coder Jun 30 2018-11-11T00: 00Z
source share