001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Sun Seng David TAN <stan@nuxeo.com>
016 *     <a href="mailto:grenard@nuxeo.com">Guillaume</a>
017 */
018package org.nuxeo.functionaltests.pages.admincenter;
019
020import java.util.ArrayList;
021import java.util.List;
022
023import org.nuxeo.functionaltests.AjaxRequestManager;
024import org.nuxeo.functionaltests.Locator;
025import org.nuxeo.functionaltests.Required;
026import org.nuxeo.functionaltests.fragment.NewVocabularyEntryForm;
027import org.openqa.selenium.Alert;
028import org.openqa.selenium.By;
029import org.openqa.selenium.NoSuchElementException;
030import org.openqa.selenium.WebDriver;
031import org.openqa.selenium.WebElement;
032import org.openqa.selenium.support.FindBy;
033import org.openqa.selenium.support.ui.Select;
034
035public class VocabulariesPage extends AdminCenterBasePage {
036
037    @FindBy(linkText = "Add a New Vocabulary Entry")
038    @Required
039    WebElement addNewEntryLink;
040
041    @FindBy(id = "selectDirectoryForm:directoriesList")
042    @Required
043    WebElement directoriesListSelectElement;
044
045    @FindBy(id = "viewDirectoryEntries")
046    @Required
047    WebElement directoryEntriesForm;
048
049    @FindBy(xpath = "//form[@id='viewDirectoryEntries']//thead")
050    @Required
051    WebElement directoryEntriesHeader;
052
053    public VocabulariesPage(WebDriver driver) {
054        super(driver);
055    }
056
057    /**
058     * @return
059     * @since 5.9.3
060     */
061    public VocabulariesPage addEntry(final String entryId, final String parentId, final String entryEnglishLabel,
062            final String entryFrenchLabel, final boolean obsolete, final int order) {
063        addNewEntryLink.click();
064        NewVocabularyEntryForm newVocabularyEntryForm = getWebFragment(By.id("addEntryView:addEntryForm"),
065                NewVocabularyEntryForm.class);
066        newVocabularyEntryForm.setNewVocabularyId(entryId);
067        newVocabularyEntryForm.setNewVocabularyEnglishLabel(entryEnglishLabel);
068        newVocabularyEntryForm.setNewVocabularyFrenchLabel(entryFrenchLabel);
069        newVocabularyEntryForm.setNewVocabularyObsolete(obsolete);
070        newVocabularyEntryForm.setNewVocabularyOrder(order);
071        newVocabularyEntryForm.setNewVocabularyParentId(parentId);
072        newVocabularyEntryForm.save();
073        Locator.waitForTextPresent(By.id("ambiance-notification"), "Vocabulary entry added");
074        return asPage(VocabulariesPage.class);
075    }
076
077    /**
078     * @since 5.9.3
079     */
080    public VocabulariesPage deleteEntry(final String entryId) {
081        WebElement entryToBeDeleted = getDirectoryEntryRow(entryId);
082        WebElement entryDeleteButton = entryToBeDeleted.findElement(By.xpath("td/input[@value='Delete']"));
083        entryDeleteButton.click();
084        Alert confirmRemove = driver.switchTo().alert();
085        confirmRemove.accept();
086        Locator.waitForTextPresent(By.id("ambiance-notification"), "Vocabulary entry deleted");
087        return asPage(VocabulariesPage.class);
088    }
089
090    /**
091     * Return the list of directories in the select box
092     */
093    public List<String> getDirectoriesList() {
094        Select directoriesListSelect = new Select(directoriesListSelectElement);
095        ArrayList<String> directoryList = new ArrayList<String>();
096        List<WebElement> list = directoriesListSelect.getOptions();
097        for (WebElement webElement : list) {
098            directoryList.add(webElement.getText());
099        }
100        return directoryList;
101    }
102
103    /**
104     * @since 5.9.3
105     */
106    public WebElement getDirectoryEntryRow(final String entryId) {
107        List<WebElement> entryElementList = directoryEntriesForm.findElements(By.xpath("table/tbody/tr"));
108        for (WebElement entryElement : entryElementList) {
109            WebElement entryIdElement = entryElement.findElement(By.xpath("td[2]/span"));
110            if (entryId.equals(entryIdElement.getText())) {
111                return entryElement;
112            }
113        }
114        throw new NoSuchElementException(String.format("Vocabulary entry with id %s not found", entryId));
115    }
116
117    /**
118     * @since 5.9.3
119     */
120    public boolean hasEntry(final String entryId) {
121        try {
122            getDirectoryEntryRow(entryId);
123            return true;
124        } catch (NoSuchElementException e) {
125            return false;
126        }
127    }
128
129    /**
130     * Select one of the directory in the select box
131     *
132     * @param directoryName
133     */
134    public VocabulariesPage select(String directoryName) {
135        Select directoriesListSelect = new Select(directoriesListSelectElement);
136        List<WebElement> list = directoriesListSelect.getOptions();
137        for (WebElement webElement : list) {
138            if (directoryName.trim().equals(webElement.getText().trim())) {
139                AjaxRequestManager a = new AjaxRequestManager(driver);
140                a.watchAjaxRequests();
141                directoriesListSelect.selectByVisibleText(webElement.getText());
142                a.waitForAjaxRequests();
143                return asPage(VocabulariesPage.class);
144            }
145        }
146        throw new NoSuchElementException(String.format("directoryName %s not available", directoryName));
147    }
148
149    /**
150     * Returns true if the directory entries table contains given string in its header.
151     *
152     * @since 7.1
153     */
154    public boolean hasHeaderLabel(String headerLabel) {
155        return directoryEntriesHeader.getText().contains(headerLabel);
156    }
157
158}