001/*
002 * (C) Copyright 2014 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 */
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.AjaxRequestManager;
027import org.nuxeo.functionaltests.Locator;
028import org.openqa.selenium.By;
029import org.openqa.selenium.WebDriver;
030import org.openqa.selenium.WebElement;
031
032/**
033 * @since 6.0
034 */
035public class CheckBoxAggregateElements implements AggregateElement {
036
037    protected WebElement element;
038
039    protected WebDriver driver;
040
041    public CheckBoxAggregateElements(final WebElement element) {
042        super();
043        this.element = element;
044    }
045
046    /**
047     * @param driver
048     * @param coverageAggregate
049     */
050    public CheckBoxAggregateElements(WebDriver driver, WebElement element) {
051        this(element);
052        this.driver = driver;
053    }
054
055    @Override
056    public Map<String, Integer> getAggregates() {
057        Map<String, Integer> result = new HashMap<String, Integer>();
058        for (WebElement e : element.findElements(By.xpath("tbody/tr/td"))) {
059            String label;
060            Integer count;
061            String temp = e.findElement(By.xpath("label")).getText();
062            Pattern regex = Pattern.compile(AGG_REGEX);
063            Matcher regexMatcher = regex.matcher(temp);
064            regexMatcher.find();
065            label = regexMatcher.group(1);
066            String s = regexMatcher.group(2);
067            count = Integer.parseInt(s);
068            result.put(label, count);
069        }
070        return result;
071    }
072
073    public void selectOrUnselect(final String label) {
074        for (WebElement e : element.findElements(By.xpath("tbody/tr/td"))) {
075            String select;
076            String temp = e.findElement(By.xpath("label")).getText();
077            Pattern regex = Pattern.compile(AGG_REGEX);
078            Matcher regexMatcher = regex.matcher(temp);
079            regexMatcher.find();
080            select = regexMatcher.group(1);
081            if (label.equals(select)) {
082                AjaxRequestManager a = new AjaxRequestManager(driver);
083                a.watchAjaxRequests();
084                WebElement input = e.findElement(By.xpath("input"));
085                Locator.waitUntilEnabled(input);
086                Locator.scrollToElement(input);
087                input.click();
088                a.waitForAjaxRequests();
089                break;
090            }
091        }
092    }
093
094}