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</a>
018 */
019package org.nuxeo.functionaltests.pages.tabs;
020
021import static org.junit.Assert.assertEquals;
022
023import java.util.List;
024
025import org.nuxeo.functionaltests.AjaxRequestManager;
026import org.nuxeo.functionaltests.Required;
027import org.nuxeo.functionaltests.pages.AbstractPage;
028import org.openqa.selenium.Alert;
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.9.3
036 */
037public class TrashSubPage extends AbstractPage {
038
039    protected static final String SELECT_ALL_BUTTON_ID = "document_trash_content:nxl_document_listing_table:listing_table_selection_box_with_current_document_header";
040
041    protected static final String PERMANENT_DELETE_BUTTON_ID = "document_trash_content_buttons:nxw_CURRENT_SELECTION_DELETE_form:nxw_CURRENT_SELECTION_DELETE";
042
043    protected static final String EMPTY_TRASH_BUTTON_ID = "document_trash_content_buttons:nxw_CURRENT_SELECTION_EMPTY_TRASH_form:nxw_CURRENT_SELECTION_EMPTY_TRASH";
044
045    @FindBy(xpath = "//form[@id=\"document_trash_content\"]//tbody//tr")
046    List<WebElement> childDocumentRows;
047
048    public List<WebElement> getChildDocumentRows() {
049        return childDocumentRows;
050    }
051
052    @Required
053    @FindBy(id = "cv_document_trash_content_0_resultsPanel")
054    protected WebElement documentContentForm;
055
056    public TrashSubPage(WebDriver driver) {
057        super(driver);
058    }
059
060    public TrashSubPage emptyTrash() {
061        findElementWaitUntilEnabledAndClick(By.id(EMPTY_TRASH_BUTTON_ID));
062        Alert alert = driver.switchTo().alert();
063        assertEquals("Permanently delete all documents in trash?", alert.getText());
064        alert.accept();
065        return asPage(TrashSubPage.class);
066    }
067
068    /**
069     * Removes all documents visible on current page.
070     */
071    public TrashSubPage purgeAllDocuments() {
072        TrashSubPage page = asPage(TrashSubPage.class);
073        By locator = By.id(SELECT_ALL_BUTTON_ID);
074        if (!hasElement(locator)) {
075            // no documents to remove
076            return page;
077        }
078        AjaxRequestManager arm = new AjaxRequestManager(driver);
079        arm.begin();
080        findElementWaitUntilEnabledAndClick(By.id(SELECT_ALL_BUTTON_ID));
081        arm.end();
082
083        deleteSelectedDocuments();
084
085        return asPage(TrashSubPage.class);
086    }
087
088    protected void deleteSelectedDocuments() {
089        findElementWaitUntilEnabledAndClick(By.id(PERMANENT_DELETE_BUTTON_ID));
090        Alert alert = driver.switchTo().alert();
091        assertEquals("Permanently delete selected document(s)?", alert.getText());
092        alert.accept();
093    }
094
095}