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.ecofactor.qa.automation.platform.util.Pageutil.java

License:Open Source License

/**
 * Gets the element by the containing text.
 * @param driver the driver//ww  w. java2  s . c om
 * @param locator the locator
 * @param textValue the text value
 * @param timeout the timeout
 * @return the element by text contains
 */
public static WebElement getElementByTextContains(final WebDriver driver, final By locator,
        final String textValue, final CustomTimeout timeout) {

    WebElement elementAttrValue = null;
    final List<WebElement> elementList = getElements(driver, locator, timeout);
    for (final WebElement webElement : elementList) {
        final String valueAttribute = webElement.getText().trim();
        if (webElement.isDisplayed() && webElement.isEnabled() && valueAttribute.trim().contains(textValue)) {
            elementAttrValue = webElement;
        }
    }
    return elementAttrValue;
}

From source file:com.ecofactor.qa.automation.util.mail.Gmail.java

License:Open Source License

/**
 * Gets the changed password./*from w ww  .  j  av a 2  s  . c  o m*/
 * @param url the url
 * @param emailUserName the email user name
 * @param emailPassword the email password
 * @param subject the subject
 * @param boldIndex the bold index
 * @param pwdIndex the pwd index
 * @return the changed password
 */
public String getChangedPassword(String url, String emailUserName, String emailPassword, String subject,
        int boldIndex, int pwdIndex) {

    WebDriver driver = DriverConfig.getDriver();
    if (DriverConfig.getDriver() == null) {
        driverConfig.loadDriver();
        driver = DriverConfig.getDriver();
    }

    String tempPassword = null;

    largeWait();
    DriverConfig.setLogString("Load Email portal URL at change password", true);
    driver.navigate().to(url + "" + emailUserName);
    largeWait();

    boolean session = isDisplayedById(driver, "Passwd", MEDIUM_TIMEOUT);
    if (session) {
        DriverConfig.setLogString(
                "Enter username and password for Email(" + emailUserName + "/" + emailPassword + ")", true);
        driver.findElement(By.id("Email")).sendKeys(emailUserName);
        driver.findElement(By.id("Passwd")).sendKeys(emailPassword);

        mediumWait();
        WebElement signInButtonElement = driver.findElement(By.id("signIn"));
        DriverConfig.setLogString("click Sign In button", true);
        signInButtonElement.click();
        largeWait();
        boolean button = isDisplayedById(driver, "no-button", MEDIUM_TIMEOUT);
        if (button) {
            WebElement buttonElement = driver.findElement(By.id("no-button"));
            buttonElement.click();
            mediumWait();
        }
    }

    isDisplayedByCSS(driver, ".aio.UKr6le", MEDIUM_TIMEOUT);
    DriverConfig.setLogString("Click Inbox.", true);
    WebElement inboxElement = DriverConfig.getDriver().findElement(By.partialLinkText("Inbox"));
    inboxElement.click();
    largeWait();

    DriverConfig.setLogString("Click Subject of Email.", true);
    WebElement webElement = driver.findElement(By.cssSelector(".ae4.UI.UJ"));
    List<WebElement> tagElement = webElement.findElements(By.tagName(TAG_BOLD));
    WebElement firstElement = tagElement.get(0);
    if (firstElement != null && firstElement.isDisplayed()
            && firstElement.getText().equalsIgnoreCase(subject)) {
        firstElement.click();
        largeWait();
    }

    DriverConfig.setLogString("Get temporary Password/activation link from Email.", true);
    if (boldIndex == 1) {
        WebElement divBodyElement = driver.findElement(By.xpath("//*[contains(@class,'adO')]"));
        if (pwdIndex == 1) {
            WebElement pwdElement = divBodyElement.findElements(By.tagName(TAG_STRONG)).get(0);
            tempPassword = pwdElement.getText();
            DriverConfig.setLogString("Temporary password got from email " + tempPassword, true);
        } else {
            WebElement pwdElement = divBodyElement.findElements(By.tagName(TAG_ANCHOR)).get(0);
            tempPassword = pwdElement.getText();
            DriverConfig.setLogString("Activation link got from email " + tempPassword, true);
        }
    } else if (boldIndex == 0) {
        DriverConfig.setLogString("Header Tag", true);
        WebElement divBodyElement = driver.findElement(By.className("gs"));
        List<WebElement> headerElement = divBodyElement.findElements(By.tagName("strong"));
        for (WebElement webelement : headerElement) {
            if (webelement.isDisplayed()) {
                tempPassword = webelement.getText();
                DriverConfig.setLogString("Temporary password got from email " + tempPassword, true);
            }
        }
    }
    mediumWait();

    deleteFirstMail(driver);

    DriverConfig.setLogString("Click user for Signout option", true);
    boolean value = isDisplayedById(DriverConfig.getDriver(), "gbgs4dn", MEDIUM_TIMEOUT);
    DriverConfig.setLogString("User name displayed for sign out : " + value, true);
    if (value) {
        driver.findElement(By.id("gbgs4dn")).click();
    } else {
        WebElement signOutElement = DriverConfig.getDriver().findElement(By.partialLinkText(emailUserName));
        signOutElement.click();
    }
    smallWait();

    DriverConfig.setLogString("Click Signout", true);
    driver.findElement(By.id("gb_71")).click();

    mediumWait();
    mediumWait();

    return tempPassword;
}

From source file:com.ecofactor.qa.automation.util.mail.Gmail.java

License:Open Source License

/**
 * Delete first mail.//  ww w .ja v a  2s  . c o  m
 * @param driver the driver
 */
public void deleteFirstMail(WebDriver driver) {

    DriverConfig.setLogString("Delete First Mail", true);
    DriverConfig.setLogString("Click Inbox", true);
    WebElement inboxElement = DriverConfig.getDriver().findElement(By.partialLinkText("Inbox"));
    inboxElement.click();
    mediumWait();

    DriverConfig.setLogString("Click Subject of Email.", true);
    boolean subjectDisplayed = isDisplayedByCSS(driver, ".ae4.UI.UJ", MEDIUM_TIMEOUT);
    if (subjectDisplayed) {
        WebElement webElement1 = driver.findElement(By.cssSelector(".ae4.UI.UJ"));
        if (!webElement1.getText().contains("No new mail")) {

            WebElement firstElement2 = null;
            if (webElement1 != null && webElement1.findElements(By.tagName(TAG_BOLD)) != null
                    && webElement1.findElements(By.tagName(TAG_BOLD)).size() > 0) {
                firstElement2 = webElement1.findElements(By.tagName(TAG_BOLD)).get(0);
            } else {
                firstElement2 = webElement1.findElements(By.cssSelector("td.yX.xY")).get(0);
            }

            if (firstElement2 != null && firstElement2.isDisplayed()) {
                firstElement2.click();
                mediumWait();

                DriverConfig.setLogString("Delete the Email", true);
                if (driver.findElements(By.cssSelector("div.ar9.T-I-J3.J-J5-Ji")) != null
                        && driver.findElements(By.cssSelector("div.ar9.T-I-J3.J-J5-Ji")).size() == 2) {
                    DriverConfig.setLogString("Click delete old mails", true);
                    WebElement webElement2 = driver.findElements(By.cssSelector("div.ar9.T-I-J3.J-J5-Ji"))
                            .get(1);
                    webElement2.click();
                } else {
                    DriverConfig.setLogString("Click delete old mails", true);
                    smallWait();
                    WebElement webElement2 = driver.findElements(By.cssSelector("div.ar9.T-I-J3.J-J5-Ji"))
                            .get(1);
                    webElement2.click();
                }

                smallWait();
            }
        }

    }
}

From source file:com.ecofactor.qa.automation.util.mail.GmailForNewUser.java

License:Open Source License

/**
 * Gets the changed password./*  ww w. java  2s.c  om*/
 * @param url the url
 * @param emailUserName the email user name
 * @param emailPassword the email password
 * @param subject the subject
 * @param boldIndex the bold index
 * @param pwdIndex the pwd index
 * @return the changed password
 */
public String getChangedPassword(String url, String emailUserName, String emailPassword, String subject,
        int boldIndex, int pwdIndex) {

    if (gmailDriver == null) {
        gmailDriver = new ChromeDriver();
    }

    String tempPassword = null;

    largeWait();
    DriverConfig.setLogString("Load Email portal URL at change password", true);
    DriverConfig.setLogString("URL : " + url + "" + emailUserName, true);
    gmailDriver.navigate().to(url + "" + emailUserName);
    largeWait();

    boolean session = isDisplayedById(gmailDriver, "Passwd", MEDIUM_TIMEOUT);
    if (session) {
        DriverConfig.setLogString(
                "Enter username and password for Email(" + emailUserName + "/" + emailPassword + ")", true);
        if (isDisplayedById(gmailDriver, "reauthEmail", SHORT_TIMEOUT)
                && gmailDriver.findElement(By.id("reauthEmail")).getText().equalsIgnoreCase(emailUserName)) {
            clearAndInput(gmailDriver, By.id("Passwd"), emailPassword);
        } else {
            clearAndInput(gmailDriver, By.id("Email"), emailUserName);
            clearAndInput(gmailDriver, By.id("Passwd"), emailPassword);
        }

        mediumWait();
        WebElement signInButtonElement = gmailDriver.findElement(By.id("signIn"));
        DriverConfig.setLogString("click Sign In button", true);
        signInButtonElement.click();
        largeWait();
        boolean button = isDisplayedById(gmailDriver, "no-button", MEDIUM_TIMEOUT);
        if (button) {
            WebElement buttonElement = gmailDriver.findElement(By.id("no-button"));
            buttonElement.click();
            mediumWait();
        }
    }

    isDisplayedByCSS(gmailDriver, ".aio.UKr6le", MEDIUM_TIMEOUT);
    DriverConfig.setLogString("Click Inbox.", true);
    WebElement inboxElement = gmailDriver.findElement(By.partialLinkText("Inbox"));
    inboxElement.click();
    largeWait();

    DriverConfig.setLogString("Click Subject of Email.", true);
    DriverConfig.setLogString("Subject of Email : " + subject, true);
    WebElement webElement = gmailDriver.findElement(By.cssSelector(".ae4.UI.UJ"));
    List<WebElement> tagElement = webElement.findElements(By.tagName(TAG_BOLD));
    WebElement firstElement = tagElement.get(0);
    if (firstElement != null && firstElement.isDisplayed()
            && firstElement.getText().equalsIgnoreCase(subject)) {
        firstElement.click();
        largeWait();
    }

    DriverConfig.setLogString("Get temporary Password/activation link from Email.", true);
    if (boldIndex == 1) {
        WebElement divBodyElement = gmailDriver.findElement(By.xpath("//*[contains(@class,'adO')]"));
        if (pwdIndex == 1) {
            WebElement pwdElement = divBodyElement.findElements(By.tagName(TAG_STRONG)).get(0);
            tempPassword = pwdElement.getText();
            DriverConfig.setLogString("Temporary password got from email " + tempPassword, true);
        } else {
            WebElement pwdElement = divBodyElement.findElements(By.tagName(TAG_ANCHOR)).get(0);
            tempPassword = pwdElement.getText();
            DriverConfig.setLogString("Activation link got from email " + tempPassword, true);
        }
    } else if (boldIndex == 0) {
        DriverConfig.setLogString("Header Tag", true);
        WebElement divBodyElement = gmailDriver.findElement(By.className("gs"));
        List<WebElement> headerElement = divBodyElement.findElements(By.tagName("strong"));
        for (WebElement webelement : headerElement) {
            if (webelement.isDisplayed()) {
                tempPassword = webelement.getText();
                DriverConfig.setLogString("Temporary password got from email " + tempPassword, true);
            }
        }
    }
    mediumWait();

    deleteFirstMail(gmailDriver);

    DriverConfig.setLogString("Click user for Signout option", true);
    boolean value = isDisplayedById(gmailDriver, "gbgs4dn", MEDIUM_TIMEOUT);
    DriverConfig.setLogString("User name displayed for sign out : " + value, true);
    if (value) {
        gmailDriver.findElement(By.id("gbgs4dn")).click();
    } else {
        WebElement signOutElement = gmailDriver.findElement(By.partialLinkText(emailUserName));
        signOutElement.click();
    }
    smallWait();

    DriverConfig.setLogString("Click Signout", true);
    gmailDriver.findElement(By.id("gb_71")).click();

    mediumWait();
    mediumWait();
    gmailDriver.close();
    gmailDriver.quit();
    gmailDriver = null;
    return tempPassword;
}

From source file:com.ecofactor.qa.automation.util.mail.GmailForNewUser.java

License:Open Source License

/**
 * Delete first mail./*from   w ww. jav  a  2s  .c o  m*/
 * @param driver the driver
 */
public void deleteFirstMail(WebDriver driver) {

    DriverConfig.setLogString("Delete First Mail", true);
    DriverConfig.setLogString("Click Inbox", true);
    WebElement inboxElement = driver.findElement(By.partialLinkText("Inbox"));
    inboxElement.click();
    mediumWait();

    DriverConfig.setLogString("Click Subject of Email.", true);
    // boolean subjectDisplayed=isDisplayedByTagName(driver, TAG_BOLD, MEDIUM_TIMEOUT);
    boolean subjectDisplayed = isDisplayedByCSS(driver, ".ae4.UI.UJ", MEDIUM_TIMEOUT);
    if (subjectDisplayed) {
        WebElement webElement1 = driver.findElement(By.cssSelector(".ae4.UI.UJ"));
        // WebElement webElement1 = driver.findElement(By.xpath(".//*[span]"));
        if (!webElement1.getText().contains("No new mail")) {

            WebElement firstElement2 = null;
            if (webElement1 != null && webElement1.findElements(By.tagName(TAG_BOLD)) != null
                    && webElement1.findElements(By.tagName(TAG_BOLD)).size() > 0) {
                firstElement2 = webElement1.findElements(By.tagName(TAG_BOLD)).get(0);
            } else {
                firstElement2 = webElement1.findElements(By.cssSelector("td.yX.xY")).get(0);
            }

            if (firstElement2 != null && firstElement2.isDisplayed()) {
                firstElement2.click();
                mediumWait();

                DriverConfig.setLogString("Delete the Email", true);
                if (driver.findElements(By.cssSelector("div.ar9.T-I-J3.J-J5-Ji")) != null
                        && driver.findElements(By.cssSelector("div.ar9.T-I-J3.J-J5-Ji")).size() == 2) {
                    DriverConfig.setLogString("Click delete old mails", true);
                    WebElement webElement2 = driver.findElements(By.cssSelector("div.ar9.T-I-J3.J-J5-Ji"))
                            .get(1);
                    webElement2.click();
                } else {
                    DriverConfig.setLogString("Click delete old mails", true);
                    smallWait();
                    WebElement webElement2 = driver.findElements(By.cssSelector("div.ar9.T-I-J3.J-J5-Ji"))
                            .get(1);
                    webElement2.click();
                }

                smallWait();
            }
        }

    }

}

From source file:com.ecofactor.qa.automation.util.mail.OutlookMail.java

License:Open Source License

/**
 * Gets the changed password./*from   w ww  .  j a  v a 2s.  c  om*/
 * @param driver the driver
 * @param url the url
 * @param emailUserName the email user name
 * @param emailPassword the email password
 * @param subject the subject
 * @param boldIndex the bold index
 * @return the changed password
 */
public String getChangedPassword(String url, String emailUserName, String emailPassword, String subject,
        int boldIndex) {

    WebDriver driver = driverConfig.getDriver();

    DriverConfig.setLogString("Load Email portal URL.", true);
    driver.navigate().to(url);
    largeWait();

    DriverConfig.setLogString("Enter Username/Password for Email.", true);
    driver.findElement(By.id("cred_userid_inputtext")).sendKeys(emailUserName);
    driver.findElement(By.id("cred_password_inputtext")).sendKeys(emailPassword);

    smallWait();
    DriverConfig.setLogString("Click Login.", true);
    WebElement signInButtonElement = driver.findElement(By.cssSelector("#cred_sign_in_button"));
    DriverConfig.setLogString("send keys to login", true);
    signInButtonElement.sendKeys(Keys.RETURN);
    largeWait();
    DriverConfig.setLogString("click btn", true);
    signInButtonElement.click();
    largeWait();

    DriverConfig.setLogString("Click Inbox.", true);
    WebElement outLookMenu = retrieveElementByTagText(driver, TAG_SPAN, "Outlook");
    if (outLookMenu != null && outLookMenu.isDisplayed()) {
        outLookMenu.click();
        mediumWait();
    }

    DriverConfig.setLogString("Click Subject of Email.", true);
    List<WebElement> subElements = retrieveElementsByTagText(driver, TAG_SPAN, subject);
    if (subElements.size() > 0) {
        subElements.get(0).click();
        mediumWait();
    }
    largeWait();

    DriverConfig.setLogString("Get temporary Password from Email.", true);
    WebElement divBodyElement = driver.findElement(By.id("Item.MessagePartBody"));
    mediumWait();
    WebElement pwdElement = divBodyElement.findElements(By.tagName(TAG_BOLD)).get(boldIndex);
    String tempPassword = pwdElement.getText();
    DriverConfig.setLogString("Temporary password got from email " + tempPassword, true);

    DriverConfig.setLogString("Click user for Signout option", true);
    WebElement menuElement = retrieveElementByAttributeValue(driver, TAG_BUTTON, ATTR_CLASS,
            "button _hl_2 _hl_e");
    menuElement.click();
    smallWait();

    DriverConfig.setLogString("Click Signout", true);
    WebElement signOut = retrieveElementByTagText(driver, TAG_SPAN, "sign out", MEDIUM_TIMEOUT);
    signOut.click();
    smallWait();

    return tempPassword;
}

From source file:com.ecofactor.qa.automation.util.PageUtil.java

License:Open Source License

/**
 * Utility to check weather the particular field is displayed or not.
 * @param driver the driver// w w w.j  a  va2 s .  c  o  m
 * @param fieldname the fieldname
 * @param timeOut the time out
 * @return boolean
 */
public static boolean isDisplayedById(final WebDriver driver, final String fieldname, final int timeOut) {

    boolean isLoaded = false;
    try {
        isLoaded = (new WebDriverWait(driver, timeOut)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {

                WebElement element = d.findElement(By.id(fieldname));
                return element.isDisplayed();
            }
        });
    } catch (TimeoutException te) {
        isLoaded = false;
    }
    return isLoaded;
}

From source file:com.ecofactor.qa.automation.util.PageUtil.java

License:Open Source License

/**
 * Utility to check weather the particular field is displayed or not.
 * @param driver the driver//from  w ww . j a v a 2 s . com
 * @param cssSelector the css selector
 * @param timeOut the time out
 * @return boolean
 */
public static boolean isDisplayedByCSS(final WebDriver driver, final String cssSelector, final int timeOut) {

    boolean isLoaded = false;

    try {
        isLoaded = (new WebDriverWait(driver, timeOut)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {

                List<WebElement> elements = d.findElements(By.cssSelector(cssSelector));
                boolean displayed = false;
                for (WebElement element : elements) {
                    displayed = element.isDisplayed();
                    if (displayed) {
                        break;
                    }
                }

                return displayed;
            }
        });
    } catch (TimeoutException te) {
        isLoaded = false;
    }

    return isLoaded;
}

From source file:com.ecofactor.qa.automation.util.PageUtil.java

License:Open Source License

/**
 * Sets the value.// w w w .  j  a  v a 2 s  .com
 * @param driver the driver
 * @param cssSelector the css selector
 * @param value the value
 */
public static void setValueByCSS(WebDriver driver, String cssSelector, String value) {

    if (isDisplayedByCSS(driver, cssSelector, 10)) {
        List<WebElement> inputs = driver.findElements(By.cssSelector(cssSelector));
        for (WebElement input : inputs) {
            if (input.isDisplayed() && input.isEnabled()) {
                input.clear();
                input.sendKeys(value);
            }
        }
    }
}

From source file:com.ecofactor.qa.automation.util.PageUtil.java

License:Open Source License

/**
 * Sets the value.//  w  w w . j  a v  a  2  s. c o m
 * @param driver the driver
 * @param cssSelector the css selector
 * @param value the value
 * @param index the index
 */
public static void setValueByCSS(WebDriver driver, String cssSelector, String value, int index) {

    if (isDisplayedByCSS(driver, cssSelector, 10)) {
        List<WebElement> inputs = driver.findElements(By.cssSelector(cssSelector));
        int count = 0;
        for (WebElement input : inputs) {
            if (input.isDisplayed() && input.isEnabled()) {

                if (count == index) {
                    input.clear();
                    input.sendKeys(value);
                    logger.info("Count " + count + ", " + input.getAttribute("id"));
                    break;
                }

                ++count;
            }
        }
    }
}