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 *     <a href="mailto:grenard@nuxeo.com">Guillaume</a>
018 *     Yannis JULIENNE
019 */
020package org.nuxeo.functionaltests.fragment;
021
022import java.util.Arrays;
023
024import org.nuxeo.functionaltests.Locator;
025import org.nuxeo.functionaltests.Required;
026import org.nuxeo.functionaltests.pages.AbstractPage;
027import org.openqa.selenium.By;
028import org.openqa.selenium.WebDriver;
029import org.openqa.selenium.WebElement;
030import org.openqa.selenium.support.FindBy;
031
032/**
033 * @since 5.9.3
034 */
035public class NewVocabularyEntryForm extends WebFragmentImpl {
036
037    @FindBy(id = "addEntryView:addEntryForm:nxl_l10nsubjects_vocabulary:nxw_l10nxvocabulary_label_en")
038    @Required
039    private WebElement englishLabelInput;
040
041    @FindBy(id = "addEntryView:addEntryForm:nxl_l10nsubjects_vocabulary:nxw_l10nxvocabulary_label_fr")
042    @Required
043    private WebElement frenchLabelInput;
044
045    @FindBy(id = "addEntryView:addEntryForm:nxl_l10nsubjects_vocabulary:nxw_l10nxvocabulary_id")
046    @Required
047    private WebElement idInput;
048
049    @FindBy(id = "nxw_parent_openPopup")
050    private WebElement newParentPopup;
051
052    @FindBy(id = "addEntryView:addEntryForm:nxl_l10nsubjects_vocabulary:nxw_l10nxvocabulary_obsolete_checkbox:0")
053    @Required
054    private WebElement notObsoleteInput;
055
056    @FindBy(id = "addEntryView:addEntryForm:nxl_l10nsubjects_vocabulary:nxw_l10nxvocabulary_obsolete_checkbox:1")
057    @Required
058    private WebElement obsoleteInput;
059
060    @FindBy(id = "addEntryView:addEntryForm:nxl_l10nsubjects_vocabulary:nxw_l10nxvocabulary_order")
061    @Required
062    private WebElement orderInput;
063
064    public NewVocabularyEntryForm(WebDriver driver, WebElement element) {
065        super(driver, element);
066    }
067
068    private void findParentNodeAndSelect(final WebElement selectParentFancyBox, final String[] path) {
069        if (path.length == 1) {
070            selectParentFancyBox.findElement(By.linkText(path[0])).click();
071        } else {
072            WebElement node = selectParentFancyBox.findElement(By.xpath("//table[contains(text(),'" + path[0] + "')]"));
073            node.findElement(By.xpath("tbody/tr[1]/td[1]/div/a")).click();
074            findParentNodeAndSelect(selectParentFancyBox, Arrays.copyOfRange(path, 1, path.length));
075        }
076    }
077
078    public void save() {
079        Locator.waitUntilEnabledAndClick(getElement().findElement(By.xpath("//input[@value='Create']")));
080    }
081
082    public void setNewVocabularyEnglishLabel(final String vocabularyEnglishLabel) {
083        englishLabelInput.sendKeys(vocabularyEnglishLabel);
084    }
085
086    public void setNewVocabularyFrenchLabel(final String vocabularyFrenchLabel) {
087        frenchLabelInput.sendKeys(vocabularyFrenchLabel);
088    }
089
090    public void setNewVocabularyId(final String vocabularyId) {
091        idInput.sendKeys(vocabularyId);
092    }
093
094    public void setNewVocabularyObsolete(final boolean obsolete) {
095        if (obsolete) {
096            Locator.waitUntilEnabledAndClick(obsoleteInput);
097        } else {
098            Locator.waitUntilEnabledAndClick(notObsoleteInput);
099        }
100    }
101
102    public void setNewVocabularyOrder(final int vocabularyOrder) {
103        orderInput.clear();
104        orderInput.sendKeys(vocabularyOrder + "");
105    }
106
107    public void setNewVocabularyParentId(final String parentLabelPath) {
108        Locator.waitUntilEnabledAndClick(newParentPopup);
109        WebElement selectParentFancyBox = AbstractPage.getFancyBoxContent();
110        String[] split = parentLabelPath.split("/");
111        findParentNodeAndSelect(selectParentFancyBox, split);
112    }
113
114}