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