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