Example usage for org.openqa.selenium.support.ui ExpectedCondition ExpectedCondition

List of usage examples for org.openqa.selenium.support.ui ExpectedCondition ExpectedCondition

Introduction

In this page you can find the example usage for org.openqa.selenium.support.ui ExpectedCondition ExpectedCondition.

Prototype

ExpectedCondition

Source Link

Usage

From source file:scormADL12.java

License:Open Source License

/**
 *  Explicit Wait for element function//from w  w w  .j  ava  2 s  .  c  o  m
 * @param locator
 * @return
 */
public ExpectedCondition<WebElement> presenceOfElementLocated(final By locator) {
    return new ExpectedCondition<WebElement>() {
        public WebElement apply(WebDriver webDriver) {
            return webDriver.findElement(locator);
        }
    };
}

From source file:scormADL12.java

License:Open Source License

/**
 *  Explicit wait for element with text//from  w  w  w . j ava 2 s.  c o  m
 * @param locator
 * @param text
 * @return
 */
public WebElement waitElementWithTextPresent(final By locator, final String text) {
    return new WebDriverWait(driver, 30).until(new ExpectedCondition<WebElement>() {
        @Override
        public WebElement apply(WebDriver d) {
            WebElement e = d.findElement(locator);
            String elementText = e.getText();
            if (elementText != null && elementText.contains(text)) {
                return e;
            } else {
                return null;
            }
        }
    });
}

From source file:IntegracaoIT.java

@Test
public void testSimple() throws Exception {
    // Create a new instance of the Firefox driver
    // Notice that the remainder of the code relies on the interface, 
    // not the implementation.
    WebDriver driver = new FirefoxDriver();

    // And now use this to visit NetBeans
    driver.get("http://www.netbeans.org");
    // Alternatively the same thing can be done like this
    // driver.navigate().to("http://www.netbeans.org");

    // Check the title of the page
    // Wait for the page to load, timeout after 10 seconds
    (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
        @Override/* w  ww.  j  av a 2s .co m*/
        public Boolean apply(WebDriver d) {
            return d.getTitle().contains("NetBeans");
        }
    });

    //Close the browser
    driver.quit();
}

From source file:seleniumTester.java

@Test
public void test2() throws Exception {
    WebElement element = driver.findElement(By.id("filter"));
    element.sendKeys("1999");
    (new WebDriverWait(driver, WAIT_MAX)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            WebElement e = d.findElement(By.tagName("tbody"));
            List<WebElement> rows = e.findElements(By.tagName("tr"));

            return rows.size() == 2;
        }/*from  w ww  .j av  a2s  .  c o m*/
    });

}

From source file:seleniumTester.java

@Test
public void test3() {
    WebElement element = driver.findElement(By.id("filter"));
    element.sendKeys(Keys.CONTROL + "a");
    element.sendKeys(Keys.DELETE);//from  www  . j a v a  2s  .  co  m

    (new WebDriverWait(driver, WAIT_MAX)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            WebElement e = d.findElement(By.tagName("tbody"));
            List<WebElement> rows = e.findElements(By.tagName("tr"));

            Assert.assertThat(rows.size(), is(5));
            return rows.size() == 5;
        }
    });

}

From source file:seleniumTester.java

@Test
public void test_5() {
    List<WebElement> tds = driver.findElement(By.id("tbodycars")).findElements(By.cssSelector("tr")).get(0)
            .findElements(By.tagName("td"));
    tds.get(7).findElements(By.tagName("a")).get(0).click();
    WebElement element = driver.findElement(By.id("description"));
    element.sendKeys(Keys.CONTROL + "a");
    element.sendKeys(Keys.DELETE);/*from   ww w. j a v  a2 s .com*/
    element.sendKeys("Cool car");
    driver.findElement(By.id("save")).click();

    (new WebDriverWait(driver, WAIT_MAX)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            WebElement e = d.findElement(By.tagName("tbody"));
            List<WebElement> rows = e.findElements(By.tagName("tr"));
            String result = null;
            for (int i = 0; i < rows.size(); i++) {
                if (rows.get(i).findElements(By.tagName("td")).get(0).getText().equalsIgnoreCase("938")) {
                    result = rows.get(i).findElements(By.tagName("td")).get(5).getText();
                    break;
                }
            }
            assertThat(result, is("Cool car"));
            return true;
        }
    });
}

From source file:WaitTool.java

License:Open Source License

/**
 * Wait for the List<WebElement> to be present in the DOM, regardless of being displayed or not.
 * Returns all elements within the current page DOM.
 *
 * @param WebDriver   The driver object to be used
 * @param By   selector to find the element
 * @param int   The time in seconds to wait until returning a failure
 *
 * @return List<WebElement> all elements within the current page DOM, or null (if the timeout is reached)
 *///w  w w  . j  a  v  a 2  s . c o  m
public static List<WebElement> waitForListElementsPresent(WebDriver driver, final By by, int timeOutInSeconds) {
    List<WebElement> elements;
    try {
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait()

        WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
        wait.until((new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver driverObject) {
                return areElementsPresent(driverObject, by);
            }
        }));

        elements = driver.findElements(by);
        driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_4_PAGE, TimeUnit.SECONDS); //reset implicitlyWait
        return elements; //return the element
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:WaitTool.java

License:Open Source License

/**
 * Wait for an element to appear on the refreshed web-page.
 * And returns the first WebElement using the given method.
 *
 * This method is to deal with dynamic pages.
 *
 * Some sites I (Mark) have tested have required a page refresh to add additional elements to the DOM.
 * Generally you (Chon) wouldn't need to do this in a typical AJAX scenario.
 *
 * @param WebDriver   The driver object to use to perform this element search
 * @param locator   selector to find the element
 * @param int   The time in seconds to wait until returning a failure
 *
 * @return WebElement   the first WebElement using the given method, or null(if the timeout is reached)
 *
 * @author Mark Collin/*from   w ww  . j a v a 2s  .c  o  m*/
 */
public static WebElement waitForElementRefresh(WebDriver driver, final By by, int timeOutInSeconds) {
    WebElement element;
    try {
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait()
        new WebDriverWait(driver, timeOutInSeconds) {
        }.until(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver driverObject) {
                driverObject.navigate().refresh(); //refresh the page ****************
                return isElementPresentAndDisplay(driverObject, by);
            }
        });
        element = driver.findElement(by);
        driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_4_PAGE, TimeUnit.SECONDS); //reset implicitlyWait
        return element; //return the element
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:WaitTool.java

License:Open Source License

/**
 * Wait for the Text to be present in the given element, regardless of being displayed or not.
 *
 * @param WebDriver   The driver object to be used to wait and find the element
 * @param locator   selector of the given element, which should contain the text
 * @param String   The text we are looking
 * @param int   The time in seconds to wait until returning a failure
 *
 * @return boolean/*from  w w  w  .  ja v a 2 s.c  o  m*/
 */
public static boolean waitForTextPresent(WebDriver driver, final By by, final String text,
        int timeOutInSeconds) {
    boolean isPresent = false;
    try {
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait()
        new WebDriverWait(driver, timeOutInSeconds) {
        }.until(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver driverObject) {
                return isTextPresent(driverObject, by, text); //is the Text in the DOM
            }
        });
        isPresent = isTextPresent(driver, by, text);
        driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_4_PAGE, TimeUnit.SECONDS); //reset implicitlyWait
        return isPresent;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:WaitTool.java

License:Open Source License

/**
 * Waits for the Condition of JavaScript.
 *
 *
 * @param WebDriver      The driver object to be used to wait and find the element
 * @param String   The javaScript condition we are waiting. e.g. "return (xmlhttp.readyState >= 2 && xmlhttp.status == 200)"
 * @param int   The time in seconds to wait until returning a failure
 *
 * @return boolean true or false(condition fail, or if the timeout is reached)
 **//*from   w  w  w . jav a  2  s  .c  om*/
public static boolean waitForJavaScriptCondition(WebDriver driver, final String javaScript,
        int timeOutInSeconds) {
    boolean jscondition = false;
    try {
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait()
        new WebDriverWait(driver, timeOutInSeconds) {
        }.until(new ExpectedCondition<Boolean>() {

            @Override
            public Boolean apply(WebDriver driverObject) {
                return (Boolean) ((JavascriptExecutor) driverObject).executeScript(javaScript);
            }
        });
        jscondition = (Boolean) ((JavascriptExecutor) driver).executeScript(javaScript);
        driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_4_PAGE, TimeUnit.SECONDS); //reset implicitlyWait
        return jscondition;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}