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