List of usage examples for org.openqa.selenium.support.ui Wait until
<T> T until(Function<? super F, T> isTrue);
From source file:daveayan.gherkinsalad.components.core.BaseBrowserElement.java
License:Open Source License
public Element findElement(final By by) { if (by == null) { error("Cannot find element on the page with a null element locator"); return NullElement.newInstance(element_locator); }/*from ww w .ja v a2 s . c o m*/ if (browser.driver() instanceof NullWebDriver) { throw new AssertionError("Cannot find any element '" + by + "' on a NullWebDriver"); } Wait<WebDriver> wait = new FluentWait<WebDriver>(browser.driver()) .withTimeout(Config.seconds_timeout, TimeUnit.SECONDS) .pollingEvery(Config.seconds_poll_interval, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); WebElement _webElement; try { _webElement = wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(by); } }); } catch (TimeoutException toe) { return NullElement.newInstance(element_locator); } return Element.newInstance(_webElement, name(), by); }
From source file:daveayan.gherkinsalad.components.core.BaseBrowserElement.java
License:Open Source License
public Elements findElements(final By by) { if (by == null) { error("Cannot find element on the page with a null element locator"); return Elements.nullInstance(); }/*from w ww . j a v a2s . com*/ if (browser.driver() instanceof NullWebDriver) { throw new AssertionError("Cannot find any element '" + by + "' on a NullWebDriver"); } Wait<WebDriver> wait = new FluentWait<WebDriver>(browser.driver()) .withTimeout(Config.seconds_timeout, TimeUnit.SECONDS) .pollingEvery(Config.seconds_poll_interval, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); List<WebElement> _webElements; try { _webElements = wait.until(new Function<WebDriver, List<WebElement>>() { public List<WebElement> apply(WebDriver driver) { return driver.findElements(by); } }); } catch (TimeoutException toe) { return Elements.nullInstance(); } Elements elements = convertToElements(_webElements, by); return elements; }
From source file:io.appium.java_client.appium.AppiumFluentWaitTest.java
License:Apache License
@Test(expected = TimeoutException.class) public void testDefaultStrategy() { final FakeElement el = new FakeElement(); final Wait<FakeElement> wait = new AppiumFluentWait<>(el, Clock.systemDefaultZone(), duration -> { assertThat(duration.getSeconds(), is(equalTo(1L))); Thread.sleep(duration.toMillis()); }).withPollingStrategy(AppiumFluentWait.IterationInfo::getInterval).withTimeout(ofSeconds(3)) .pollingEvery(ofSeconds(1)); wait.until(FakeElement::isDisplayed); Assert.fail("TimeoutException is expected"); }
From source file:io.appium.java_client.appium.AppiumFluentWaitTest.java
License:Apache License
@Test public void testCustomStrategyOverridesDefaultInterval() { final FakeElement el = new FakeElement(); final AtomicInteger callsCounter = new AtomicInteger(0); final Wait<FakeElement> wait = new AppiumFluentWait<>(el, Clock.systemDefaultZone(), duration -> { callsCounter.incrementAndGet();/*from w ww. j a v a2 s.com*/ assertThat(duration.getSeconds(), is(equalTo(2L))); Thread.sleep(duration.toMillis()); }).withPollingStrategy(info -> ofSeconds(2)).withTimeout(ofSeconds(3)).pollingEvery(ofSeconds(1)); try { wait.until(FakeElement::isDisplayed); Assert.fail("TimeoutException is expected"); } catch (TimeoutException e) { // this is expected assertThat(callsCounter.get(), is(equalTo(2))); } }
From source file:io.appium.java_client.appium.AppiumFluentWaitTest.java
License:Apache License
@Test public void testIntervalCalculationForCustomStrategy() { final FakeElement el = new FakeElement(); final AtomicInteger callsCounter = new AtomicInteger(0); // Linear dependency final Function<Long, Long> pollingStrategy = x -> x * 2; final Wait<FakeElement> wait = new AppiumFluentWait<>(el, Clock.systemDefaultZone(), duration -> { int callNumber = callsCounter.incrementAndGet(); assertThat(duration.getSeconds(), is(equalTo(pollingStrategy.apply((long) callNumber)))); Thread.sleep(duration.toMillis()); }).withPollingStrategy(info -> ofSeconds(pollingStrategy.apply(info.getNumber()))).withTimeout(ofSeconds(4)) .pollingEvery(ofSeconds(1)); try {//from w ww . j av a 2s .co m wait.until(FakeElement::isDisplayed); Assert.fail("TimeoutException is expected"); } catch (TimeoutException e) { // this is expected assertThat(callsCounter.get(), is(equalTo(2))); } }
From source file:org.apache.zeppelin.AbstractZeppelinIT.java
License:Apache License
protected WebElement pollingWait(final By locator, final long timeWait) { Wait<WebDriver> wait = new FluentWait<>(driver).withTimeout(timeWait, TimeUnit.SECONDS) .pollingEvery(1, TimeUnit.SECONDS).ignoring(NoSuchElementException.class); return wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(locator); }/* ww w. j ava 2 s . c o m*/ }); }
From source file:org.bigtester.ate.model.page.elementfind.AbstractElementFind.java
License:Apache License
/** * Find through frames./*from w ww .ja v a2s.c o m*/ * * @param win * the win * @param winFrame * the win frame * @param wait * the wait * @param findByValue * the find by value * @return the web element */ @Nullable protected WebElement findThroughFrames(BrowserWindow win, WindowFrame winFrame, Wait<WebDriver> wait, final By findByValue) { win.getCurrentElementFindFrameChain().add(winFrame); winFrame.obtainFrameFocus(); WebElement retValWE = null;// NOPMD try { retValWE = wait.until( // NOPMD new Function<WebDriver, WebElement>() { public @Nullable WebElement apply( // NOPMD @Nullable WebDriver driver) { if (null == driver) { throw new IllegalStateException("webdriver is not correctly populated."); } else { List<WebElement> allElements = driver.findElements(findByValue); if (allElements.size() == 0) throw new NoSuchElementException(findByValue.toString()); WebElement retVal; int intIndex = getIndexOfSameElementsInt(); if (intIndex < -1) { retVal = allElements.get(0); } else if (intIndex == -1) { retVal = allElements.get(allElements.size() - 1); } else { retVal = allElements.get(intIndex); } return retVal; // return driver.findElement(findByValue); } } }); if (null != retValWE) { if (!win.getLastSuccessElementFindFrameChain().equals(win.getCurrentElementFindFrameChain())) { win.getLastSuccessElementFindFrameChain().clear(); win.getLastSuccessElementFindFrameChain().addAll(win.getCurrentElementFindFrameChain()); } } } catch (NoSuchElementException | TimeoutException error) { List<WindowFrame> childFrames = winFrame.getChildFrames(); for (WindowFrame gChildF : childFrames) { if (null == gChildF) throw GlobalUtils.createInternalError("java arraylist", error); retValWE = findThroughFrames(win, gChildF, wait, findByValue); if (null != retValWE) { break; } } } if (null == retValWE) { if (winFrame.getParentFrame() == null) { winFrame.focusDefautContent(); } else { winFrame.focusParentFrame(); } } return retValWE; }
From source file:org.callimachusproject.webdriver.helpers.WebBrowserDriver.java
License:Apache License
public void clickHiddenLink(final String cssSelector) { Wait<WebDriver> wait = new WebDriverWait(driver, 120); Boolean present = wait.until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver wd) { String js = "return document.querySelector(arguments[0]) && true || false"; try { return (Boolean) driver.executeScript(js, cssSelector); } catch (WebDriverException e) { return null; }/* ww w . j a va 2s . c o m*/ } public String toString() { return "hidden " + cssSelector + " to load"; } }); assertTrue(present); driver.executeScript("document.querySelector(arguments[0]).click()", cssSelector); }
From source file:org.callimachusproject.webdriver.helpers.WebBrowserDriver.java
License:Apache License
public void waitUntilTextPresent(final By locator, final String needle) { waitForScript();/* w w w. ja va 2 s. c om*/ try { driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); Wait<WebDriver> wait = new WebDriverWait(driver, 60); Boolean present = wait.until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver driver) { for (WebElement element : driver.findElements(locator)) { try { if (element.getText().contains(needle)) { return true; } } catch (StaleElementReferenceException e) { continue; } } return null; } public String toString() { return "text " + needle + " to be present in " + locator; } }); assertTrue(present); } finally { driver.manage().timeouts().implicitlyWait(IMPLICITLY_WAIT, TimeUnit.SECONDS); } }
From source file:org.callimachusproject.webdriver.helpers.WebBrowserDriver.java
License:Apache License
public void waitForScript() { Wait<WebDriver> wait = new WebDriverWait(driver, 240); assertTrue(wait.until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver wd) { String js = "try {\n" + "if (document.documentElement)\n" + " return document.documentElement.className;\n" + "else if (document.body)\n" + " return '';\n" + "} catch(e) {}\n" + " return 'wait';"; try { Object className = driver.executeScript(js); return !String.valueOf(className).contains("wait"); } catch (WebDriverException e) { return null; }/* www . j a va2 s.c om*/ } public String toString() { return "script to be ready"; } })); }