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