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