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: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.  j  av a 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: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  w w .  j a  v a 2  s. co  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//w  ww. j a va  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)
 **///  w  ww.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;
}

From source file: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
 *
 * @return boolean true or false(condition fail, or if the timeout is reached)
 * *///from  w w  w  .  j a  v a 2s  .c o m
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:TestaCadastroProcesso.java

@Test(dependsOnGroups = "login", groups = "required")
public void testaMsgNumero() throws Exception {

    driver.navigate()/* w  ww  . j  a va 2s .c o m*/
            .to("http://52.1.49.37/SIAPCON_SPRINT11/ListarProcessos.jsf?(Not.Licensed.For.Production)=");

    driver.navigate().to(
            "http://52.1.49.37/SIAPCON_SPRINT11/ProcessoDetail.jsf?processoId=0&(Not.Licensed.For.Production)=");

    // gera um tempo de espera para a pgina carregar e o elemento ser renderizado
    driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);

    try {

        // insere um numero de processo valido
        WebElement numero = driver
                .findElement(By.id("RichWidgets_wt95:wtMainContent:wtnumeroProcessoAtualWidget"));
        numero.sendKeys("000");

        // clica no boto submit
        WebElement submit = driver.findElement(By.id("RichWidgets_wt95:wtMainContent:wt38"));
        submit.click();

        // Espera at que o elemento que contm a msg de erro esteja vsivel
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("Feedback_Message_Error")));

        // testa se a msg de obrigatoriedade est sendo exibida
        if (!driver.findElement(By.className("Feedback_Message_Error")).isDisplayed()) {
            Assert.fail("No est exibindo msg");
        }

    } catch (Exception e) {
        throw (e);
    }

    driver.navigate().to(
            "http://52.1.49.37/SIAPCON_SPRINT11/ProcessoDetail.jsf?processoId=0&(Not.Licensed.For.Production)=");

    // insere um numero de processo valido
    WebElement numero = driver.findElement(By.id("RichWidgets_wt95:wtMainContent:wtnumeroProcessoAtualWidget"));
    numero.sendKeys("00000.000000/00");

    try {

        // clica no boto submit
        WebElement submit = driver.findElement(By.id("RichWidgets_wt95:wtMainContent:wt38"));
        submit.click();

        // espera at que o elemento que contm a msg de erro esteja visvel
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("Feedback_Message_Error")));

        if (!driver.findElement(By.className("Feedback_Message_Error")).isDisplayed()) {
            Assert.fail("No est exibindo msg");
        }

    } catch (Exception e) {
        throw (e);
    }
}

From source file:TestaCadastroProcesso.java

@Test(dependsOnGroups = "required", groups = "salva")
public void testaInsercaoProcesso() {

    driver.navigate().to(//from w  w  w.j a  va 2 s  .c o m
            "http://52.1.49.37/SIAPCON_SPRINT11/ProcessoDetail.jsf?processoId=0&(Not.Licensed.For.Production)=");

    driver.navigate().to(
            "http://52.1.49.37/SIAPCON_SPRINT11/ProcessoDetail.jsf?processoId=0&(Not.Licensed.For.Production)=");

    try {

        // insere um numero de processo valido
        WebElement numero = driver
                .findElement(By.id("RichWidgets_wt95:wtMainContent:wtnumeroProcessoAtualWidget"));
        numero.sendKeys(proc);

        // insere um tipo de processo valido
        WebElement tipo = driver.findElement(By.id("RichWidgets_wt95:wtMainContent:wttipoProcessoWidget"));
        tipo.sendKeys("a");

        // clica no boto submit
        WebElement submit = driver.findElement(By.id("RichWidgets_wt95:wtMainContent:wt38"));
        submit.click();

        // espera at que o boto de encerrar cadastro esteja visvel
        WebDriverWait wait = new WebDriverWait(driver, 60);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("corbranca")));

        if (!driver.findElement(By.className("corbranca")).isDisplayed()) {
            Assert.fail("No exibiu o boto encerrar cadastro");
        }
    } catch (Exception e) {
        throw (e);
    }
}

From source file:GeneralCookieDriver.java

License:Open Source License

public Integer conductTest(Browser browser, boolean privateBrowsing, int[] cookies, int numTrialsPerRun) {
    WebDriver driver = getWebDriver(browser, privateBrowsing);

    if (driver == null) {
        System.err.println("WebDriver could not be found for " + browser + " in "
                + (privateBrowsing ? "private" : "normal") + " mode.");
        return 0;
    }/*from   w w w  .  j  av  a2 s .  c om*/

    if (browser.equals(Browser.SAFARI) && privateBrowsing) {
        System.err.println("Pausing for 10 seconds...");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.err.println("Resuming from pause...");
    }

    for (int c = 0; c < cookies.length; c++) {

        for (int i = 0; i < numTrialsPerRun; i++) {

            driver.get(new String(cookieGeneratorLocation + "?numCookies=" + cookies[c] + "&browser="
                    + browser.toString() + "&mode=" + (privateBrowsing ? "private" : "normal")));

            try {
                new WebDriverWait(driver, 120).until(ExpectedConditions.titleContains("[ResultProvided]"));

            } catch (org.openqa.selenium.UnhandledAlertException e) {
                System.err.println("UnhandledAlertException");
                continue;
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }

            Integer result = new Integer(driver.getTitle().split(" ")[0]);

            System.out.println(
                    browser.toString() + (privateBrowsing ? ",P," : ",N,") + cookies[c] + "," + result);
        }
    }

    //Close the browser
    driver.quit();

    return 0;
}

From source file:NumOfWHGamesTest.java

@Before
public void setup() throws MalformedURLException, UnknownHostException {

    ProfilesIni allProfiles = new ProfilesIni();
    FirefoxProfile myProfile = allProfiles.getProfile("SimBin");
    myProfile.setAcceptUntrustedCertificates(true);
    myProfile.setAssumeUntrustedCertificateIssuer(false);
    driver2 = new FirefoxDriver(myProfile);

    WebDriverWait wait = new WebDriverWait(driver2, 15);
    driver2.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    driver2.navigate().to(urlWHGames);//from   www. j a  v a2s  .  com
    driver2.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver2.navigate().to(urlWHGamesAZ);

    wait.until(ExpectedConditions.urlMatches(urlWHGamesAZ));

    wait = new WebDriverWait(driver2, 30);
    driver2.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

From source file:TestWriteanArticle.java

@org.junit.Test
public void positive() throws InterruptedException {
    System.setProperty("webdriver.gecko.driver", "C://Users/Mari/Downloads/geckodriver.exe");
    WebDriver webDriver = new FirefoxDriver();
    String page = "http://www.wikihow.com/Special:CreatePage";
    webDriver.get(page);//from w  w  w.  j  a va2s.  co m
    WebElement title = webDriver.findElement(By.id("cp_title_input"));
    title.sendKeys("how to use selenium");
    webDriver.findElement(By.id("cp_title_btn")).click();
    Thread.sleep(10);

    WebDriverWait wait = new WebDriverWait(webDriver, 50);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("cpr_title_hdr")));
    assertTrue(webDriver.getPageSource().contains("Are any of these topics the same as yours?"));

    webDriver.close();
}