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:bg.pragmatic.lecture13mvn.waits.utils.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.
 * /*ww w  .ja  v  a2  s .  com*/
 * 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
 */
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>() {

            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:bg.pragmatic.lecture13mvn.waits.utils.WaitTool.java

License:Open Source License

/**
 * Wait for the Text to be present in the given element, regardless of being
 * displayed or not.//from w w  w . j av  a  2s .  c o  m
 * 
 * @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
 */
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>() {

            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:bg.pragmatic.lecture13mvn.waits.utils.WaitTool.java

License:Open Source License

/**
 * Waits for the Condition of JavaScript.
 * //w  w  w .j  a  v  a2 s. c o m
 * 
 * @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)
 **/
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>() {

            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:bg.pragmatic.lecture13mvn.waits.utils.WaitTool.java

License:Open Source License

/**
 * Waits for the completion of Ajax jQuery processing by checking
 * "return jQuery.active == 0" condition.
 * /*from www  .ja  va 2 s .  c om*/
 * @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)
 * */
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>() {

            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:bg.pragmatic.lecture13mvn.waits.utils.WaitTool.java

License:Open Source License

public static void waitForPageLoad(WebDriver driver) {
    Wait<WebDriver> wait = new WebDriverWait(driver, 10);
    wait.until(new Function<WebDriver, Boolean>() {

        public Boolean apply(WebDriver driver) {
            //System.out.println("Current window state : " + String.valueOf(((JavascriptExecutor)driver).executeScript("return document.readyState")));
            return String.valueOf(((JavascriptExecutor) driver).executeScript("return document.readyState"))
                    .equals("complete");
        }/*from  w w  w  .j  av a 2s .c o  m*/
    });
}

From source file:bg.pragmatic.myautomationframework.utils.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.
 * //from  w w w  .  j a va  2s.  c  o  m
 * @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)
 */
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>() {
            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:bi.com.seleniumgrid.PhantomJsTest.java

License:Apache License

@Test
public void test() {
    final DesiredCapabilities capabilities = new DesiredCapabilities();
    // Configure our WebDriver to support JavaScript and be able to find the PhantomJS binary
    capabilities.setJavascriptEnabled(true);
    capabilities.setCapability("takesScreenshot", false);
    capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, PHANTOMJS_BINARY);
    final WebDriver driver = new PhantomJSDriver(capabilities);
    // Your test code here. For example:
    WebDriverWait wait = new WebDriverWait(driver, 30); // 30 seconds of timeout
    driver.get("https://en.wikipedia.org/wiki/Main_Page"); // navigate to Wikipedia

    pageTitle = driver.getTitle().trim();
    Assert.assertEquals(pageTitle, "GoalQuest");

    System.out.println("Page title is: " + driver.getTitle());

    By searchInput = By.id("searchInput"); // search for "Software"
    wait.until(ExpectedConditions.presenceOfElementLocated(searchInput));
    driver.findElement(searchInput).sendKeys("Software");
    By searchButton = By.id("searchButton");
    wait.until(ExpectedConditions.elementToBeClickable(searchButton));
    driver.findElement(searchButton).click();

    wait.until(ExpectedConditions.textToBePresentInElementLocated(By.tagName("body"), "Computer software")); // assert that the resulting page contains a text
}

From source file:botski.example.AddMeFastExample.java

License:Apache License

public void run() {
    try {//www.ja va2  s .c  o  m
        int likes = 0;
        int points = 0;
        long start_time = System.currentTimeMillis();

        // Setup Selenium
        driver = new FirefoxDriver();
        jse = (JavascriptExecutor) driver;

        // Perform the Login actions
        facebookLogin();
        addMeFastLogin();

        // Goto the Facebook Likes page
        driver.get("http://addmefast.com/free_points/facebook_likes.html");
        if ("http://addmefast.com/free_points/facebook_likes.html".equals(driver.getCurrentUrl()) == false) {
            System.err.println(
                    "I was trying to navigate to 'http://addmefast.com/free_points/facebook_likes.html' and ended up here '"
                            + driver.getCurrentUrl() + "'");
            return;
        }

        // Remember the main window handle
        String windowHandle = (String) driver.getWindowHandles().toArray()[0];

        // Go into the loop
        while (true) {
            // Reacts to the appearance of a Like button
            (new WebDriverWait(driver, 60)).until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver d) {
                    return (Boolean) jse.executeScript(
                            "if(jQuery('.single_like_button').length){return true;}else{return false;}");
                }
            });

            // Click the like button by injecting JavaScript to the page
            jse.executeScript("jQuery('.single_like_button').click();");

            // Allow time for the popup window to appear
            Utils.sleep(1000);

            // Switch to the new window
            Set<String> winSet = driver.getWindowHandles();
            List<String> winList = new ArrayList<String>(winSet);
            String newTab = winList.get(winList.size() - 1);
            driver.switchTo().window(newTab); // switch to new tab

            // Click the first "Like" button on the page
            jse.executeScript(
                    "var inputs=document.getElementsByTagName('input');for(var i=0; i<inputs.length; i++){var input=inputs[i];var value=input.getAttribute('value');if(value!=null){if(value=='Like'){input.click();break;}}}");

            // Allow time for the Like action to go through
            Utils.sleep(1000);

            // Close this window and switch back to the main one
            driver.close();
            driver.switchTo().window(windowHandle);

            // This delay allows AddMeFast to detect the window close and request the next page to Like
            Utils.sleep(5000);

            // Update counters
            likes++;
            WebElement points_count = driver.findElement(By.className("points_count"));
            try {
                points = Integer.parseInt(points_count.getText());
            } catch (Exception ignore) {
            }
            long diff = (System.currentTimeMillis() - start_time) / 1000;
            System.out.println("" + likes + " likes, " + points + " points in " + diff + " seconds");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:br.com.dextraining.selenium.SeleniumTestCase.java

protected void esperarPor(String elementoDeId) throws InterruptedException {
    //Thread.sleep(2000); //apenas para podermos visualizar a tela antes da verificacao
    (new WebDriverWait(driver, TEMPO_MAXIMO_ESPERA)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver driver) {
            return driver.findElement(By.id(elementoDeId)).isDisplayed();
        }//w w w . jav  a 2  s  . c o  m
    });
}

From source file:br.com.esign.logistics.test.selenium.page.HomePage.java

License:Open Source License

private void waitForElement(WebElement element) {
    WebDriverWait driverWait = new WebDriverWait(driver, 20);
    driverWait.until(ExpectedConditions.visibilityOf(element));
    driverWait.until(ExpectedConditions.elementToBeClickable(element));
}