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 *     <a href="mailto:grenard@nuxeo.com">Guillaume</a>
019 *     Yannis JULIENNE
020 */
021package org.nuxeo.functionaltests.pages.admincenter;
022
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.commons.lang3.StringUtils;
027import org.nuxeo.functionaltests.AjaxRequestManager;
028import org.nuxeo.functionaltests.Locator;
029import org.nuxeo.functionaltests.Required;
030import org.nuxeo.functionaltests.fragment.NewVocabularyEntryForm;
031import org.openqa.selenium.Alert;
032import org.openqa.selenium.By;
033import org.openqa.selenium.NoSuchElementException;
034import org.openqa.selenium.WebDriver;
035import org.openqa.selenium.WebElement;
036import org.openqa.selenium.support.FindBy;
037import org.openqa.selenium.support.ui.Select;
038
039public class VocabulariesPage extends AdminCenterBasePage {
040
041    @FindBy(linkText = "Add a New Vocabulary Entry")
042    @Required
043    WebElement addNewEntryLink;
044
045    @FindBy(id = "selectDirectoryForm:directoriesList")
046    @Required
047    WebElement directoriesListSelectElement;
048
049    @FindBy(id = "viewDirectoryEntries")
050    @Required
051    WebElement directoryEntriesForm;
052
053    @FindBy(xpath = "//form[@id='viewDirectoryEntries']//thead")
054    @Required
055    WebElement directoryEntriesHeader;
056
057    public VocabulariesPage(WebDriver driver) {
058        super(driver);
059    }
060
061    /**
062     * @return
063     * @since 5.9.3
064     */
065    public VocabulariesPage addEntry(final String entryId, final String parentId, final String entryEnglishLabel,
066            final String entryFrenchLabel, final boolean obsolete, final int order) {
067        AjaxRequestManager arm = new AjaxRequestManager(driver);
068        arm.begin();
069        Locator.waitUntilEnabledAndClick(addNewEntryLink);
070        arm.end();
071        NewVocabularyEntryForm newVocabularyEntryForm = getWebFragment(By.id("addEntryView:addEntryForm"),
072                NewVocabularyEntryForm.class);
073        newVocabularyEntryForm.setNewVocabularyId(entryId);
074        newVocabularyEntryForm.setNewVocabularyEnglishLabel(entryEnglishLabel);
075        newVocabularyEntryForm.setNewVocabularyFrenchLabel(entryFrenchLabel);
076        newVocabularyEntryForm.setNewVocabularyObsolete(obsolete);
077        newVocabularyEntryForm.setNewVocabularyOrder(order);
078        if (!StringUtils.isBlank(parentId)) {
079            newVocabularyEntryForm.setNewVocabularyParentId(parentId);
080        }
081        arm.begin();
082        newVocabularyEntryForm.save();
083        Locator.waitForTextPresent(By.id("ambiance-notification"), "Vocabulary entry added");
084        arm.end();
085        return asPage(VocabulariesPage.class);
086    }
087
088    /**
089     * @since 5.9.3
090     */
091    public VocabulariesPage deleteEntry(final String entryId) {
092        WebElement entryToBeDeleted = getDirectoryEntryRow(entryId);
093        WebElement entryDeleteButton = entryToBeDeleted.findElement(By.xpath("td/input[@value='Delete']"));
094        AjaxRequestManager arm = new AjaxRequestManager(driver);
095        arm.begin();
096        Locator.waitUntilEnabledAndClick(entryDeleteButton);
097        Alert confirmRemove = driver.switchTo().alert();
098        confirmRemove.accept();
099        Locator.waitForTextPresent(By.id("ambiance-notification"), "Vocabulary entry deleted");
100        arm.end();
101        return asPage(VocabulariesPage.class);
102    }
103
104    /**
105     * Return the list of directories in the select box
106     */
107    public List<String> getDirectoriesList() {
108        Select directoriesListSelect = new Select(directoriesListSelectElement);
109        ArrayList<String> directoryList = new ArrayList<String>();
110        List<WebElement> list = directoriesListSelect.getOptions();
111        for (WebElement webElement : list) {
112            directoryList.add(webElement.getText());
113        }
114        return directoryList;
115    }
116
117    /**
118     * @since 5.9.3
119     */
120    public WebElement getDirectoryEntryRow(final String entryId) {
121        List<WebElement> entryElementList = directoryEntriesForm.findElements(By.xpath("table/tbody/tr"));
122        for (WebElement entryElement : entryElementList) {
123            WebElement entryIdElement = entryElement.findElement(By.xpath("td[2]/span"));
124            if (entryId.equals(entryIdElement.getText())) {
125                return entryElement;
126            }
127        }
128        throw new NoSuchElementException(String.format("Vocabulary entry with id %s not found", entryId));
129    }
130
131    /**
132     * @since 5.9.3
133     */
134    public boolean hasEntry(final String entryId) {
135        try {
136            getDirectoryEntryRow(entryId);
137            return true;
138        } catch (NoSuchElementException e) {
139            return false;
140        }
141    }
142
143    /**
144     * Select one of the directory in the select box
145     *
146     * @param directoryName
147     */
148    public VocabulariesPage select(String directoryName) {
149        Select directoriesListSelect = new Select(directoriesListSelectElement);
150        List<WebElement> list = directoriesListSelect.getOptions();
151        for (WebElement webElement : list) {
152            if (directoryName.trim().equals(webElement.getText().trim())) {
153                AjaxRequestManager a = new AjaxRequestManager(driver);
154                a.watchAjaxRequests();
155                directoriesListSelect.selectByVisibleText(webElement.getText());
156                a.waitForAjaxRequests();
157                return asPage(VocabulariesPage.class);
158            }
159        }
160        throw new NoSuchElementException(String.format("directoryName %s not available", directoryName));
161    }
162
163    /**
164     * Returns true if the directory entries table contains given string in its header.
165     *
166     * @since 7.1
167     */
168    public boolean hasHeaderLabel(String headerLabel) {
169        return directoryEntriesHeader.getText().contains(headerLabel);
170    }
171
172}