001/*
002 * (C) Copyright 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 *     Mariana Cedica
018 *     Yannis JULIENNE
019 */
020package org.nuxeo.functionaltests.pages;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.functionaltests.Locator;
025import org.nuxeo.functionaltests.Required;
026import org.nuxeo.functionaltests.forms.Select2WidgetElement;
027import org.nuxeo.functionaltests.fragment.WebFragmentImpl;
028import org.nuxeo.functionaltests.pages.tabs.SummaryTabSubPage;
029import org.openqa.selenium.By;
030import org.openqa.selenium.WebDriver;
031import org.openqa.selenium.WebElement;
032import org.openqa.selenium.support.FindBy;
033
034/**
035 * @since 5.8
036 */
037public class WorkflowHomePage extends AbstractPage {
038
039    protected static final Log log = LogFactory.getLog(WorkflowHomePage.class);
040
041    public WorkflowHomePage(WebDriver driver) {
042        super(driver);
043    }
044
045    @Required
046    @FindBy(xpath = "//div[contains(@id, 'cv_user_open_tasks_nxw_current_user_open_tasks_resultsPanel')]")
047    protected WebElement userTasksPanel;
048
049    @Required
050    @FindBy(id = "nxl_userOpenTasksLayout:nxw_contentViewActions_refreshContentView_form:nxw_contentViewActions_refreshContentView")
051    protected WebElement refreshTask;
052
053    @Required
054    @FindBy(linkText = "Workflow")
055    public WebElement workflowLink;
056
057    public boolean taskExistsOnTasksDashboard(String taskName) {
058        WebElement taskNameEl = Locator.findElementWithTimeout(
059                By.xpath("//span[contains(@id, 'nxw_routing_task_name')]"), userTasksPanel);
060        return taskName.equals(taskNameEl.getText());
061    }
062
063    public void processFirstTask() {
064        WebElement processButton = userTasksPanel.findElement(By.xpath("//input[@type='submit' and @value='Process']"));
065        waitUntilEnabledAndClick(processButton);
066    }
067
068    public SummaryTabSubPage redirectToTask(String taskTitle) {
069        findElementWaitUntilEnabledAndClick(By.linkText(taskTitle));
070        return new SummaryTabSubPage(driver);
071    }
072
073    public boolean isTasksDashboardEmpty() {
074        return !userTasksPanel.getText().contains("Task Name");
075    }
076
077    /**
078     * @since 5.9.1
079     */
080    public void reassignTask(String taskDirective, String user) {
081        TaskFancyBoxFragment taskBox = showTaskFancyBox("Reassign Task");
082        taskBox.waitForTextToBePresent(taskDirective);
083        Select2WidgetElement particpants = new Select2WidgetElement(driver,
084                driver.findElement(By
085                                     .xpath("//div[contains(@id, 'nxl_workflowTaskReassignmentLayout_1:nxw_task_reassignment_actors_1_select2')]")),
086                true);
087        particpants.selectValue(user);
088        taskBox.submit();
089    }
090
091    /**
092     * @since 5.9.1
093     */
094    public void delegateTask(String taskDirective, String user) {
095        TaskFancyBoxFragment taskBox = showTaskFancyBox("Delegate Task");
096        taskBox.waitForTextToBePresent(taskDirective);
097        Select2WidgetElement particpants = new Select2WidgetElement(driver,
098                driver.findElement(By
099                                     .xpath("//div[contains(@id, 'nxl_workflowTaskReassignmentLayout:nxw_task_reassignment_actors_select2')]")),
100                true);
101        particpants.selectValue(user);
102        taskBox.submit();
103    }
104
105    /**
106     * @since 5.9.1
107     */
108    public TaskFancyBoxFragment showTaskFancyBox(String taskAction) {
109        findElementWaitUntilEnabledAndClick(
110                By.xpath(String.format("//input[@type='submit' and @value='%s']", taskAction)));
111        WebElement element = AbstractPage.getFancyBoxContent();
112        return getWebFragment(element, WorkflowHomePage.TaskFancyBoxFragment.class);
113    }
114
115    /**
116     * @since 5.9.1
117     */
118    public static class TaskFancyBoxFragment extends WebFragmentImpl {
119
120        @FindBy(xpath = "//div[@id='fancybox-content']//input[@value='Cancel']")
121        public WebElement cancelButton;
122
123        @FindBy(xpath = "//div[@id='fancybox-content']//input[@type='submit']")
124        public WebElement sumbitButton;
125
126        public TaskFancyBoxFragment(WebDriver driver, WebElement element) {
127            super(driver, element);
128        }
129
130        public void cancel() {
131            Locator.waitUntilEnabledAndClick(cancelButton);
132            AbstractPage.waitForFancyBoxClosed();
133        }
134
135        @Override
136        public void submit() {
137            Locator.waitUntilEnabledAndClick(sumbitButton);
138            AbstractPage.waitForFancyBoxClosed();
139        }
140
141    }
142}