001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
018 *
019 */
020
021package org.nuxeo.functionaltests.pages.search.aggregates;
022
023import java.util.HashMap;
024import java.util.Map;
025import java.util.regex.Matcher;
026import java.util.regex.Pattern;
027
028import org.nuxeo.functionaltests.forms.Select2WidgetElement;
029import org.openqa.selenium.By;
030import org.openqa.selenium.WebDriver;
031import org.openqa.selenium.WebElement;
032
033/**
034 * @since 7.4
035 */
036public class Select2AggregateElement extends Select2WidgetElement implements AggregateElement {
037
038    public Select2AggregateElement(WebDriver driver, WebElement element) {
039        super(driver, element);
040    }
041
042    public Select2AggregateElement(WebDriver driver, WebElement element, boolean multiple) {
043        super(driver, element, multiple);
044    }
045
046    public Map<String, Integer> getAggregates() {
047        Map<String, Integer> result = new HashMap<String, Integer>();
048        clickSelect2Field();
049        for (WebElement e : getSuggestedEntries()) {
050            String label;
051            Integer count;
052            String temp = e.getText();
053            Pattern regex = Pattern.compile(AGG_REGEX);
054            Matcher regexMatcher = regex.matcher(temp);
055            regexMatcher.find();
056            label = regexMatcher.group(1);
057            String s = regexMatcher.group(2);
058            count = Integer.parseInt(s);
059            result.put(label, count);
060        }
061        return result;
062    }
063
064    @Override
065    public void clickSelect2Field() {
066        WebElement select2Field = null;
067        if (multiple) {
068            select2Field = element.findElement(By.xpath("ul/li/input"));
069        } else {
070            select2Field = element.findElement(By.xpath("a[contains(@class,'select2-choice')]"));
071        }
072        select2Field.click();
073    }
074
075}