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