001/*
002 * (C) Copyright 2015-2016 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 *     Nelson Silva <nsilva@nuxeo.com>
018 */
019package org.nuxeo.functionaltests.pages.admincenter.activity;
020
021import java.text.SimpleDateFormat;
022import java.util.Date;
023
024import org.nuxeo.functionaltests.EventListener;
025import org.openqa.selenium.By;
026import org.openqa.selenium.JavascriptExecutor;
027import org.openqa.selenium.WebDriver;
028
029/**
030 * @since 7.10
031 */
032public abstract class ActivityDashboardPage extends ActivityPage {
033
034    public static final String DATA_CHANGED_EVENT = "data-changed";
035
036    private final String selector;
037
038    private final JavascriptExecutor js;
039
040    public ActivityDashboardPage(WebDriver driver, String selector) {
041        super(driver);
042        this.js = (JavascriptExecutor) driver;
043        this.selector = selector;
044        // ensure our elements are ready
045        findElementsWithTimeout(By.cssSelector(selector));
046    }
047
048    public EventListener listenForDataChanges() {
049        return new EventListener(driver, ActivityDashboardPage.DATA_CHANGED_EVENT, selector);
050    }
051
052    public void setStartDate(Date date) {
053        String fmtDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
054        StringBuilder sb = new StringBuilder();
055        sb.append("var date = document.querySelectorAll(\"input[type='date']\")[0];");
056        sb.append("date.value = '" + fmtDate + "';");
057        // force set the date since change events might not be triggered
058        sb.append("var els = document.querySelectorAll('" + selector + "');");
059        sb.append("for (var i=0; i<els.length; i++) { els[i].startDate = '" + fmtDate + "'; }");
060        js.executeScript(sb.toString());
061    }
062
063    public void setEndDate(Date date) {
064        String fmtDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
065        StringBuilder sb = new StringBuilder();
066        sb.append("var date = document.querySelectorAll(\"input[type='date']\")[1];");
067        sb.append("date.value = '" + fmtDate + "';");
068        // force set the date since change events might not be triggered
069        sb.append("var els = document.querySelectorAll('" + selector + "');");
070        sb.append("for (var i=0; i<els.length; i++) { els[i].endDate = '" + fmtDate + "'; }");
071        js.executeScript(sb.toString());
072    }
073}