br.gov.frameworkdemoiselle.behave.runner.webdriver.ui.WebSelect.java Source code

Java tutorial

Introduction

Here is the source code for br.gov.frameworkdemoiselle.behave.runner.webdriver.ui.WebSelect.java

Source

/*
 * Demoiselle Framework
 * Copyright (C) 2013 SERPRO
 * ----------------------------------------------------------------------------
 * This file is part of Demoiselle Framework.
 * 
 * Demoiselle Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License version 3
 * along with this program; if not,  see <http://www.gnu.org/licenses/>
 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA  02110-1301, USA.
 * ----------------------------------------------------------------------------
 * Este arquivo  parte do Framework Demoiselle.
 * 
 * O Framework Demoiselle  um software livre; voc pode redistribu-lo e/ou
 * modific-lo dentro dos termos da GNU LGPL verso 3 como publicada pela Fundao
 * do Software Livre (FSF).
 * 
 * Este programa  distribudo na esperana que possa ser til, mas SEM NENHUMA
 * GARANTIA; sem uma garantia implcita de ADEQUAO a qualquer MERCADO ou
 * APLICAO EM PARTICULAR. Veja a Licena Pblica Geral GNU/LGPL em portugus
 * para maiores detalhes.
 * 
 * Voc deve ter recebido uma cpia da GNU LGPL verso 3, sob o ttulo
 * "LICENCA.txt", junto com esse programa. Se no, acesse <http://www.gnu.org/licenses/>
 * ou escreva para a Fundao do Software Livre (FSF) Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
 */
package br.gov.frameworkdemoiselle.behave.runner.webdriver.ui;

import java.util.List;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import br.gov.frameworkdemoiselle.behave.runner.ui.Select;

/**
 * @author SERPRO
 */
public class WebSelect extends WebBase implements Select {

    Logger log = Logger.getLogger(WebSelect.class);

    /**
     * Armazena o elemento html principal que compe o combo
     * 
     * <b>Obs:</b> Especfico para combos do primefaces
     */
    private WebElement elementMain = null;

    /**
     * {@inheritDoc}
     */
    public void selectByVisibleText(String value) {
        select(value, WebSelectType.TEXT);
    }

    /**
     * {@inheritDoc}
     */
    public void selectByIndex(int index) {
        select(index + "", WebSelectType.INDEX);
    }

    /**
     * {@inheritDoc}
     */
    public void selectByValue(String value) {
        select(value, WebSelectType.VALUE);
    }

    /**
     * {@inheritDoc}
     */
    @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();
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void blur() {
        getElements().get(0).click();
    }

    public void blur(WebElement element) {
        // Clica em outro elemento para tirar o foco
        element.click();
    }

    /**
     * Mtodo generalizado para selecionar o valor do select (DropDown)
     * 
     * @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);

        }

    }

    /**
     * Seleciona um item <li>da lista do combo, retirando ou no o foco a
     * depender do que for definido no parmetro "action"
     * 
     * @param WebElement
     *            item Elemento <li>que ser selecionado
     * @see ElementMainAction
     */
    private void itemListClick(WebElement item) {
        try {
            item.click();
        } catch (Throwable t) {
            waitElement(1);
            item.click();
        }
    }

    public enum WebSelectType {
        TEXT, INDEX, VALUE
    }

}