Example usage for org.openqa.selenium By tagName

List of usage examples for org.openqa.selenium By tagName

Introduction

In this page you can find the example usage for org.openqa.selenium By tagName.

Prototype

public static By tagName(String tagName) 

Source Link

Usage

From source file:br.gov.frameworkdemoiselle.behave.runner.webdriver.ui.WebScreen.java

License:Open Source License

public void waitText(String text, Long timeout) {
    int totalMilliseconds = 0;
    while (!((WebDriver) runner.getDriver()).findElement(By.tagName("body")).getText().contains(text)) {
        try {/* ww  w. j a v a 2 s  .com*/
            logger.debug("Aguardando o elemento [" + text + "]");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        totalMilliseconds += 1000;

        if (totalMilliseconds > timeout)
            Assert.fail("Texto no encontrado na tela. Texto: " + text);
    }
}

From source file:br.gov.frameworkdemoiselle.behave.runner.webdriver.ui.WebSelect.java

License:Open Source License

/**
 * {@inheritDoc}/* www.  jav  a 2 s  .  com*/
 */
@Override
public String getText() {
    // Fazer tratamento para SELECT normal e PrimeFaces
    List<WebElement> elements = getElements();
    if (elements.get(0).getTagName().equals("select")) {
        org.openqa.selenium.support.ui.Select lSelect = new org.openqa.selenium.support.ui.Select(
                elements.get(0));
        return lSelect.getFirstSelectedOption().getText();
    } else {
        WebElement element = elements.get(0);
        if (element.getAttribute("class").contains("ui-selectonemenu")
                && !element.getAttribute("class").contains("ui-selectonemenu-label")) {
            org.openqa.selenium.support.ui.Select lSelectInnerElement = new org.openqa.selenium.support.ui.Select(
                    element.findElement(By.tagName("select")));
            return lSelectInnerElement.getFirstSelectedOption().getAttribute("innerHTML");
        } else {
            return element.getText();
        }
    }
}

From source file:br.gov.frameworkdemoiselle.behave.runner.webdriver.ui.WebSelect.java

License:Open Source License

/**
 * Mtodo generalizado para selecionar o valor do select (DropDown)
 * /*from   ww  w. j a  va  2 s .c o m*/
 * @param value
 * @param type
 */
private void select(String value, WebSelectType type) {

    // Aguarda o primeiro elemento ser clicvel
    waitElement(0);

    List<WebElement> elements = getElements();

    if (elements.get(0).getTagName().equals("select")) {

        // Select comum e usa um helper do selenium
        org.openqa.selenium.support.ui.Select lSelect = new org.openqa.selenium.support.ui.Select(
                elements.get(0));

        // Verifica o tipo valor do select
        if (type == WebSelectType.TEXT) {
            lSelect.selectByVisibleText(value);
        } else if (type == WebSelectType.INDEX) {
            lSelect.selectByIndex(Integer.parseInt(value));

            // Soluo de contorno para atualizar o valor selecionado
            lSelect.getFirstSelectedOption();
        } else if (type == WebSelectType.VALUE) {
            lSelect.selectByValue(value);
        }

    } else {

        // Outros tipos de select como a do primefaces
        elementMain = elements.get(0);
        elementMain.click();

        // Tempo do efeito de abertura das opes
        waitElementOnlyVisible(1);

        List<WebElement> elementValue = elements.get(1).findElements(By.tagName("li"));

        // Aguarda o segundo elemento ser clicvel
        if (type == WebSelectType.INDEX) {
            // ?ndice comeando em 1 - Muitas vezes o 1  o item SELECIONE
            int index = 1;
            for (WebElement item : elementValue) {
                if (index++ == Integer.valueOf(value)) {
                    itemListClick(item);
                    break;
                }
            }
        } else {
            for (WebElement item : elementValue) {
                // Verifica se existe a virgula, se existir significa que
                // so multiplos valores e procura por contains
                if (value.contains(",")) {
                    if (!item.getText().equals("") && value.contains(item.getText())) {
                        itemListClick(item);

                        item = null;
                        continue;
                    }
                } else if (item.getText().equals(value)) {
                    itemListClick(item);
                    break;
                }
            }
        }

        // Aguarda o DIV sumir se no for seleo multipla, pois na seleo
        // multipla ele tem o boto de fechar
        if (!value.contains(","))
            waitInvisible(1);

    }

}

From source file:br.gov.frameworkdemoiselle.behave.runner.webdriver.util.ByConverter.java

License:Open Source License

public static By convert(ElementLocatorType type, String locator) {
    By by = null;/*from ww  w.  ja v  a  2  s. c o  m*/

    if (type == ElementLocatorType.Id) {
        by = By.id(locator);
    } else if (type == ElementLocatorType.ClassName) {
        by = By.className(locator);
    } else if (type == ElementLocatorType.CssSelector) {
        by = By.cssSelector(locator);
    } else if (type == ElementLocatorType.LinkText) {
        by = By.linkText(locator);
    } else if (type == ElementLocatorType.Name) {
        by = By.name(locator);
    } else if (type == ElementLocatorType.TagName) {
        by = By.tagName(locator);
    } else if (type == ElementLocatorType.XPath) {
        by = By.xpath(locator);
    } else if (type == ElementLocatorType.Value) {
        by = By.xpath("//*[@value='" + locator + "']");
    } else {
        throw new BehaveException(message.getString("exception-invalid-option", type, "convert"));
    }

    return by;
}

From source file:br.gov.frameworkdemoiselle.behave.runner.webdriver.WebDriverRunner.java

License:Open Source License

public File saveScreenshotTo(String fileName, boolean generateSource) {
    File screenshotFile = new File(fileName);

    screenshotFile.getParentFile().mkdirs();

    driver.manage().window().maximize();

    File screenshot = null;//from w w w  .jav a  2 s  .c  o  m

    if (BehaveConfig.getRunner_screenShotZoomout() != 0) {
        WebElement html = driver.findElement(By.tagName("html"));

        for (int x = 0; x < BehaveConfig.getRunner_screenShotZoomout(); x++) {
            html.sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT));
        }

        screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        html.sendKeys(Keys.chord(Keys.CONTROL, "0"));
    } else {
        screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    }

    if (screenshot != null) {
        try {
            FileUtils.copyFile(screenshot, new File(screenshotFile.getAbsolutePath()));

            if (generateSource) {
                writeHtmlFile(screenshotFile.getAbsolutePath());
            }
        } catch (IOException e) {
            throw new BehaveException(message.getString("exception-save-screenshot"), e);
        }
    }

    return screenshotFile;
}

From source file:br.ufmg.dcc.saotome.beholder.selenium.SeleniumBrowser.java

License:Apache License

@Override
public boolean isTextPresent(final String text) {

    try {//  w w  w. java  2 s  . co  m
        WebDriverWait wait = new WebDriverWait(SeleniumController.getDriver(), SeleniumComponent.TIMEOUT);

        ExpectedCondition<Boolean> resultsAreDisplayed = new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver arg0) {
                String expression = text.toLowerCase();
                String pageText = SeleniumController.getDriver().findElement(By.tagName("body")).getText()
                        .toLowerCase();
                return pageText.contains(expression);
            }
        };
        wait.until(resultsAreDisplayed);
        return true;
    } catch (TimeoutException toe) {
        return false;
    }
}

From source file:br.ufmg.dcc.saotome.beholder.selenium.ui.form.SeleniumSelectField.java

License:Apache License

@Override
public final List<Option> getOptions() {

    SeleniumOption option;//from   w  w w .j av  a2s  . c o  m
    reloadElement();
    List<WebElement> webElements = getElement().findElements(By.tagName("option"));
    for (WebElement element : webElements) {
        option = new SeleniumOption();
        option.setIndex(Integer.valueOf(element.getAttribute("index")));
        option.setValue(element.getAttribute("value"));
        option.setText(element.getText());
        option.webElement = element;
        options.add(option);
    }
    return this.options;
}

From source file:br.ufmg.dcc.saotome.beholder.selenium.ui.SeleniumComponent.java

License:Apache License

@Override
public void loadByAttribute(final String tagName, final String attributeName, final String value) {

    this.locator = new Locator(tagName, attributeName, value);

    if (this.isDisplayed) {
        WebDriverWait wait = new WebDriverWait(getSeleniumWebDriver(), TIMEOUT);
        ExpectedCondition<Boolean> resultsAreDisplayed = new ExpectedCondition<Boolean>() {

            public Boolean apply(WebDriver arg0) {
                List<WebElement> elements = getSeleniumWebDriver().findElements(By.tagName(tagName));
                for (WebElement el : elements) {
                    if ((el.getAttribute(attributeName) != null)
                            && (el.getAttribute(attributeName).equalsIgnoreCase(value))) {
                        setAttribute(attributeName, value);
                        setElement(el);/*from ww  w  .  ja v a 2 s .  com*/
                        return true;
                    }
                }
                return false;
            }

        };
        wait.until(resultsAreDisplayed);
    }
}

From source file:br.ufmg.dcc.saotome.beholder.selenium.ui.SeleniumComponent.java

License:Apache License

@Override
public <T extends Component, Y extends T> List<T> loadByAttribute(Class<Y> type, final String IdFather,
        final String tagName, final String attributeName, final String value) {

    List<T> components = new ArrayList<T>();

    this.locator = new Locator(tagName, attributeName, value);

    if (this.isDisplayed) {
        WebDriverWait wait = new WebDriverWait(getSeleniumWebDriver(), TIMEOUT);

        wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.tagName(tagName)));
        List<WebElement> elements;

        if (IdFather == null) {
            elements = getSeleniumWebDriver().findElements(By.tagName(tagName));
        } else {//from w w w  . j  ava  2s  .  c o m
            elements = getSeleniumWebDriver().findElement(By.id(IdFather)).findElements(By.tagName(tagName));
        }

        for (WebElement el : elements) {
            if ((el.getAttribute(attributeName) != null)
                    && (el.getAttribute(attributeName).equalsIgnoreCase(value))) {
                T sc = null;

                try {
                    // Use of reflection for instantiate sc 
                    // this equivalent the get an instance of Builder.uiComponentBuilderInstance()
                    sc = (T) type.getDeclaredConstructor(WebDriver.class).newInstance(getSeleniumWebDriver());

                } catch (Exception e) {
                    e.printStackTrace();
                }

                ((SeleniumComponent) sc).setAttribute(attributeName, value);
                ((SeleniumComponent) sc).setElement(el);
                components.add(sc);
            }
        }
    }
    return components;
}

From source file:br.ufmg.dcc.saotome.beholder.selenium.WebDriverAdapter.java

License:Apache License

@Override
public List<WebElement> findElements(final By by) {

    // It's instanced a html element to reuse the findElements of WebElementAdapter      
    WebElement html = this.driver.findElement(By.tagName("html"));
    WebElement htmlAdapter = new WebElementAdapter(html, null, By.tagName("html"));

    return htmlAdapter.findElements(by);
}