001/*
002 * (C) Copyright 2014 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 */
019package org.nuxeo.functionaltests.fragment;
020
021import org.nuxeo.functionaltests.AbstractTest;
022import org.nuxeo.functionaltests.AjaxRequestManager;
023import org.nuxeo.functionaltests.Locator;
024import org.nuxeo.functionaltests.forms.Select2WidgetElement;
025import org.nuxeo.functionaltests.pages.DocumentBasePage;
026import org.openqa.selenium.By;
027import org.openqa.selenium.NoSuchElementException;
028import org.openqa.selenium.WebDriver;
029import org.openqa.selenium.WebElement;
030
031/**
032 * @since 5.9.3
033 */
034public class AddToCollectionForm extends WebFragmentImpl {
035
036    private final static String ADD_BUTTON_ID = "nxw_addToCollectionAction_after_view_fancy_subview:nxw_addToCollectionAction_after_view_fancyform:add";
037
038    private final static String S2_CHOOSE_COLLECTION_ID = "s2id_nxw_addToCollectionAction_after_view_fancy_subview:nxw_addToCollectionAction_after_view_fancyform:nxw_singleDocumentSuggestion_1_select2";
039
040    private final static String NEW_COLLECTION_DESCRIPTION_ID = "nxw_addToCollectionAction_after_view_fancy_subview:nxw_addToCollectionAction_after_view_fancyform:description";
041
042    private static final String EXISTING_COLLECTION_DESCRIPTION_ID = "nxw_addToCollectionAction_after_view_fancy_subview:nxw_addToCollectionAction_after_view_fancyform:scd";
043
044    protected boolean multiple = false;
045
046    public AddToCollectionForm(WebDriver driver, WebElement element) {
047        super(driver, element);
048    }
049
050    protected boolean isMultiple() {
051        return multiple;
052    }
053
054    public DocumentBasePage add() {
055        return add(DocumentBasePage.class);
056    }
057
058    public <T> T add(final Class<T> pageClassProxy) {
059        Locator.findElementWaitUntilEnabledAndClick(By.id(ADD_BUTTON_ID));
060        return AbstractTest.asPage(pageClassProxy);
061    }
062
063    protected String getAddButtonId() {
064        return ADD_BUTTON_ID;
065    }
066
067    protected String getChooseCollectionId() {
068        return S2_CHOOSE_COLLECTION_ID;
069    }
070
071    protected String getNewCollectionDescriptionId() {
072        return NEW_COLLECTION_DESCRIPTION_ID;
073    }
074
075    protected String getCollectionDescriptionId() {
076        return EXISTING_COLLECTION_DESCRIPTION_ID;
077    }
078
079    public void setCollection(final String collectionName) {
080        Locator.findElementAndWaitUntilEnabled(By.id(getChooseCollectionId()));
081        Select2WidgetElement s2Collection = new Select2WidgetElement(driver, getChooseCollectionId());
082        AjaxRequestManager arm = new AjaxRequestManager(driver);
083        arm.begin();
084        s2Collection.selectValue(collectionName, false, true);
085        arm.end();
086        Locator.findElementAndWaitUntilEnabled(By.id(getAddButtonId()));
087    }
088
089    public void setNewDescription(final String collectionDescription) {
090        Locator.findElementAndWaitUntilEnabled(By.id(getNewCollectionDescriptionId())).sendKeys(collectionDescription);
091    }
092
093    public boolean isNewDescriptionVisible() {
094        try {
095            driver.findElement(By.id(getNewCollectionDescriptionId()));
096            return true;
097        } catch (final NoSuchElementException e) {
098            return false;
099        }
100    }
101
102    public boolean isExistingDescriptionVisible() {
103        try {
104            driver.findElement(By.id(getCollectionDescriptionId()));
105            return true;
106        } catch (NoSuchElementException e) {
107            return false;
108        }
109    }
110
111    public String getExistingDescription() {
112        return driver.findElement(By.id(getCollectionDescriptionId())).getText();
113    }
114
115}