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 *     Sun Seng David TAN <stan@nuxeo.com>
018 *     Yannis JULIENNE
019 */
020package org.nuxeo.functionaltests.pages.tabs;
021
022import java.util.ArrayList;
023import java.util.List;
024
025import org.nuxeo.functionaltests.AjaxRequestManager;
026import org.nuxeo.functionaltests.Locator;
027import org.nuxeo.functionaltests.Required;
028import org.nuxeo.functionaltests.pages.AbstractPage;
029import org.openqa.selenium.By;
030import org.openqa.selenium.NoSuchElementException;
031import org.openqa.selenium.WebDriver;
032import org.openqa.selenium.WebElement;
033import org.openqa.selenium.support.FindBy;
034
035/**
036 * @author Sun Seng David TAN <stan@nuxeo.com>
037 */
038public class SummaryTabSubPage extends AbstractPage {
039
040    private static final String COLLECTIONS_FORM_ID = "nxl_grid_summary_layout:nxw_summary_current_document_collections_form";
041
042    public static final String WORKFLOW_START_BUTTON_XPATH = "//input[contains(@id, 'nxw_start_route_widget_start_route')]";
043
044    public static final String WORKFLOW_SELECTOR_XPATH = "//select[contains(@id, 'nxw_start_route_widget')]";
045
046    @FindBy(xpath = WORKFLOW_START_BUTTON_XPATH)
047    public WebElement startWorkflowBtn;
048
049    @FindBy(xpath = WORKFLOW_SELECTOR_XPATH)
050    public WebElement workflowSelector;
051
052    @FindBy(xpath = "//form[contains(@id, 'nxl_grid_summary_layout:nxw_summary_current_document_single_tasks')]")
053    public WebElement workflowTasksForm;
054
055    @FindBy(xpath = "//div[@class='nxw_lastContributor']")
056    public WebElement lastContributor;
057
058    @FindBy(xpath = "//div[@class='nxw_author']")
059    public WebElement creator;
060
061    @FindBy(xpath = "//span[@id='nxl_grid_summary_layout:nxw_summary_current_document_dublincore_form:nxl_dublincore:nxw_created']")
062    public WebElement createdAt;
063
064    @FindBy(xpath = "//span[@id='nxl_grid_summary_layout:nxw_summary_current_document_dublincore_form:nxl_dublincore:nxw_modified']")
065    public WebElement lastModifiedAt;
066
067    @FindBy(xpath = "//span[@class[starts-with(.,'nxw_contributors_')]]")
068    public List<WebElement> contributors;
069
070    @FindBy(xpath = "//form[@id='nxl_grid_summary_layout:nxw_summary_current_document_states_form']")
071    public WebElement lifeCycleState;
072
073    @Required
074    @FindBy(xpath = "//div[@class='publication_block']")
075    public WebElement publicationBlock;
076
077    @FindBy(xpath = "//span[@class=\"versionNumber\"]")
078    public WebElement versionNumberField;
079
080    public SummaryTabSubPage(WebDriver driver) {
081        super(driver);
082    }
083
084    public SummaryTabSubPage startDefaultWorkflow() {
085        AjaxRequestManager a = new AjaxRequestManager(driver);
086        a.watchAjaxRequests();
087        selectItemInDropDownMenu(workflowSelector, "Serial document review");
088        a.waitForAjaxRequests();
089        Locator.waitUntilEnabledAndClick(startWorkflowBtn);
090        return asPage(SummaryTabSubPage.class);
091    }
092
093    public SummaryTabSubPage startDefaultParallelWorkflow() {
094        selectItemInDropDownMenu(workflowSelector, "Parallel document review");
095        Locator.waitUntilEnabledAndClick(startWorkflowBtn);
096        return asPage(SummaryTabSubPage.class);
097    }
098
099    public boolean workflowAlreadyStarted() {
100        return findElementWithTimeout(
101                By.xpath("//*[@id='nxl_grid_summary_layout:nxw_summary_document_route_form']")).getText().contains(
102                        "review has been started");
103    }
104
105    public boolean openTaskForCurrentUser() {
106        return findElementWithTimeout(By.xpath(
107                "//form[contains(@id, 'nxl_grid_summary_layout:nxw_summary_current_document_single_tasks')]")).getText()
108                                                                                                              .contains(
109                                                                                                                      "Please accept or reject the document");
110    }
111
112    /**
113     * @since 5.8
114     */
115    public boolean parallelOpenTaskForCurrentUser() {
116        return findElementWithTimeout(By.xpath(
117                "//form[contains(@id, 'nxl_grid_summary_layout:nxw_summary_current_document_single_tasks')]")).getText()
118                                                                                                              .contains(
119                                                                                                                      "Please give your opinion. Click on N/A if you have no advice.");
120    }
121
122    public WorkflowTabSubPage getWorkflow() {
123        clickOnTabIfNotSelected("nxw_documentTabs_panel", "nxw_TAB_ROUTE_WORKFLOW");
124        return asPage(WorkflowTabSubPage.class);
125    }
126
127    public boolean cantStartWorkflow() {
128        return findElementWithTimeout(By.xpath(
129                "//form[contains(@id, 'nxl_grid_summary_layout:nxw_summary_document_route_form')]")).getText().contains(
130                        "No workflow process can be started on this document.");
131    }
132
133    /**
134     * Get the creator of the doc.
135     *
136     * @since 5.8
137     */
138    public String getCreator() {
139        return creator.getText();
140    }
141
142    /**
143     * Get the last contributor of the doc.
144     *
145     * @since 5.8
146     */
147    public String getLastContributor() {
148        return lastContributor.getText();
149    }
150
151    /**
152     * Get the list of contributors of the doc.
153     *
154     * @since 5.8
155     */
156    public List<String> getContributors() {
157        List<String> result = new ArrayList<String>();
158        for (WebElement contributor : contributors) {
159            result.add(contributor.getText());
160        }
161        return result;
162    }
163
164    /**
165     * @since 5.8
166     */
167    public String getCurrentLifeCycleState() {
168        return lifeCycleState.findElement(By.className("sticker")).getText();
169    }
170
171    /**
172     * @since 5.9.3
173     */
174    public boolean isCollectionsFormDisplayed() {
175        try {
176            driver.findElement(By.id(COLLECTIONS_FORM_ID));
177            return true;
178        } catch (NoSuchElementException e) {
179            return false;
180        }
181    }
182
183    /**
184     * @since 5.9.3
185     */
186    public int getCollectionCount() {
187        return driver.findElement(By.id(COLLECTIONS_FORM_ID))
188                     .findElements(By.xpath(
189                             "div/span[@id='nxl_grid_summary_layout:nxw_summary_current_document_collections_form:collections']/span[@class='tag tagLink']"))
190                     .size();
191    }
192
193    /**
194     * @since 8.3
195     */
196    public boolean isPublished() {
197        try {
198            return publicationBlock.getText().contains("This document is published.");
199        } catch (NoSuchElementException e) {
200            // no publication block
201            return false;
202        }
203    }
204
205    /**
206     * @since 8.3
207     */
208    public boolean isAwaitingPublication() {
209        try {
210            return publicationBlock.getText().contains("This document is waiting for a publication approval.");
211        } catch (NoSuchElementException e) {
212            // no publication block
213            return false;
214        }
215    }
216
217    /**
218     * @since 8.3
219     */
220    public boolean hasApprovePublicationButton() {
221        try {
222            return publicationBlock.findElement(By.xpath(".//input[@value='Approve']")) != null;
223        } catch (NoSuchElementException e) {
224            return false;
225        }
226    }
227
228    /**
229     * @since 8.3
230     */
231    public SummaryTabSubPage approvePublication() {
232        Locator.findElementWaitUntilEnabledAndClick(publicationBlock, By.xpath(".//input[@value='Approve']"));
233        return asPage(SummaryTabSubPage.class);
234    }
235
236    /**
237     * @since 8.3
238     */
239    public boolean hasRejectPublicationComment() {
240        try {
241            return publicationBlock.findElement(By.xpath(".//*[contains(@name, 'rejectPublishingComment')]")) != null;
242        } catch (NoSuchElementException e) {
243            return false;
244        }
245    }
246
247    /**
248     * @since 8.3
249     */
250    public boolean hasRejectPublicationButton() {
251        try {
252            return publicationBlock.findElement(By.xpath(".//input[@value='Reject']")) != null;
253        } catch (NoSuchElementException e) {
254            return false;
255        }
256    }
257
258    /**
259     * @since 8.3
260     */
261    public void rejectPublication(String comment) {
262        WebElement text = publicationBlock.findElement(By.xpath(".//*[contains(@name, 'rejectPublishingComment')]"));
263        text.sendKeys(comment);
264        Locator.findElementWaitUntilEnabledAndClick(publicationBlock, By.xpath(".//input[@value='Reject']"));
265    }
266
267    /**
268     * @since 9.1
269     */
270    public String getVersionNumberText() {
271        return versionNumberField.getText();
272    }
273}