List of usage examples for org.openqa.selenium.support.ui Wait until
<T> T until(Function<? super F, T> isTrue);
From source file:org.nuxeo.functionaltests.Locator.java
License:Apache License
/** * Fluent wait for an element not to be present, checking every 100 ms. * * @since 5.7.2//from w ww.j a v a 2s. c o m */ public static void waitUntilElementNotPresent(final By locator) { Wait<WebDriver> wait = getFluentWait(); wait.until((new Function<WebDriver, By>() { @Override public By apply(WebDriver driver) { try { driver.findElement(locator); } catch (NoSuchElementException ex) { // ok return locator; } return null; } })); }
From source file:org.nuxeo.functionaltests.pages.tabs.RelationTabSubPage.java
License:Open Source License
public RelationTabSubPage initRelationSetUp() { addANewRelationLink.click();/*from w ww . j a v a 2 s . c o m*/ Function<WebDriver, Boolean> createRelationFormVisible = new Function<WebDriver, Boolean>() { public Boolean apply(WebDriver driver) { return createRelationForm != null; } }; Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(CREATE_FORM_LOADING_TIMEOUT, TimeUnit.SECONDS).pollingEvery(100, TimeUnit.MILLISECONDS) .ignoring(NoSuchElementException.class); wait.until(createRelationFormVisible); return asPage(RelationTabSubPage.class); }
From source file:org.nuxeo.functionaltests.pages.tabs.RelationTabSubPage.java
License:Open Source License
public RelationTabSubPage setRelationWithDocument(String documentName, String predicateUri) { org.junit.Assert.assertFalse(isObjectDocumentChecked()); Select predicateSelect = new Select(predicate); predicateSelect.selectByValue(predicateUri); Select2WidgetElement documentSuggestionWidget = new Select2WidgetElement(driver, By.xpath("//*[@id='s2id_createForm:nxw_singleDocumentSuggestion_select2']")); documentSuggestionWidget.selectValue(documentName); Function<WebDriver, Boolean> isDocumentSelected = new Function<WebDriver, Boolean>() { public Boolean apply(WebDriver driver) { String value = selectedDocument.getAttribute("value"); boolean result = StringUtils.isNotBlank(value); if (!result) { log.debug("Waiting for select2 ajaxReRender"); }//w w w . j av a2s . c o m return result; } }; org.junit.Assert.assertTrue(isObjectDocumentChecked()); Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(SELECT2_CHANGE_TIMEOUT, TimeUnit.SECONDS).pollingEvery(100, TimeUnit.MILLISECONDS); wait.until(isDocumentSelected); log.debug("Submitting relation on document: " + selectedDocument.getAttribute("value")); addButton.click(); return asPage(RelationTabSubPage.class); }
From source file:org.objectfabric.Selenium.java
License:Apache License
@Test public void testHelloWorld() { _driver.get("http://www.google.com"); Wait<WebDriver> wait = getWait(); wait.until(new Function<WebDriver, Object>() { @Override/*from www . jav a 2 s . c om*/ public Object apply(WebDriver _) { WebElement text = _driver.findElement(By.name("text")); return "Hello World!".equals(text) ? this : null; } }); // WebElement text = _driver.findElement(By.name("text")); // searchBox.sendKeys("webdriver"); // searchBox.quit(); // assertEquals("webdriver - Google Search", _driver.getTitle()); }
From source file:org.objectfabric.Selenium.java
License:Apache License
private WebElement fluentWait(final By locator) { Wait<WebDriver> wait = new FluentWait<WebDriver>(_driver) // .withTimeout(5, TimeUnit.SECONDS) // .pollingEvery(100, TimeUnit.MILLISECONDS) // .ignoring(NoSuchElementException.class); WebElement foo = wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(locator); }//from w ww .jav a2 s . c o m }); return foo; }
From source file:org.testeditor.fixture.web.AbstractWebFixture.java
License:Open Source License
/** * Searches for a given text on the page. * /* w ww.j a v a 2 s . co m*/ * @param text * to be searched for * @return {@code true} if the {@code text} is present on the page, * {@code false} otherwise */ public boolean checkTextIsPresentOnPage(final String text) { waitForPage(); try { int interval = (int) Math.floor(Math.sqrt(timeout)); logger.info("Got interval: " + interval); Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver).withTimeout(timeout, TimeUnit.SECONDS) .pollingEvery(interval, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class, StaleElementReferenceException.class); return wait.until(new ExpectedCondition<Boolean>() { @Override public Boolean apply(WebDriver driver) { String source = webDriver.getPageSource(); logger.info("Got source: " + "\"" + source + "\""); logger.info("text to be checked: " + "\"" + text + "\""); source = source.replaceFirst("(?i:<HEAD[^>]*>[\\s\\S]*</HEAD>)", ""); return source.contains(text.trim()); } }); } catch (Exception e) { return false; } }
From source file:org.testeditor.fixture.web.AbstractWebFixture.java
License:Open Source License
/** * Waits for page is complete loaded. Therefore the "document.readyState" is * checked./* w ww . j av a 2 s . c o m*/ * * @return {@code true} if complete loaded during the timeout, {@code false} * otherwise */ public boolean waitForPage() { int interval = (int) Math.floor(Math.sqrt(timeout)); Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver).withTimeout(timeout, TimeUnit.SECONDS) .pollingEvery(interval, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class, StaleElementReferenceException.class); try { return wait.until(new ExpectedCondition<Boolean>() { @Override public Boolean apply(WebDriver arg) { return ((JavascriptExecutor) webDriver).executeScript("return document.readyState") .equals("complete"); } }); } catch (TimeoutException e) { throw new ContinueTestException("Page could not be loaded within the timeout."); } catch (Exception e) { logger.error(e.getMessage()); } return true; }
From source file:org.testeditor.fixture.web.AbstractWebFixture.java
License:Open Source License
/** * Finds and returns all web element in the DOM matching the technical * locator. This does not necessarily mean that the elements are visible. * The configured {@code timeout} is used to specify the time to wait for * the element./* w w w . j a v a2 s . co m*/ * * @param elementListKey * key in the element list to find the technical locator * @param replaceArgs * values to replace the place holders in the element list entry * @return the web elements or {@code null} if no matching element is * present in the DOM * @throws StopTestException * if a timeout occurred while finding the web elements */ protected List<WebElement> findWebElements(String elementListKey, String... replaceArgs) throws StopTestException { int interval = (int) Math.floor(Math.sqrt(timeout)); Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver).withTimeout(timeout, TimeUnit.SECONDS) .pollingEvery(interval, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class, StaleElementReferenceException.class); try { return wait.until( ExpectedConditions.presenceOfAllElementsLocatedBy(createBy(elementListKey, replaceArgs))); } catch (TimeoutException e) { throw new StopTestException("There was a timeout while finding the element '" + createBy(elementListKey, replaceArgs) + "'!"); } }
From source file:org.testeditor.fixture.web.AbstractWebFixture.java
License:Open Source License
/** * Finds and returns a list of web element displayed on the page. If non * matching element, an empty list is returned. * /* www . ja v a2s . c o m*/ * @param elementListKey * key in the element list to find the technical locator * @param replaceArgs * values to replace the place holders in the element list entry * @return a list of available (present and not hidden) web elements * @throws StopTestException * if a timeout occurred */ protected List<WebElement> findAllAvailableWebElements(String elementListKey, String... replaceArgs) throws StopTestException { int interval = (int) Math.floor(Math.sqrt(timeout)); Wait<WebDriver> wait = new FluentWait<WebDriver>(webDriver).withTimeout(timeout, TimeUnit.SECONDS) .pollingEvery(interval, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class, StaleElementReferenceException.class); try { return wait.until( ExpectedConditions.visibilityOfAllElementsLocatedBy(createBy(elementListKey, replaceArgs))); } catch (TimeoutException e) { throw new StopTestException("There was a timeout while finding the element '" + createBy(elementListKey, replaceArgs) + "'!"); } }
From source file:org.xwiki.test.ui.framework.elements.BaseElement.java
License:Open Source License
/** * Wait until one or all of a array of element locators are displayed. * /*from w w w .ja v a 2s .c om*/ * @param locators the array of element locators to look for. * @param timeout how long to wait in seconds before giving up. * @param all if true then don't return until all elements are found. Otherwise return after finding one. */ public void waitUntilElementsAreVisible(final By[] locators, int timeout, final boolean all) { Wait<WebDriver> wait = new WebDriverWait(this.getDriver(), timeout); try { wait.until(new ExpectedCondition<WebElement>() { public WebElement apply(WebDriver driver) { RenderedWebElement element = null; for (int i = 0; i < locators.length; i++) { try { element = (RenderedWebElement) driver.findElement(locators[i]); } catch (NotFoundException e) { // This exception is caught by WebDriverWait // but it returns null which is not necessarily what we want. if (all) { return null; } continue; } // At this stage it's possible the element is no longer valid (for example if the DOM has // changed). If it's no longer attached to the DOM then consider we haven't found the element // yet. try { if (element.isDisplayed()) { if (!all) { return element; } } else if (all) { return null; } } catch (StaleElementReferenceException e) { // Consider we haven't found the element yet return null; } } return element; } }); } catch (TimeoutException e) { StringBuffer sb = new StringBuffer("Failed to find the following locators: [\n"); for (By by : locators) { sb.append(by).append("\n"); } sb.append("] in the source [\n"); sb.append(getDriver().getPageSource()); sb.append("\n]"); throw new TimeoutException(sb.toString(), e); } }