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

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

Introduction

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

Prototype

public WebDriverWait(WebDriver driver, Duration timeout) 

Source Link

Document

Wait will ignore instances of NotFoundException that are encountered (thrown) by default in the 'until' condition, and immediately propagate all others.

Usage

From source file:br.com.mundotigre.scripts.WaitTool.java

License:Open Source License

/**
* Wait for the element to be present in the DOM, and displayed on the page. 
* And returns the first WebElement using the given method.
* 
* @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 WebElement   the first WebElement using the given method, or null (if the timeout is reached)
*///  w  w w.ja va 2s. c o m
public static WebElement waitForElement(WebDriver driver, final By by, int timeOutInSeconds) {
    WebElement element;
    try {
        //To use WebDriverWait(), we would have to nullify implicitlyWait(). 
        //Because implicitlyWait time also set "driver.findElement()" wait time.  
        //info from: https://groups.google.com/forum/?fromgroups=#!topic/selenium-users/6VO_7IXylgY
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait() 

        WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
        element = wait.until(ExpectedConditions.visibilityOfElementLocated(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:br.com.mundotigre.scripts.WaitTool.java

License:Open Source License

/**
* Wait for the element to be present in the DOM, regardless of being displayed or not.
* And returns the first WebElement using the given method.
*
* @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 WebElement   the first WebElement using the given method, or null (if the timeout is reached)
*//*from  w w  w .j av a 2 s  .  c  o m*/
public static WebElement waitForElementPresent(WebDriver driver, final By by, int timeOutInSeconds) {
    WebElement element;
    try {
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait() 

        WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
        element = wait.until(ExpectedConditions.presenceOfElementLocated(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:br.com.mundotigre.scripts.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 ww  . java  2s  .  c  om
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:br.com.mundotigre.scripts.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 /*  w ww .  ja  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:br.com.mundotigre.scripts.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   www  .j a v a 2  s .  co  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:br.com.mundotigre.scripts.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
 * //from w ww.j ava 2s  .c om
 * @return boolean true or false(condition fail, or if the timeout is reached)
 **/
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;
}

From source file:br.com.mundotigre.scripts.WaitTool.java

License:Open Source License

/** Waits for the completion of Ajax jQuery processing by checking "return jQuery.active == 0" condition.  
 *
 * @param WebDriver - The driver object to be used to wait and find the element
 * @param int - The time in seconds to wait until returning a failure
 * /*  ww w .jav  a 2  s  . c om*/
 * @return boolean true or false(condition fail, or if the timeout is reached)
 * */
public static boolean waitForJQueryProcessing(WebDriver driver, int timeOutInSeconds) {
    boolean jQcondition = 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("return jQuery.active == 0");
            }
        });
        jQcondition = (Boolean) ((JavascriptExecutor) driver).executeScript("return jQuery.active == 0");
        driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_4_PAGE, TimeUnit.SECONDS); //reset implicitlyWait
        return jQcondition;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return jQcondition;
}

From source file:br.edu.ifpb.praticas.testSystem.FilmeTest.java

@Test
public void testCadastro() throws Exception {
    WebDriver driver = new FirefoxDriver();
    WebElement element = driver.findElement(By.name("nome"));
    // Create a new instance of the Firefox driver
    // Notice that the remainder of the code relies on the interface, 
    // not the implementation.

    assertEquals("http://localhost:8085/SisFilme/index.xhtml", driver.getCurrentUrl());

    Thread.sleep(2000L);/* w  w w  . j  a v a2 s .  c o m*/
    element = driver.findElement(By.name("nome"));
    element.sendKeys("007 contra moscol");
    element = driver.findElement(By.name("nome"));
    element.sendKeys("007 contra moscol");

    element = driver.findElement(By.name("ano"));
    element.sendKeys("2014");
    element = driver.findElement(By.name("genero"));
    element = driver.findElement(By.name("nota"));
    element.sendKeys("2");

    element = driver.findElement(By.name("salvar"));
    Thread.sleep(2000L);
    element.click();
    assertEquals("http://localhost:8085/SisFilme/gerenciamento.xhtml", driver.getCurrentUrl());

    assertNotNull(element);
    // Wait for the page to load, timeout after 10 seconds
    (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver d) {
            return d.getTitle().contains("NetBeans");
        }
    });

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

From source file:br.edu.ifpb.praticas.testSystem.TituloPaginaT.java

@Test
public void testTitulo() throws Exception {

    WebDriver driver = new FirefoxDriver();
    driver.get("http://localhost:8085/SisFilme/index.xhtml");
    assertEquals("Avalie filmes", driver.getTitle());

    (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
        @Override//from w w w  . j av a 2s . c o  m
        public Boolean apply(WebDriver d) {
            return d.getTitle().contains("Avalie filmes");
        }
    });

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

From source file:br.eti.kinoshita.selenium.samples.GoogleMainPage.java

License:Open Source License

/**
 * @param driver Selenium WebDriver.//from   ww  w. j a va 2 s.com
 * @param timeOutInSeconds 
 */
public GoogleMainPage(WebDriver driver, int timeOutInSeconds) {
    super(driver, timeOutInSeconds);
    this.wait = new WebDriverWait(driver, timeOutInSeconds);
}