001/*
002 * (C) Copyright 2015 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 *     Nelson Silva <nsilva@nuxeo.com>
018 */
019package org.nuxeo.functionaltests.forms;
020
021import java.util.List;
022
023import org.nuxeo.functionaltests.JSListRequestManager;
024import org.nuxeo.functionaltests.Locator;
025import org.openqa.selenium.By;
026import org.openqa.selenium.WebDriver;
027import org.openqa.selenium.WebElement;
028
029/**
030 * Represents a list widget, with helper method to retrieve/check its subwidgets.
031 *
032 * @since 7.2
033 */
034public class JSListWidgetElement extends AbstractWidgetElement {
035
036    public static enum Display {
037        BLOCK_LEFT, BLOCK_TOP, TABLE, INLINE
038    }
039
040    /** The display attribute controls the rendering of subwidgets. */
041    private final Display display;
042
043    public JSListWidgetElement(WebDriver driver, String id) {
044        this(driver, id, Display.BLOCK_LEFT);
045    }
046
047    public JSListWidgetElement(WebDriver driver, String id, Display display) {
048        super(driver, id);
049        this.display = display;
050    }
051
052    protected String getListSubElementSuffix(String subId, int index) {
053        return String.format("%s:%s", Integer.valueOf(index), subId);
054    }
055
056    public void addNewElement() {
057        WebElement addElement = getElement(id + "_add");
058        JSListRequestManager rm = new JSListRequestManager(driver);
059        rm.begin();
060        Locator.waitUntilEnabledAndClick(addElement);
061        rm.end();
062    }
063
064    public void removeElement(int index) {
065        WebElement delElement = getRowActions(index).findElement(By.className("deleteBtn"));
066        JSListRequestManager rm = new JSListRequestManager(driver);
067        rm.begin();
068        Locator.waitUntilEnabledAndClick(delElement);
069        rm.end();
070    }
071
072    public void moveUpElement(int index) {
073        WebElement moveElement = getRowActions(index).findElement(By.className("moveUpBtn"));
074        JSListRequestManager rm = new JSListRequestManager(driver);
075        rm.begin();
076        Locator.waitUntilEnabledAndClick(moveElement);
077        rm.end();
078    }
079
080    public void moveDownElement(int index) {
081        WebElement moveElement = getRowActions(index).findElement(By.className("moveDownBtn"));
082        JSListRequestManager rm = new JSListRequestManager(driver);
083        rm.begin();
084        Locator.waitUntilEnabledAndClick(moveElement);
085        rm.end();
086    }
087
088    public void waitForSubWidget(String id, int index) {
089        getSubElement(getListSubElementSuffix(id, index), true);
090    }
091
092    public WidgetElement getSubWidget(String id, int index) {
093        return getSubWidget(id, index, false);
094    }
095
096    /**
097     * @since 8.3
098     */
099    public String getSubWidgetId(String id, int index) {
100        return getSubWidgetId(id, index, false);
101    }
102
103    public WidgetElement getSubWidget(String id, int index, boolean wait) {
104        if (wait) {
105            waitForSubWidget(id, index);
106        }
107        return getWidget(getListSubElementSuffix(id, index));
108    }
109
110    /**
111     * @since 8.3
112     */
113    public String getSubWidgetId(String id, int index, boolean wait) {
114        return getSubWidget(id, index, wait).getId();
115    }
116
117    public <T> T getSubWidget(String id, int index, Class<T> widgetClassToProxy, boolean wait) {
118        if (wait) {
119            waitForSubWidget(id, index);
120        }
121        return getWidget(getListSubElementSuffix(id, index), widgetClassToProxy);
122    }
123
124    @Override
125    public String getMessageValue() {
126        return getMessageValue("_message");
127    }
128
129    protected WebElement getRowActions(int i) {
130        if (display == Display.TABLE || display == Display.INLINE) {
131            return driver.findElement(By.cssSelector(getRowCssSelector(i) + " > .listWidgetActions"));
132        }
133        return driver.findElement(By.cssSelector(getRowCssSelector(i) + " .listWidgetActionsTable"));
134    }
135
136    public String getSubWidgetMessageValue(String id, int idx) {
137        return getSubWidgetMessageValue(id, idx, 0);
138    }
139
140    public String getSubWidgetMessageValue(String id, int idx, int msgIdx) {
141        String subId = id + "_message";
142        if (msgIdx != 0) {
143            subId += "_" + msgIdx;
144        }
145        return getMessageValue(":" + getListSubElementSuffix(subId, idx));
146    }
147
148    public List<WebElement> getRows() {
149        return driver.findElements(By.cssSelector(getRowsCssSelector()));
150    }
151
152    private String getElementCssSelector() {
153        return "#" + id.replaceAll(":", "\\\\:") + "_panel";
154    }
155
156    private String getRowsCssSelector() {
157        String path = getElementCssSelector();
158        if (display == Display.TABLE || display == Display.INLINE) {
159            path += " > table > tbody";
160        }
161        return path + " > .listItem";
162    }
163
164    private String getRowCssSelector(int i) {
165        return getRowsCssSelector() + ":nth-of-type(" + (i + 1) + ")";
166    }
167}