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