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.Locator;
029import org.nuxeo.functionaltests.forms.Select2WidgetElement;
030import org.openqa.selenium.By;
031import org.openqa.selenium.WebDriver;
032import org.openqa.selenium.WebElement;
033
034/**
035 * @since 7.4
036 */
037public class Select2AggregateElement extends Select2WidgetElement implements AggregateElement {
038
039    public Select2AggregateElement(WebDriver driver, WebElement element) {
040        super(driver, element);
041    }
042
043    public Select2AggregateElement(WebDriver driver, WebElement element, boolean multiple) {
044        super(driver, element, multiple);
045    }
046
047    public Map<String, Integer> getAggregates() {
048        Map<String, Integer> result = new HashMap<String, Integer>();
049        clickSelect2Field();
050        for (WebElement e : getSuggestedEntries()) {
051            String label;
052            Integer count;
053            String temp = e.getText();
054            Pattern regex = Pattern.compile(AGG_REGEX);
055            Matcher regexMatcher = regex.matcher(temp);
056            regexMatcher.find();
057            label = regexMatcher.group(1);
058            String s = regexMatcher.group(2);
059            count = Integer.parseInt(s);
060            result.put(label, count);
061        }
062        return result;
063    }
064
065    @Override
066    public void clickSelect2Field() {
067        WebElement select2Field = null;
068        if (multiple) {
069            select2Field = element.findElement(By.xpath("ul/li/input"));
070        } else {
071            select2Field = element.findElement(By.xpath("a[contains(@class,'select2-choice')]"));
072        }
073        Locator.waitUntilEnabled(select2Field);
074        Locator.scrollToElement(select2Field);
075        select2Field.click();
076    }
077
078}