Example usage for org.openqa.selenium.support.ui ExpectedConditions visibilityOfElementLocated

List of usage examples for org.openqa.selenium.support.ui ExpectedConditions visibilityOfElementLocated

Introduction

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

Prototype

public static ExpectedCondition<WebElement> visibilityOfElementLocated(final By locator) 

Source Link

Document

An expectation for checking that an element is present on the DOM of a page and visible.

Usage

From source file:IWDmainsiteProject.PageObjects.Verify_Pages.java

public static void Verify_Start_Scan_Health_Check(WebDriver driver) {

    driver.findElement(By.cssSelector("form[id='health_check__scan_store'] input[name='site_url']"))
            .sendKeys("https://www.iwdagency.com/extensions/");
    driver.findElement(By.cssSelector("#health_check__scan_store>button")).click();

    WebDriverWait wait1 = new WebDriverWait(driver, 10);
    try {//w w w  .  ja  v  a2s.  c o m
        wait1.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#healthcheck__overlay")));
        wait1.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("#healthcheck__overlay")));
    } catch (org.openqa.selenium.NoSuchElementException | TimeoutException e) {
        System.out.println("#healthcheck__overlay not visible");

        //.health_cheack__cancel

    }
}

From source file:IWDmainsiteProject.PageObjects.Verify_Pages.java

public static void Verify_Scan_My_Store_Health_Check(WebDriver driver) {

    driver.findElement(By.cssSelector("form[id='health_check__site_url'] input[name='site_url']"))
            .sendKeys("https://www.iwdagency.com/extensions/");
    driver.findElement(By.cssSelector("#health_check__site_url>button")).click();

    WebDriverWait wait1 = new WebDriverWait(driver, 10);
    try {/*from w ww  . j a  v  a  2s . com*/
        wait1.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#healthcheck__overlay")));
        wait1.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("#healthcheck__overlay")));
    } catch (org.openqa.selenium.NoSuchElementException | TimeoutException e) {
        System.out.println("#healthcheck__overlay not visible");
    }
    String TextLink1 = driver.findElement(By.cssSelector(".site_url")).getText();
    System.out.println(TextLink1);
    String expectedlink1 = "https://www.iwdagency.com/extensions/";
    assertEquals(TextLink1, expectedlink1);
    System.out.println("Link is correct https://www.iwdagency.com/extensions");

    driver.findElement(By.cssSelector(".health_check__remove.fa.fa-times")).click();
    wait1.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".site_url")));
    System.out.println("Link was removed");

    driver.findElement(By.cssSelector("form[id='health_check__scan_url'] input[name='site_url']"))
            .sendKeys("http://dev.weeetail.com/extensions/");
    driver.findElement(By.cssSelector("#health_check__scan_url>button")).click();

    try {
        wait1.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#healthcheck__overlay")));
        wait1.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("#healthcheck__overlay")));
    } catch (org.openqa.selenium.NoSuchElementException | TimeoutException e) {
        System.out.println("#healthcheck__overlay not visible");
    }

    String TextLink2 = driver.findElement(By.cssSelector(".site_url")).getText();
    System.out.println(TextLink2);
    String expectedlink2 = "http://dev.weeetail.com/extensions/";
    assertEquals(TextLink2, expectedlink2);
    System.out.println("Link is correct dev.m2ce.deviwd.com");
}

From source file:IWDmainsiteProject.PageObjects.Verify_Pages.java

public static void Verify_buttons_left_right_Meet_the_team(WebDriver driver) throws InterruptedException {

    WebDriverWait wait1 = new WebDriverWait(driver, 10);
    wait1.until(ExpectedConditions
            .visibilityOfElementLocated(By.cssSelector("span[class='fa fa-chevron-circle-left']")));
    wait1.until(ExpectedConditions/*  w w  w .j a v a 2 s .  c  o m*/
            .visibilityOfElementLocated(By.cssSelector("span[class='fa fa-chevron-circle-right']")));
    System.out.println("Right and left buttons were found");

    driver.findElement(By.cssSelector("span[class='fa fa-chevron-circle-left']")).click();
    driver.findElement(By.cssSelector("span[class='fa fa-chevron-circle-left']")).click();
    driver.findElement(By.cssSelector("span[class='fa fa-chevron-circle-left']")).click();
    driver.findElement(By.cssSelector("span[class='fa fa-chevron-circle-right']")).click();
    driver.findElement(By.cssSelector("span[class='fa fa-chevron-circle-right']")).click();
    driver.findElement(By.cssSelector("span[class='fa fa-chevron-circle-right']")).click();
    driver.findElement(By.cssSelector("span[class='fa fa-chevron-circle-left']")).click();
    driver.findElement(By.cssSelector("span[class='fa fa-chevron-circle-left']")).click();
    driver.findElement(By.cssSelector("span[class='fa fa-chevron-circle-left']")).click();
    System.out.println("Functionality of right and left buttons works");

}

From source file:IWDmainsiteProject.PageObjects.Verify_Pages.java

public static void Verify_get_started_price_b2b(WebDriver driver) throws InterruptedException {

    //M1/*from  w  ww.  j av  a2  s  .com*/
    driver.findElement(
            By.cssSelector("a[href=\"/extensions/b2b-wholesale-suite.html?editions=magento-1&plans=Monthly\"]"))
            .click();

    WebDriverWait wait_for_popup = new WebDriverWait(driver, 10);
    wait_for_popup.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".ajaxcart-content")));

    String actual_price_m1 = driver.findElement(By.cssSelector("#product-price-44")).getText();
    System.out.println(actual_price_m1);
    String expected_price_m1 = "$50 /mo";
    assertEquals(expected_price_m1, actual_price_m1);
    System.out.println("$50 /mo was found");

    Thread.sleep(2000);
    driver.findElement(By.cssSelector("#options_105_text")).sendKeys("test.com");
    driver.findElement(By.cssSelector(".button.btn-add-to-cart.left")).click();

    String actual_price_m1_shopping_cart = driver
            .findElement(By.cssSelector("div[class=\"cart-price\"] span[class=\"price\"]")).getText();
    System.out.println(actual_price_m1_shopping_cart);
    String expected_price_m1_shopping_cart = "$50.00";
    assertEquals(expected_price_m1_shopping_cart, actual_price_m1_shopping_cart);
    System.out.println("Price $50 in shopping cart is right ");

    driver.findElement(By.cssSelector(".fa.fa-times")).click();
    driver.findElement(
            By.cssSelector("div[class='cart-empty'] a[href='https://www.iwdagency.com/extensions/']"));
    driver.findElement(By.cssSelector("ul[class='services'] a[href='/extensions/b2b-wholesale-suite.html']"))
            .click();

    //M2 Bronze

    driver.findElement(
            By.cssSelector("a[href=\"/extensions/b2b-wholesale-suite.html?editions=magento-2&plans=Bronze\"]"))
            .click();
    wait_for_popup.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".ajaxcart-content")));
    wait_for_popup.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#select_-177328")));

    String actual_price_m2_bronze = driver.findElement(By.cssSelector("#product-price-44")).getText();
    System.out.println(actual_price_m2_bronze);
    String expected_price_m2_bronze = "$50 /mo";
    assertEquals(actual_price_m2_bronze, expected_price_m2_bronze);
    System.out.println("$50 /mo was found for m2");

    Thread.sleep(2000);
    driver.findElement(By.cssSelector("#options_105_text")).sendKeys("test.com");
    driver.findElement(By.cssSelector(".button.btn-add-to-cart.left")).click();

    String actual_price_m2_bronze_shopping_cart = driver
            .findElement(By.cssSelector("div[class=\"cart-price\"] span[class=\"price\"]")).getText();
    System.out.println(actual_price_m2_bronze_shopping_cart);
    String expected_price_m2_bronze_shopping_cart = "$50.00";
    assertEquals(expected_price_m2_bronze_shopping_cart, actual_price_m2_bronze_shopping_cart);
    System.out.println("Price $50 in shopping cart is right for m2");

    driver.findElement(By.cssSelector(".fa.fa-times")).click();
    driver.findElement(
            By.cssSelector("div[class='cart-empty'] a[href='https://www.iwdagency.com/extensions/']"));
    driver.findElement(By.cssSelector("ul[class='services'] a[href='/extensions/b2b-wholesale-suite.html']"))
            .click();

    // M2 Silver

    driver.findElement(
            By.cssSelector("a[href=\"/extensions/b2b-wholesale-suite.html?editions=magento-2&plans=Silver\"]"))
            .click();
    wait_for_popup.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".ajaxcart-content")));
    wait_for_popup.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#select_-177328")));

    String actual_price_m2_silver = driver.findElement(By.cssSelector("#product-price-44")).getText();
    System.out.println(actual_price_m2_silver);
    String expected_price_m2_silver = "$250 /mo";
    assertEquals(expected_price_m2_silver, actual_price_m2_silver);
    System.out.println("$250 /mo was found for m2");

    Thread.sleep(2000);
    driver.findElement(By.cssSelector("#options_105_text")).sendKeys("test.com");
    driver.findElement(By.cssSelector(".button.btn-add-to-cart.left")).click();

    String actual_price_m2_silver_shopping_cart = driver
            .findElement(By.cssSelector("div[class=\"cart-price\"] span[class=\"price\"]")).getText();
    System.out.println(actual_price_m2_silver_shopping_cart);
    String expected_price_m2_silver_shopping_cart = "$250.00";
    assertEquals(expected_price_m2_silver_shopping_cart, actual_price_m2_silver_shopping_cart);
    System.out.println("Price $250 in shopping cart is right for m2");

    driver.findElement(By.cssSelector(".fa.fa-times")).click();
    driver.findElement(
            By.cssSelector("div[class='cart-empty'] a[href='https://www.iwdagency.com/extensions/']"));
    driver.findElement(By.cssSelector("ul[class='services'] a[href='/extensions/b2b-wholesale-suite.html']"))
            .click();

    //M2 Bronze M2 Gold

    driver.findElement(
            By.cssSelector("a[href=\"/extensions/b2b-wholesale-suite.html?editions=magento-2&plans=Gold\"]"))
            .click();
    wait_for_popup.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".ajaxcart-content")));
    wait_for_popup.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#select_-177328")));

    String actual_price_m2_gold = driver.findElement(By.cssSelector("#product-price-44")).getText();
    System.out.println(actual_price_m2_gold);
    String expected_price_m2_gold = "$500 /mo";
    assertEquals(expected_price_m2_gold, actual_price_m2_gold);
    System.out.println("$500 /mo was found for m2");

    Thread.sleep(2000);
    driver.findElement(By.cssSelector("#options_105_text")).sendKeys("test.com");
    driver.findElement(By.cssSelector(".button.btn-add-to-cart.left")).click();

    String actual_price_m2_gold_shopping_cart = driver
            .findElement(By.cssSelector("div[class=\"cart-price\"] span[class=\"price\"]")).getText();
    System.out.println(actual_price_m2_gold_shopping_cart);
    String expected_price_m2_gold_shopping_cart = "$500.00";
    assertEquals(expected_price_m2_gold_shopping_cart, actual_price_m2_gold_shopping_cart);
    System.out.println("Price $500 in shopping cart is right for m2");

    driver.findElement(By.cssSelector(".fa.fa-times")).click();
    driver.findElement(
            By.cssSelector("div[class='cart-empty'] a[href='https://www.iwdagency.com/extensions/']"));
    driver.findElement(By.cssSelector("ul[class='services'] a[href='/extensions/b2b-wholesale-suite.html']"))
            .click();

    //M2 Bronze M2 Platinum

    driver.findElement(By
            .cssSelector("a[href=\"/extensions/b2b-wholesale-suite.html?editions=magento-2&plans=Platinum\"]"))
            .click();
    wait_for_popup.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".ajaxcart-content")));
    wait_for_popup.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#select_-177328")));

    String actual_price_m2_platinum = driver.findElement(By.cssSelector("#product-price-44")).getText();
    System.out.println(actual_price_m2_platinum);
    String expected_price_m2_platinum = "$1000 /mo";
    assertEquals(expected_price_m2_platinum, actual_price_m2_platinum);
    System.out.println("$1000 /mo was found for m2");

    Thread.sleep(2000);
    driver.findElement(By.cssSelector("#options_105_text")).sendKeys("test.com");
    driver.findElement(By.cssSelector(".button.btn-add-to-cart.left")).click();

    String actual_price_m2_platinum_shopping_cart = driver
            .findElement(By.cssSelector("div[class=\"cart-price\"] span[class=\"price\"]")).getText();
    System.out.println(actual_price_m2_platinum_shopping_cart);
    String expected_price_m2_platinum_shopping_cart = "$1,000.00";
    assertEquals(expected_price_m2_platinum_shopping_cart, actual_price_m2_platinum_shopping_cart);
    System.out.println("Price $1,000.00 in shopping cart is right for m2");

    driver.findElement(By.cssSelector(".fa.fa-times")).click();
    driver.findElement(
            By.cssSelector("div[class='cart-empty'] a[href='https://www.iwdagency.com/extensions/']"));
    driver.findElement(By.cssSelector("ul[class='services'] a[href='/extensions/b2b-wholesale-suite.html']"))
            .click();

    //M2 Bronze M2 Diamond

    driver.findElement(
            By.cssSelector("a[href=\"/extensions/b2b-wholesale-suite.html?editions=magento-2&plans=Diamond\"]"))
            .click();
    wait_for_popup.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".ajaxcart-content")));
    wait_for_popup.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#select_-177328")));

    String actual_price_m2_diamond = driver.findElement(By.cssSelector("#product-price-44")).getText();
    System.out.println(actual_price_m2_diamond);
    String expected_price_m2_diamond = "$2000 /mo";
    assertEquals(expected_price_m2_diamond, actual_price_m2_diamond);
    System.out.println("$2000 /mo was found for m2");

    Thread.sleep(2000);
    driver.findElement(By.cssSelector("#options_105_text")).sendKeys("test.com");
    driver.findElement(By.cssSelector(".button.btn-add-to-cart.left")).click();

    String actual_price_m2_diamond_shopping_cart = driver
            .findElement(By.cssSelector("div[class=\"cart-price\"] span[class=\"price\"]")).getText();
    System.out.println(actual_price_m2_diamond_shopping_cart);
    String expected_price_m2_diamond_shopping_cart = "$2,000.00";
    assertEquals(expected_price_m2_diamond_shopping_cart, actual_price_m2_diamond_shopping_cart);
    System.out.println("Price $2,000.00 in shopping cart is right for m2");

    driver.findElement(By.cssSelector(".fa.fa-times")).click();
    driver.findElement(
            By.cssSelector("div[class='cart-empty'] a[href='https://www.iwdagency.com/extensions/']"));
    driver.findElement(By.cssSelector("ul[class='services'] a[href='/extensions/b2b-wholesale-suite.html']"))
            .click();

}

From source file:javax.portlet.tck.driver.TCKLiferayTestDriver.java

License:Apache License

private List<WebElement> getExclusive() {

    String[] tokens = tcName.split("_");
    String liferayWar = "_WAR_tck" + tokens[0];
    String noV = tcName.replaceFirst("^V[0-9]+", "");
    String portletName = noV.substring(0, noV.lastIndexOf("_"));
    String portletId = portletName + liferayWar;

    String url = baseUrl + page + "?p_p_id=" + portletId + "&p_p_state=exclusive";

    // System.out.println("getExclusive: url = " + url);
    hadToGet = true;//from   www  . j  a  v a  2s.co  m
    exclusive = true;
    driver.get(url);

    WebDriverWait wdw = new WebDriverWait(driver, timeout);
    wdw.until(ExpectedConditions.visibilityOfElementLocated(By.name(tcName)));
    List<WebElement> wels = driver.findElements(By.name(tcName));
    return wels;

}

From source file:javax.portlet.tck.driver.TCKSimpleTestDriver.java

License:Apache License

/**
 * Tries to access the page for the test case. Looks for the page link
 * and clicks it, waiting for the page to load.
 * //from w w w. ja  v  a2s. co  m
 * @return  a list of elements for the TC (should only be one)
 */
protected List<WebElement> accessPage() throws Exception {
    List<WebElement> wels = driver.findElements(By.linkText(page));
    debugLines.add("   Access page, link found: " + !wels.isEmpty() + ", page===" + page + "===");

    if (wels.isEmpty()) {
        // retry through login page
        debugLines.add("   logging in ... ");
        login();
        wels = driver.findElements(By.linkText(page));
        if (wels.isEmpty()) {
            throw new Exception("Page " + page + ": link could not be found.");
        }
    }

    WebElement wel = wels.get(0);
    if (scroll) {
        JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
        javascriptExecutor.executeScript(
                "window.scrollTo(0, (arguments[0].getBoundingClientRect().top + window.pageYOffset) - (window.innerHeight / 2));",
                wel);
    }
    wel.click();
    WebDriverWait wdw = new WebDriverWait(driver, timeout);
    wdw.until(ExpectedConditions.visibilityOfElementLocated(By.name(tcName)));
    wels = driver.findElements(By.name(tcName));
    if (wels.isEmpty()) {
        throw new Exception("For test case " + tcName + ": no elements found.");
    }
    return wels;
}

From source file:javax.portlet.tck.driver.TCKSimpleTestDriver.java

License:Apache License

/**
 * Looks for a link or button that can be clicked for the TC and clicks it if found.
 * /*from w ww. j a va 2  s . co  m*/
 * First looks for a test case setup link or button and clicks it if found. Then it 
 * looks for a test case execution link and clicks it if found. 
 * 
 * @return  web element list containing the test case results.
 * @throws Exception 
 */
@SuppressWarnings("unused")
protected List<WebElement> processClickable(List<WebElement> wels) throws Exception {
    String setupId = tcName + Constants.SETUP_ID;
    String actionId = tcName + Constants.CLICK_ID;
    String resultId = tcName + Constants.RESULT_ID;
    String detailId = tcName + Constants.DETAIL_ID;
    String asyncId = tcName + Constants.ASYNC_ID;
    String notreadyId = tcName + Constants.NOTREADY_ID;
    List<WebElement> tcels = null;

    for (WebElement wel : wels) {
        tcels = wel.findElements(By.id(setupId));
        if (!tcels.isEmpty())
            break;
    }
    debugLines.add("   Setup link found: " + ((tcels != null) && !tcels.isEmpty()));

    // If were dealing with async, make sure the JavaScript is initialized
    List<WebElement> acels = driver.findElements(By.id(asyncId));
    debugLines.add("   Async elements found: " + ((acels != null) && !acels.isEmpty()));
    if (acels != null && !acels.isEmpty()) {
        WebDriverWait wdw = new WebDriverWait(driver, timeout);
        wdw.until(ExpectedConditions.invisibilityOfElementLocated(By.id(notreadyId)));
        debugLines.add("   Async elements are now ready.");
    }

    // Click setup link if found
    if ((tcels != null) && !tcels.isEmpty()) {
        WebElement wel = tcels.get(0);
        if (scroll) {
            JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
            javascriptExecutor.executeScript(
                    "window.scrollTo(0, (arguments[0].getBoundingClientRect().top + window.pageYOffset) - (window.innerHeight / 2));",
                    wel);
        }
        wel.click();
        debugLines.add("   Clicked setup link.");

        WebDriverWait wdw = new WebDriverWait(driver, timeout);

        String expr = "//*[@id='" + resultId + "'] | //*[@id='" + actionId + "']";
        debugLines.add("   xpath string: ===" + expr + "===");

        wdw.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(expr)));
        wels = driver.findElements(By.name(tcName));

        debugLines.add("   Found elements: " + (!wels.isEmpty()));
        List<WebElement> xels = driver.findElements(By.xpath(expr));
        for (WebElement w : xels) {
            debugLines.add("      Element: " + w.getTagName() + ", id=" + w.getAttribute("id"));
        }
    }

    // Now click the action link, if present
    for (WebElement wel : wels) {
        tcels = wel.findElements(By.id(actionId));
        if (!tcels.isEmpty())
            break;
    }
    debugLines.add("   Clickable link found: " + ((tcels != null) && !tcels.isEmpty()));

    if (tcels != null && !tcels.isEmpty()) {
        WebElement wel = tcels.get(0);
        if (scroll) {
            JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
            javascriptExecutor.executeScript(
                    "window.scrollTo(0, (arguments[0].getBoundingClientRect().top + window.pageYOffset) - (window.innerHeight / 2));",
                    wel);
        }
        wel.click();
        WebDriverWait wdw = new WebDriverWait(driver, timeout);
        wdw.until(ExpectedConditions.visibilityOfElementLocated(By.id(resultId)));
        wels = driver.findElements(By.name(tcName));
        if ((wels == null) || wels.isEmpty()) {
            throw new Exception("Test case " + tcName + " failed. No results after action link click.");
        }
    }

    return wels;
}

From source file:javax.portlet.tck.driver.TCKSimpleTestDriver.java

License:Apache License

/**
 * Looks for an async element on the page. The async element will be filed in 
 * with results by the test case JavaScript code, which runs asynchronously.  
 * /* w  w  w . jav a2 s. c o m*/
 * If an async element is found, this function waits the timeout period to 
 * let the async test case code update the results.
 * 
 * @return  <code>true</code> if async was handled; <code>false</code> otherwise.
 * @throws Exception 
 */
protected boolean processAsync() throws Exception {
    String asyncId = tcName + Constants.ASYNC_ID;
    String resultId = tcName + Constants.RESULT_ID;

    List<WebElement> tcels = null;

    tcels = driver.findElements(By.id(asyncId));

    debugLines.add("   Element with async id=" + asyncId + " found: " + !tcels.isEmpty());

    if (tcels.isEmpty()) {
        // no async element
        return false;
    }

    WebDriverWait wdw = new WebDriverWait(driver, timeout);
    wdw.until(ExpectedConditions.visibilityOfElementLocated(By.id(resultId)));

    return true;
}

From source file:javax.portlet.tck.driver.TCKTestDriver.java

License:Apache License

/**
 * Tries to access the page for the test case. Looks for the page link
 * and clicks it, waiting for the page to load.
 * /*from   www.j a v  a2s .com*/
 * @return  a list of elements for the TC (should only be one)
 */
protected List<WebElement> accessPage() throws Exception {
    List<WebElement> wels = driver.findElements(By.linkText(page));
    debugLines.add("   Access page, link found: " + !wels.isEmpty() + ", page===" + page + "===");

    if (wels.isEmpty()) {
        // retry through login page
        debugLines.add("accessPage: debugLines:   logging in ... ");
        login();
        wels = driver.findElements(By.linkText(page));
        if (wels.isEmpty()) {
            throw new Exception("Page " + page + ": link could not be found.");
        }
    }

    WebElement wel = wels.get(0);
    if (scroll) {
        JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
        javascriptExecutor.executeScript(
                "window.scrollTo(0, (arguments[0].getBoundingClientRect().top + window.pageYOffset) - (window.innerHeight / 2));",
                wel);
    }
    click(wel);
    WebDriverWait wdw = new WebDriverWait(driver, timeout);
    wdw.until(ExpectedConditions.visibilityOfElementLocated(By.name(tcName)));
    wels = driver.findElements(By.name(tcName));
    if (wels.isEmpty()) {
        throw new Exception("For test case " + tcName + ": no elements found.");
    }
    return wels;
}

From source file:javax.portlet.tck.driver.TCKTestDriver.java

License:Apache License

/**
 * Looks for a link or button that can be clicked for the TC and clicks it if found.
 * /*w w w  .  j  a va 2s .c  om*/
 * First looks for a test case setup link or button and clicks it if found. Then it 
 * looks for a test case execution link and clicks it if found. 
 * 
 * @return  web element list containing the test case results.
 * @throws Exception 
 */
@SuppressWarnings("unused")
protected List<WebElement> processClickable(List<WebElement> wels) throws Exception {
    String setupId = tcName + Constants.SETUP_ID;
    String actionId = tcName + Constants.CLICK_ID;
    String resultId = tcName + Constants.RESULT_ID;
    String detailId = tcName + Constants.DETAIL_ID;
    String asyncId = tcName + Constants.ASYNC_ID;
    String notreadyId = tcName + Constants.NOTREADY_ID;
    List<WebElement> tcels = null;

    for (WebElement wel : wels) {
        tcels = wel.findElements(By.id(setupId));
        if (!tcels.isEmpty())
            break;
    }
    debugLines.add("   Setup link found: " + ((tcels != null) && !tcels.isEmpty()));

    // If were dealing with async, make sure the JavaScript is initialized
    List<WebElement> acels = driver.findElements(By.id(asyncId));
    debugLines.add("   Async elements found: " + ((acels != null) && !acels.isEmpty()));
    if (acels != null && !acels.isEmpty()) {
        WebDriverWait wdw = new WebDriverWait(driver, timeout);
        wdw.until(ExpectedConditions.invisibilityOfElementLocated(By.id(notreadyId)));
        debugLines.add("   Async elements are now ready.");
    }

    // Click setup link if found
    if ((tcels != null) && !tcels.isEmpty()) {
        WebElement wel = tcels.get(0);
        if (scroll) {
            JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
            javascriptExecutor.executeScript(
                    "window.scrollTo(0, (arguments[0].getBoundingClientRect().top + window.pageYOffset) - (window.innerHeight / 2));",
                    wel);
        }
        try {
            wel.click();
        } catch (StaleElementReferenceException e) {
            System.out.println("setup link: " + e.getClass().getName()
                    + " caught when trying to use WebElements found with " + tcName);
            wels = driver.findElements(By.name(tcName));
            for (WebElement welly : wels) {
                tcels = welly.findElements(By.id(setupId));
                if (!tcels.isEmpty())
                    break;
            }
            wel = tcels.get(0);
            wel.click();
        }
        debugLines.add("   Clicked setup link.");

        WebDriverWait wdw = new WebDriverWait(driver, timeout);

        String expr = "//*[@id='" + resultId + "'] | //*[@id='" + actionId + "']";
        debugLines.add("   xpath string: ===" + expr + "===");

        wdw.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(expr)));
        wels = driver.findElements(By.name(tcName));

        debugLines.add("   Found elements: " + (!wels.isEmpty()));
        List<WebElement> xels = driver.findElements(By.xpath(expr));
        for (WebElement w : xels) {
            debugLines.add("      Element: " + w.getTagName() + ", id=" + w.getAttribute("id"));
        }
    }

    // Now click the action link, if present
    for (WebElement wel : wels) {
        tcels = wel.findElements(By.id(actionId));
        if (!tcels.isEmpty())
            break;
    }
    debugLines.add("   Clickable link found: " + ((tcels != null) && !tcels.isEmpty()));

    if (tcels != null && !tcels.isEmpty()) {
        WebElement wel = tcels.get(0);
        if (scroll) {
            JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
            javascriptExecutor.executeScript(
                    "window.scrollTo(0, (arguments[0].getBoundingClientRect().top + window.pageYOffset) - (window.innerHeight / 2));",
                    wel);
        }
        try {
            wel.click();
        } catch (StaleElementReferenceException e) {
            System.out.println("action link: " + e.getClass().getName()
                    + " caught when trying to use WebElements found with " + tcName);
            wels = driver.findElements(By.name(tcName));
            for (WebElement welly : wels) {
                tcels = welly.findElements(By.id(actionId));
                if (!tcels.isEmpty())
                    break;
            }
            wel = tcels.get(0);
            wel.click();
        }
        WebDriverWait wdw = new WebDriverWait(driver, timeout);
        wdw.until(ExpectedConditions.visibilityOfElementLocated(By.id(resultId)));
        wels = driver.findElements(By.name(tcName));
        if ((wels == null) || wels.isEmpty()) {
            throw new Exception("Test case " + tcName + " failed. No results after action link click.");
        }
    }

    return wels;
}