Example usage for org.openqa.selenium WebElement isDisplayed

List of usage examples for org.openqa.selenium WebElement isDisplayed

Introduction

In this page you can find the example usage for org.openqa.selenium WebElement isDisplayed.

Prototype

boolean isDisplayed();

Source Link

Document

Is this element displayed or not?

Usage

From source file:influent.selenium.tests.FlowViewTests.java

License:MIT License

@Test
public void initBehaviorTest() {

    SeleniumUtils.navigateToTab(driver, SeleniumUtils.TAB_FLOW);

    List<WebElement> elements = driver.findElements(By.className("column"));

    for (WebElement elem : elements) {
        if (elem.isDisplayed()) {
            throw new AssertionError("Initial State is not a clear screen");
        }/*from   w  ww.  j a  v a  2 s .  co  m*/
    }
}

From source file:influent.selenium.tests.OperationsBarTest.java

License:MIT License

@Test
public void transactionsToAccountsTest() {

    SeleniumUtils.navigateToTab(driver, SeleniumUtils.TAB_TRANSACTIONS);

    List<WebElement> elements = driver.findElements(By.id("influent-view-toolbar-search-input"));

    WebElement element = SeleniumUtils.getDisplayedElement(elements);

    if (element == null) {
        throw new AssertionError("Couldn't find: influent-view-toolbar-search-input element");
    }//from  w w w . j a va 2  s .  c  om

    element.sendKeys("from:a.loan.b311953 to:a.loan.b311953 datatype:loan");
    elements = driver.findElements(By.className("infGoSearch"));
    element = SeleniumUtils.getDisplayedElement(elements);

    if (element == null) {
        throw new AssertionError("Couldn't find: infGoSearch element");
    }

    element.click();

    element = (new WebDriverWait(driver, 120))
            .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".infSearchResultCounts")));
    if (element == null) {
        throw new AssertionError("Couldn't find any search results");
    }

    // Select first two search results with a data id
    List<WebElement> dataIdElements = driver.findElements(By.cssSelector(".selectSingleResult input"));
    int elementsToSelect = 2;
    int elementsSelected = 0;
    for (WebElement e : dataIdElements) {
        e.click();
        elementsSelected++;
        if (elementsToSelect == elementsSelected) {
            break;
        }
    }

    List<WebElement> switchViewButtons = driver.findElements(By.className("btn-operationsBar"));

    WebElement switchToAccountsButton = null;
    WebElement switchToFlowButton = null;
    WebElement switchToTransactionsButton = null;
    for (WebElement e : switchViewButtons) {
        if (!e.isDisplayed()) {
            continue;
        }
        String switchTo = e.getAttribute("switchesto");
        if (switchTo.equals(SeleniumUtils.FLOW_NAME)) {
            switchToFlowButton = e;
            if (switchToTransactionsButton == null || switchToAccountsButton == null) {
                throw new AssertionError("Operations bar buttons are not in the correct order");
            }
        } else if (switchTo.equals(SeleniumUtils.ACCOUNTS_NAME)) {
            switchToAccountsButton = e;
            if (switchToFlowButton != null && switchToTransactionsButton != null) {
                throw new AssertionError("Operations bar buttons are not in the correct order");
            }
        } else if (switchTo.equals("")) {
            switchToTransactionsButton = e;
            if (switchToFlowButton != null || switchToAccountsButton == null) {
                throw new AssertionError("Operations bar buttons are not in the correct order");
            }
        }
    }

    if (switchToFlowButton == null) {
        throw new AssertionError("Couldn't find: Button to switch to flow view in infOperationsBar");
    }
    if (switchToAccountsButton == null) {
        throw new AssertionError("Couldn't find: Button to switch to accounts view in infOperationsBar");
    }

    switchToAccountsButton.click();
    WebElement firstTransactionResult = (new WebDriverWait(driver, 120))
            .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".accountsSearchResult")));
    if (firstTransactionResult == null) {
        throw new AssertionError(
                "Couldn't find any transaction results.   Either timed out or search returned no transactions!");
    }
}

From source file:influent.selenium.tests.OperationsBarTest.java

License:MIT License

@Test
public void transacitonsToFlowTest() {

    SeleniumUtils.navigateToTab(driver, SeleniumUtils.TAB_TRANSACTIONS);

    List<WebElement> elements = driver.findElements(By.id("influent-view-toolbar-search-input"));

    WebElement element = SeleniumUtils.getDisplayedElement(elements);

    if (element == null) {
        throw new AssertionError("Couldn't find: influent-view-toolbar-search-input element");
    }//w w w.j  av a 2  s  .c  o m

    element.sendKeys("from:a.loan.b311953 to:a.loan.b311953 datatype:loan");
    elements = driver.findElements(By.className("infGoSearch"));
    element = SeleniumUtils.getDisplayedElement(elements);

    if (element == null) {
        throw new AssertionError("Couldn't find: infGoSearch element");
    }

    element.click();

    element = (new WebDriverWait(driver, 120))
            .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".infSearchResultCounts")));
    if (element == null) {
        throw new AssertionError("Couldn't find any search results");
    }

    // Select first two search results with a data id
    List<WebElement> dataIdElements = driver.findElements(By.cssSelector(".selectSingleResult input"));
    int elementsToSelect = 2;
    int elementsSelected = 0;
    for (WebElement e : dataIdElements) {
        e.click();
        elementsSelected++;
        if (elementsToSelect == elementsSelected) {
            break;
        }
    }

    List<WebElement> switchViewButtons = driver.findElements(By.className("btn-operationsBar"));
    WebElement switchToAccountsButton = null;
    WebElement switchToFlowButton = null;
    for (WebElement e : switchViewButtons) {
        if (!e.isDisplayed()) {
            continue;
        }
        String switchTo = e.getAttribute("switchesto");
        if (switchTo.equals(SeleniumUtils.FLOW_NAME)) {
            switchToFlowButton = e;
        } else if (switchTo.equals(SeleniumUtils.ACCOUNTS_NAME)) {
            switchToAccountsButton = e;
        }
    }
    if (switchToFlowButton == null) {
        throw new AssertionError("Couldn't find: Button to switch to flow view in infOperationsBar");
    }
    if (switchToAccountsButton == null) {
        throw new AssertionError("Couldn't find: Button to switch to accounts view in infOperationsBar");
    }

    switchToFlowButton.click();

    WebElement firstTransactionResult = (new WebDriverWait(driver, 120))
            .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".insideBaseballCard")));
    if (firstTransactionResult == null) {
        throw new AssertionError(
                "Couldn't find any transaction results.   Either timed out or search returned no transactions!");
    }
}

From source file:influent.selenium.tests.TransactionsTableTest.java

License:MIT License

@Test
public void tableMultiResultsOnePageTest() {
    getFlowViewResults("name:\"joe\" countryCode:\"PE\" datatype:loan matchtype:\"any\"", 2, true);

    //Test a card that has no transactions
    List<WebElement> elements = driver.findElements(By.className("baseballcardContainer"));

    WebElement card1 = null;/*from   ww w .j  av  a  2 s . c o  m*/
    WebElement card2 = null;
    for (WebElement webElement : elements) {
        if (webElement.isDisplayed()) {
            if (card1 == null) {
                card1 = webElement;
            } else if (card2 == null) {
                card2 = webElement;
                break;
            }
        }
    }

    if (card1 == null || card2 == null) {
        throw new AssertionError("Couldn't find: baseballcardContainer element");
    }

    card2.click();

    //Wait a few seconds for page to load. This is needed despite waiting for presence of element
    try {
        Thread.sleep(2000);
    } catch (Exception e) {
    }

    WebElement element = (new WebDriverWait(driver, 120))
            .until(ExpectedConditions.presenceOfElementLocated(By.id("transactions-table-pagination-label")));

    assertEquals("Showing 1 - 6 of 6", element.getText());

    //check page buttons disabled
    element = driver.findElement(By.id("transactions-previous-page"));
    assertEquals("btn btn-default disabled", element.getAttribute("class"));

    element = driver.findElement(By.id("transactions-next-page"));
    assertEquals("btn btn-default disabled", element.getAttribute("class"));

    //check rows
    element = driver.findElement(By.id("transactions-table-body"));
    elements = element.findElements(By.tagName("tr"));

    assertEquals(6, elements.size());

    element = SeleniumUtils.getDisplayedElement(elements);
    assertEquals("1 2012-11-30 status: paid; repayed: 181.65 (in PEN) - $68.12", element.getText());

    //Test highlighting      
    element = card2.findElement(By.cssSelector("[title='highlight flow']"));
    element.click();

    //Wait a few seconds for page to load. This is needed despite waiting for presence of element
    try {
        Thread.sleep(2000);
    } catch (Exception e) {
    }

    element = driver.findElement(By.id("transactions-table-body"));
    elements = element.findElements(By.tagName("tr"));

    for (WebElement webElement : elements) {
        assertEquals(true, webElement.getAttribute("class").contains("transactionsHighlight"));
    }

    //Test Export
    element = driver.findElement(By.id("exportTransactions"));
    element.click();

    //Wait a few seconds for page to load. This is needed despite waiting for presence of element
    try {
        Thread.sleep(2000);
    } catch (Exception e) {
    }

    element = (new WebDriverWait(driver, 120))
            .until(ExpectedConditions.presenceOfElementLocated(By.id("dialog-confirm")));
    assertEquals("Export successful!", element.getText());

    elements = driver.findElements((By.className("ui-button")));
    for (WebElement elem : elements) {
        elem.findElement(By.className("ui-button-text"));
        if (elem != null && elem.getText().equals("Ok")) {
            elem.click();
        }
    }

    try {
        Thread.sleep(2000);
    } catch (Exception e) {
    }

    //Test table row hyperlinks
    element = driver.findElement(By.id("transactions-table-body"));
    elements = element.findElements(By.tagName("tr"));
    element = SeleniumUtils.getDisplayedElement(elements);
    element.click();

    element = (new WebDriverWait(driver, 120))
            .until(ExpectedConditions.presenceOfElementLocated(By.className("simpleSearchSummary")));
    assertEquals("Showing 1 - 5 of 5 results", element.getText());
}

From source file:influent.selenium.tests.TransactionsTableTest.java

License:MIT License

@Test
public void tableMultiResultsMultiPageTest() {

    ///////////////////////////////////////////////////////////////
    //Set Up: Select a card that has 100+ transactions where we can
    //highlight some of those rows.
    ///////////////////////////////////////////////////////////////
    getFlowViewResults("name:\"joe\" countryCode:\"PE\" datatype:loan matchtype:\"any\"", 1, false);

    List<WebElement> elements = driver.findElements(By.className("baseballcardContainer"));

    WebElement card1 = null;/*from  www  .  jav a 2  s .co m*/
    for (WebElement webElement : elements) {
        if (webElement.isDisplayed()) {
            if (card1 == null) {
                card1 = webElement;
                break;
            }
        }
    }

    if (card1 == null) {
        throw new AssertionError("Couldn't find: baseballcardContainer element");
    }

    //Click card so add to file buttons appear
    card1.click();

    //Wait a few seconds for page to load. This is needed despite waiting for presence of element
    try {
        Thread.sleep(2000);
    } catch (Exception e) {
    }

    WebElement element = (new WebDriverWait(driver, 120)).until(ExpectedConditions
            .presenceOfElementLocated(By.cssSelector("[title='branch right to outflowing destinations']")));

    element.click();

    //Wait a few seconds for page to load. This is needed despite waiting for presence of element
    try {
        Thread.sleep(2000);
    } catch (Exception e) {
    }

    elements = driver.findElements(By.className("baseballcardContainer"));

    //WebElement card1 = null;
    WebElement lender = null;
    int x = 0;
    for (WebElement webElement : elements) {
        if (webElement.getLocation().getX() > x) {
            x = webElement.getLocation().getX();
            lender = webElement;
        }
    }

    lender.click();

    //Wait a few seconds for page to load. This is needed despite waiting for presence of element
    try {
        Thread.sleep(2000);
    } catch (Exception e) {
    }

    ///////////////////////////////////////////////////////////////
    //We should now have a transactions table with 100 transactions
    ///////////////////////////////////////////////////////////////

    //Check results summary label
    element = (new WebDriverWait(driver, 120))
            .until(ExpectedConditions.presenceOfElementLocated(By.id("transactions-table-pagination-label")));
    assertEquals("Showing 1 - 100 of 111", element.getText());

    //check previous buttons disabled
    element = driver.findElement(By.id("transactions-previous-page"));
    assertEquals("btn btn-default disabled", element.getAttribute("class"));

    //check previous buttons enabled
    element = driver.findElement(By.id("transactions-next-page"));
    assertEquals("btn btn-default", element.getAttribute("class"));

    //check rows
    element = driver.findElement(By.id("transactions-table-body"));
    elements = element.findElements(By.tagName("tr"));
    assertEquals(100, elements.size());

    //test next button
    element = driver.findElement(By.id("transactions-next-page"));
    element.click();

    //Wait a few seconds for new table to load. We can't use ExpectedConditions.presenceOfElementLocated since they are already present
    try {
        Thread.sleep(2000);
    } catch (Exception e) {
    }

    //Check results summary label
    element = (new WebDriverWait(driver, 120))
            .until(ExpectedConditions.presenceOfElementLocated(By.id("transactions-table-pagination-label")));
    assertEquals("Showing 101 - 111 of 111", element.getText());

    //check previous buttons enabled
    element = driver.findElement(By.id("transactions-previous-page"));
    assertEquals("btn btn-default", element.getAttribute("class"));

    //check previous buttons disabled
    element = driver.findElement(By.id("transactions-next-page"));
    assertEquals("btn btn-default disabled", element.getAttribute("class"));

    //check rows
    element = driver.findElement(By.id("transactions-table-body"));
    elements = element.findElements(By.tagName("tr"));
    assertEquals(11, elements.size());

    ///////////////////////////////////////////////////////////////
    //Test highlighting
    ///////////////////////////////////////////////////////////////

    //Setup
    //find card again
    element = driver.findElement(By.className("file"));
    card1 = element.findElement(By.className("baseballcardContainer"));

    //Click card again so branch right button appears
    card1.click();

    //Wait a few seconds for page to load. This is needed despite waiting for presence of element
    try {
        Thread.sleep(2000);
    } catch (Exception e) {
    }

    element = card1.findElement(By.cssSelector("[title='highlight flow']"));
    element.click();

    //Wait a few seconds for page to load. This is needed despite waiting for presence of element
    try {
        Thread.sleep(2000);
    } catch (Exception e) {
    }

    //Find lender again
    elements = driver.findElements(By.className("baseballcardContainer"));

    //WebElement card1 = null;
    lender = null;
    x = 0;
    for (WebElement webElement : elements) {
        if (webElement.getLocation().getX() > x) {
            x = webElement.getLocation().getX();
            lender = webElement;
        }
    }

    lender.click();
    //Wait a few seconds for new table to load. We can't use ExpectedConditions.presenceOfElementLocated since they are already present
    try {
        Thread.sleep(2000);
    } catch (Exception e) {
    }

    //We should now have transactions from card2 highlighted in lender's transactions table

    //Check we have the correct number of highlighted rows
    element = driver.findElement(By.id("transactions-table-body"));
    elements = element.findElements(By.tagName("tr"));
    int highlightCount = 0;
    for (WebElement webElement : elements) {
        if (webElement.getAttribute("class") != null && webElement.getAttribute("class").length() >= 21
                && webElement.getAttribute("class").substring(0, 21).equals("transactionsHighlight")) {
            highlightCount++;
        }
    }

    assertEquals(5, highlightCount);

    //Check the first highlighted row is correct
    element = element.findElement(By.className("transactionsHighlight-0"));
    assertEquals("31 2012-11-30 status: paid; repayed: 181.65 (in PEN) $68.12 -", element.getText());

    //Test highlighting on next page
    element = driver.findElement(By.id("transactions-next-page"));
    element.click();

    //Wait a few seconds for new table to load. We can't use ExpectedConditions.presenceOfElementLocated since they are already present
    try {
        Thread.sleep(2000);
    } catch (Exception e) {
    }

    //Check we have the correct number of highlighted rows
    element = driver.findElement(By.id("transactions-table-body"));
    elements = element.findElements(By.tagName("tr"));
    highlightCount = 0;
    for (WebElement webElement : elements) {
        if (webElement.getAttribute("class") != null && webElement.getAttribute("class").length() >= 21
                && webElement.getAttribute("class").substring(0, 21).equals("transactionsHighlight")) {
            highlightCount++;
        }
    }

    assertEquals(1, highlightCount);

    //Check the first highlighted row is correct
    element = element.findElement(By.className("transactionsHighlight-1"));
    assertEquals("104 2012-05-29 status: paid; loan: 1k (in PEN) - $375.00", element.getText());
}

From source file:influent.selenium.tests.TransactionsTableTest.java

License:MIT License

@Test
public void transactionsViewButtonTest() {
    getFlowViewResults("name:\"joe\" countryCode:\"PE\" datatype:loan matchtype:\"any\"", 1, false);

    //Test a card that has no transactions
    List<WebElement> elements = driver.findElements(By.className("baseballcardContainer"));

    WebElement card1 = null;/*from   ww w  .  j  av  a2s. c o m*/
    for (WebElement webElement : elements) {
        if (webElement.isDisplayed()) {
            if (card1 == null) {
                card1 = webElement;
            }
        }
    }

    if (card1 == null) {
        throw new AssertionError("Couldn't find: baseballcardContainer element");
    }

    card1.click();

    //Wait a few seconds for page to load. This is needed despite waiting for presence of element
    try {
        Thread.sleep(2000);
    } catch (Exception e) {
    }

    WebElement element = (new WebDriverWait(driver, 120))
            .until(ExpectedConditions.presenceOfElementLocated(By.id("transactions-table-pagination-label")));

    element = driver.findElement(By.id("transactions-button"));

    element.click();

    try {
        Thread.sleep(2000);
    } catch (Exception e) {
    }

    element = (new WebDriverWait(driver, 120))
            .until(ExpectedConditions.presenceOfElementLocated(By.className("simpleSearchSummary")));
    assertEquals("Showing 1 - 6 of 6 results", element.getText());

    try {
        Thread.sleep(2000);
    } catch (Exception e) {
    }

    elements = driver.findElements(By.className("simpleSearchResultText"));
    element = SeleniumUtils.getDisplayedElement(elements);
    element = element.findElement(By.className("simpleSearchResultFieldValue"));
    assertEquals("To: a.partner.p71-438363  From: a.loan.b438363", element.getText());
}

From source file:influent.selenium.util.SeleniumUtils.java

License:MIT License

/**
 * Returns the first element that is being displayed from the list of given elements
 * @param elements/*from w ww . j av  a2 s .  co  m*/
 * the list of given elements
 * @return
 * the first element that is being displayed from the list of given elements
 */
static public WebElement getDisplayedElement(List<WebElement> elements) {
    WebElement element = null;
    for (WebElement webElement : elements) {
        if (webElement.isDisplayed()) {
            element = webElement;
            break;
        }
    }
    return element;
}

From source file:info.magnolia.integrationtests.uitest.AbstractMagnoliaUITest.java

License:Open Source License

/**
 * Tries to retrieve requested element./*from   w w w. jav  a 2s  . c  o  m*/
 *
 * @param by locator of an element
 * @return the searched specified element or a NonExistingWebElement in case it couldn't be found.
 */
protected WebElement getElement(final By by) {
    WebElement element;
    try {
        // will loop and try to retrieve the specified element until found or it times out.
        element = new WebDriverWait(driver, timeout).until(new ExpectedCondition<WebElement>() {

            @Override
            public WebElement apply(WebDriver d) {
                try {
                    WebElement element = d.findElement(by);
                    if (element.isDisplayed()) {
                        takeScreenshot(by.toString());
                        return element;
                    }
                    takeScreenshot(by.toString() + "_notDisplayed");
                    return null;
                } catch (NoSuchElementException e) {
                    takeScreenshot(by.toString() + "_notFound");
                    return null;
                }
            }
        });
    } catch (TimeoutException e) {
        log.debug("Could not retrieve element by path {}. Got: {}", by, e);
        // not found within the time limit - assume that element is not existing
        element = new NonExistingWebElement(by.toString());
    } catch (StaleElementReferenceException s) {
        // re-trying on StaleElementReferenceExceptions: see http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp
        log.info("{} when accessing element {} - trying again", s.toString(), by);
        element = getElement(by);
    }
    return element;
}

From source file:info.magnolia.integrationtests.uitest.AbstractMagnoliaUITest.java

License:Open Source License

private WebElement doGetTabElement(String tabCaption, String... parentTitles) {
    final WebElement tabLabel = getPotentiallyHiddenTab(tabCaption, parentTitles);
    // Either tab does not exist at all (yield non-existent element) or it is displayed and ready to shown
    if (!isExisting(tabLabel) || tabLabel.isDisplayed()) {
        return tabLabel;
    } else {/* www  .  jav a 2  s.co m*/
        final String parentXPath = buildPathFromTitles(parentTitles);
        final By byPopupControl = getElementLocatorByXpath("%s%s", parentXPath,
                "//*[contains(@class, 'hidden-tabs-popup-button') and not(contains(@style, 'display: none'))]");
        final WebElement popupControl = getElement(byPopupControl);
        popupControl.click();
        final By byHiddenTab = getElementLocatorByXpath(
                "//*[contains(@class, 'hidden-tabs-menu')]//*[contains(@class, 'menu-item') and text() = '%s']",
                tabCaption);
        return getElement(byHiddenTab);
    }
}

From source file:info.magnolia.integrationtests.uitest.AbstractMagnoliaUITest.java

License:Open Source License

/**
 * Shell app is considered loaded once both shellapps viewport and the given shell-app meet the following conditions:
 * <ul>/* w  ww .j a  va  2  s.c om*/
 * <li>element is displayed</li>
 * <li>element transition is complete (no more transition related property in inline-styles).</li>
 * </ul>
 *
 * @param shellAppType the {@link ShellApp} to wait for.
 */
protected ExpectedCondition<WebElement> shellAppIsLoaded(final ShellApp shellAppType) {
    // shell app should be displayed (block) and non-transitioning (opacity cleared upon transition complete)
    final WebElement shellApp = driver.findElement(
            By.xpath(String.format("//div[contains(@class, 'v-viewport-shellapps')]/*[contains(@class, '%s')]",
                    shellAppType.getClassName())));
    final WebElement viewport = getElement(By.className("v-viewport-shellapps"));
    return new ExpectedCondition<WebElement>() {

        @Override
        public WebElement apply(WebDriver driver) {
            boolean viewportTransitioning = viewport.getAttribute("style").contains("transition");
            boolean viewportDisplayed = viewport.isDisplayed();
            boolean shellAppTransitioning = shellApp.getAttribute("style").contains("transition");
            boolean shellAppDisplayed = shellApp.isDisplayed();
            return !viewportTransitioning && viewportDisplayed && !shellAppTransitioning && shellAppDisplayed
                    ? shellApp
                    : null;
        }
    };
}