001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
016 *
017 */
018
019package org.nuxeo.functionaltests.pages.search.aggregates;
020
021import java.util.HashMap;
022import java.util.Map;
023import java.util.regex.Matcher;
024import java.util.regex.Pattern;
025
026import org.nuxeo.functionaltests.forms.Select2WidgetElement;
027import org.openqa.selenium.By;
028import org.openqa.selenium.WebDriver;
029import org.openqa.selenium.WebElement;
030
031/**
032 * @since 7.4
033 */
034public class Select2AggregateElement extends Select2WidgetElement implements AggregateElement {
035
036    public Select2AggregateElement(WebDriver driver, WebElement element) {
037        super(driver, element);
038    }
039
040    public Select2AggregateElement(WebDriver driver, WebElement element, boolean multiple) {
041        super(driver, element, multiple);
042    }
043
044    public Map<String, Integer> getAggregates() {
045        Map<String, Integer> result = new HashMap<String, Integer>();
046        clickSelect2Field();
047        for (WebElement e : getSuggestedEntries()) {
048            String label;
049            Integer count;
050            String temp = e.getText();
051            Pattern regex = Pattern.compile(AGG_REGEX);
052            Matcher regexMatcher = regex.matcher(temp);
053            regexMatcher.find();
054            label = regexMatcher.group(1);
055            String s = regexMatcher.group(2);
056            count = Integer.parseInt(s);
057            result.put(label, count);
058        }
059        return result;
060    }
061
062    @Override
063    public void clickSelect2Field() {
064        WebElement select2Field = null;
065        if (mutliple) {
066            select2Field = element.findElement(By.xpath("ul/li/input"));
067        } else {
068            select2Field = element.findElement(By.xpath("a[contains(@class,'select2-choice')]"));
069        }
070        select2Field.click();
071    }
072
073}