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:com.screenslicer.core.scrape.QueryKeyword.java

License:Open Source License

private static void handleIframe(RemoteWebDriver driver) throws ActionFailed {
    List<WebElement> iframes = null;
    try {/*w ww .j  a  v a 2  s. c  om*/
        iframes = driver.findElementsByTagName("iframe");
    } catch (Throwable t) {
        throw new ActionFailed(t);
    }
    try {
        for (WebElement iframe : iframes) {
            try {
                Dimension dim = iframe.getSize();
                if (iframe.isDisplayed() && (dim.getHeight() * dim.getWidth()) > MIN_IFRAME_AREA) {
                    String src = iframe.getAttribute("src");
                    if (!CommonUtil.isEmpty(src) && src.indexOf("://") > -1 && src.indexOf("?") > -1) {
                        String origHandle = null;
                        String origUrl = null;
                        String newHandle = null;
                        try {
                            origHandle = driver.getWindowHandle();
                            origUrl = driver.getCurrentUrl();
                            newHandle = Util.newWindow(driver);
                        } catch (Throwable t) {
                            Log.exception(t);
                            throw new ActionFailed(t);
                        }
                        boolean undo = false;
                        try {
                            Util.get(driver, src, true);
                            driver.executeScript(
                                    "document.getElementsByTagName('html')[0].style.overflow='scroll';");
                            Util.driverSleepShort();
                            if (driver.findElementByTagName("body").getText().length() < MIN_SOURCE_DIFF) {
                                undo = true;
                            }
                        } catch (Throwable t) {
                            Log.exception(t);
                            undo = true;
                        }
                        try {
                            if (undo) {
                                if (origHandle.equals(newHandle)) {
                                    if (!driver.getCurrentUrl().equals(origUrl)) {
                                        try {
                                            driver.navigate().back();
                                        } catch (Throwable t) {
                                            Log.exception(t);
                                        }
                                    }
                                    if (!driver.getCurrentUrl().equals(origUrl)) {
                                        driver.get(origUrl);
                                    }
                                } else {
                                    Util.cleanUpNewWindows(driver, origHandle);
                                }
                            } else {
                                Util.cleanUpNewWindows(driver, newHandle);
                                break;
                            }
                        } catch (Throwable t) {
                            Log.exception(t);
                            throw new ActionFailed(t);
                        }
                    }
                }
            } catch (Throwable t) {
                Log.exception(t);
                continue;
            }
        }
    } catch (Throwable t) {
        Log.exception(t);
        throw new ActionFailed(t);
    }
}

From source file:com.seleniumtests.uipage.htmlelements.HtmlElement.java

License:Apache License

/**
 * returns an element depending on configured index
 * @param allElements//w w w . jav  a2 s.  c om
 */
private WebElement getElementByIndex(List<WebElement> allElements) {
    if (elementIndex == FIRST_VISIBLE) {
        for (WebElement el : allElements) {
            if (el.isDisplayed()) {
                return el;
            }
        }
        throw new WebDriverException("no visible element has been found for " + by.toString());
    } else if (elementIndex < 0) {
        return allElements.get(allElements.size() + elementIndex);
    } else {
        if (elementIndex == null) {
            elementIndex = 0;
        }
        return allElements.get(elementIndex);
    }
}

From source file:com.seleniumtests.uipage.htmlelements.HtmlElement.java

License:Apache License

/**
* Make element visible. Sometimes useful when real elements are backed by an image element
*///  w  w w  .j av a 2  s  .  c o  m
protected void makeWebElementVisible(WebElement element) {
    if (SeleniumTestsContextManager.isWebTest()) {
        if (element.isDisplayed()) {
            return;
        }
        try {

            if (element.getLocation().x < 0) {
                Long viewportHeight = (Long) ((JavascriptExecutor) driver)
                        .executeScript("return document.documentElement.clientHeight");
                Integer heightPosition = element.getLocation().y > viewportHeight
                        ? element.getLocation().y - viewportHeight.intValue()
                        : element.getLocation().y;
                changeCssAttribute(element, "left", "20px");
                changeCssAttribute(element, "top", heightPosition + "px");
                changeCssAttribute(element, "position", "inherit");
            }
            if (element.getAttribute("style").toLowerCase().replace(" ", "").contains("display:none")) {
                changeCssAttribute(element, "display", "block");
            }
            //            changeCssAttribute(element, "clip", "auto");
            changeCssAttribute(element, "zIndex", "100000");
        } catch (Exception e) {
            return;
        }

        // wait for element to be displayed
        try {
            new WebDriverWait(driver, 1).until(ExpectedConditions.visibilityOf(element));
        } catch (ElementNotVisibleException e) {
            TestLogging.info(String.format("element %s not visible", element));
        } catch (Exception e) {
            logger.warn("Could not make element visible", e);
        }

    }
}

From source file:com.sios.stc.coseng.test.Base.java

License:Open Source License

public void acceptSslCertificate(final WebDriver webDriver) throws InterruptedException {
    // For Internet Explorer
    if (!browser.equals(Browser.FIREFOX) || !browser.equals(Browser.CHROME)) {
        final Boolean title = webDriver.getTitle().equals("Certificate Error: Navigation Blocked");
        final int count = webDriver.findElements(By.id("overridelink")).size();
        if (title && (count == 1)) {
            final WebElement overrideLink = webDriver.findElement(By.id("overridelink"));
            if (overrideLink.isDisplayed()) {
                new Actions(driver).moveToElement(overrideLink).click().build().perform();
            }/*from ww w.ja  va  2s .  c  o  m*/
        }
    }
}

From source file:com.sios.stc.coseng.test.Bing.java

License:Open Source License

@Test(description = "Verify connect to Google")
public void connect() throws Exception {

    driver.get("http://www.bing.com");
    saveScreenshot(driver, "landingpage-bing");
    final WebElement bingSearchInput = driver.findElement(By.id("sbox"));
    Assert.assertTrue(bingSearchInput.isDisplayed());
}

From source file:com.sios.stc.coseng.test.Google.java

License:Open Source License

@Test(description = "Verify connect to Google")
public void connect() throws Exception {

    driver.get("http://www.google.com");
    saveScreenshot(driver, "landingpage-google");
    final WebElement googleSearchInput = driver.findElement(By.id("gbqfq"));
    Assert.assertTrue(googleSearchInput.isDisplayed());
}

From source file:com.sios.stc.coseng.test.User.java

License:Open Source License

@Test(description = "Verity initial login, change password.", dataProvider = "credentials")
public void firstLogin(final String username, final String password, final String newPassword)
        throws Exception {

    User.log.log(Level.INFO,/*from   www  .  j  a  va 2 s . co m*/
            "Username: " + username + " Password: " + password + " NewPassword: " + newPassword);

    driver.get(baseUrl + "/ui/#/login");

    final WebDriverWait wait = new WebDriverWait(driver, 10);
    final Actions actions = new Actions(driver);

    WebElement weUsername = driver.findElement(By.name("userName"));
    WebElement wePassword = driver.findElement(By.name("password"));
    WebElement weSubmit = driver.findElement(By.className("login_submit_button"));

    actions.moveToElement(weUsername).sendKeys(weUsername, username).moveToElement(wePassword)
            .sendKeys(wePassword, password).click(weSubmit).build().perform();

    WebElement weNewPassword = driver.findElement(By.name("newpassword"));
    WebElement weRePassword = driver.findElement(By.name("repassword"));
    final WebElement weCancel = driver.findElement(By.linkText("Cancel"));

    wait.until(ExpectedConditions.visibilityOf(weNewPassword));

    actions.moveToElement(weNewPassword).sendKeys(weNewPassword, newPassword).build().perform();

    actions.moveToElement(weRePassword).sendKeys(weRePassword, newPassword).build().perform();

    actions.click(weCancel).build().perform();

    weUsername = driver.findElement(By.name("userName"));
    wePassword = driver.findElement(By.name("password"));
    weSubmit = driver.findElement(By.className("login_submit_button"));

    wait.until(ExpectedConditions.visibilityOf(weUsername));

    actions.moveToElement(weUsername).sendKeys(weUsername, username).moveToElement(wePassword)
            .sendKeys(wePassword, password).click(weSubmit).build().perform();

    weNewPassword = driver.findElement(By.name("newpassword"));
    weRePassword = driver.findElement(By.name("repassword"));
    final WebElement weOk = driver.findElement(By.linkText("OK"));

    wait.until(ExpectedConditions.visibilityOf(weNewPassword));

    actions.moveToElement(weNewPassword).sendKeys(weNewPassword, newPassword).moveToElement(weRePassword)
            .sendKeys(weRePassword, newPassword).click(weOk).build().perform();

    final WebElement weManageArea = driver.findElement(By.className("manage_area_2"));

    wait.until(ExpectedConditions.visibilityOf(weManageArea));

    Assert.assertTrue(weManageArea.isDisplayed());
}

From source file:com.sios.stc.coseng.test.User.java

License:Open Source License

@Test(description = "Verify login with newly changed password.", dataProvider = "credentials")
public void login(final String username, final String password) throws Exception {

    User.log.log(Level.INFO, "Username: " + username + " Password: " + password);

    driver.get(baseUrl + "/ui/#/login");
    new WebDriverWait(driver, 10);

    final WebElement weUsername = driver.findElement(By.name("userName"));
    final WebElement wePassword = driver.findElement(By.name("password"));
    final WebElement weSubmit = driver.findElement(By.className("login_submit_button"));

    final Actions action = new Actions(driver);

    action.moveToElement(weUsername).sendKeys(weUsername, username).moveToElement(wePassword)
            .sendKeys(wePassword, password).click(weSubmit).build().perform();

    final WebElement weManageArea = driver.findElement(By.className("manage_area_2"));

    Assert.assertTrue(weManageArea.isDisplayed());
}

From source file:com.smartqa.engine.WebEngine.java

License:Apache License

/**
 * check web page web element status/*from   w ww.  j  a va2  s  .  c o  m*/
 * 
 * @param name - name stands for web element
 * @param condition - currently support: display, enable
 * @param args - possible need dynamic args to build xpath
 * @return WebEngine
 */
public boolean should(String name, String condition, String... args) {
    String xpath = path.getPath(namespace, name);

    if (args != null && args.length > 0)
        for (int i = 0; i < args.length; i++) {
            String flag = "\\{" + i + "\\}";
            if (xpath.contains("{" + i + "}"))
                xpath = xpath.replaceAll(flag, args[i]);
        }

    if (StringUtils.isEmpty(xpath))
        throw new InvalidPathException(name, namespace);

    WebElement element = driver.findElement(By.xpath(xpath));
    if (element == null)
        throw new ElementNotFoundException(xpath);

    if ("display".equalsIgnoreCase(condition) || "show".equalsIgnoreCase(condition))
        return element.isDisplayed();
    else if ("enable".equalsIgnoreCase(condition))
        return element.isEnabled();

    return false;
}

From source file:com.smartqa.utils.WebDriverUtils.java

License:Apache License

/**
 * create visibility expected condition<br/>
 * so avoid web element not show up in time
 * //  ww  w. ja v  a2 s.c om
 * @param by 
 * @return condition instance
 */
public static ExpectedCondition<WebElement> visibility(final By by) {
    return new ExpectedCondition<WebElement>() {
        public WebElement apply(WebDriver driver) {
            WebElement element = driver.findElement(by);
            return element.isDisplayed() ? element : null;
        }
    };
}