1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.appwithinminutes.test.po

File ClassFieldEditPane.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
40
17
1
260
109
18
0.45
2.35
17
1.06

Classes

Class Line # Actions
ClassFieldEditPane 36 40 0% 18 2
0.964912396.5%
 

Contributing tests

This file is covered by 30 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.appwithinminutes.test.po;
21   
22    import org.openqa.selenium.By;
23    import org.openqa.selenium.JavascriptExecutor;
24    import org.openqa.selenium.NoSuchElementException;
25    import org.openqa.selenium.WebElement;
26    import org.openqa.selenium.interactions.Actions;
27    import org.xwiki.test.ui.po.BaseElement;
28    import org.xwiki.test.ui.po.ConfirmationBox;
29   
30    /**
31    * Represents the pane used to edit a class field.
32    *
33    * @version $Id: dfbfff935694c1e03c02b4233d65ab104d557613 $
34    * @since 4.2M1
35    */
 
36    public class ClassFieldEditPane extends BaseElement
37    {
38    /**
39    * The name of the edited field.
40    */
41    private final String fieldName;
42   
43    /**
44    * The element that wraps the class field edit pane.
45    */
46    private final WebElement container;
47   
48    /**
49    * The field tool box. Includes the icons for moving and deleting a field, as well as the icon to show/hide the
50    * field configuration panel.
51    */
52    private final WebElement toolBox;
53   
54    /**
55    * Creates a new instance that can be used to edit the specified class field.
56    *
57    * @param fieldName the field name
58    */
 
59  71 toggle public ClassFieldEditPane(String fieldName)
60    {
61  71 this.fieldName = fieldName;
62   
63    // Wait for the element to become visible.
64  71 By containerLocator = By.id("field-" + fieldName);
65  71 getDriver().waitUntilElementIsVisible(containerLocator);
66   
67  71 container = getDriver().findElement(containerLocator);
68  71 toolBox = container.findElement(By.className("toolBox"));
69    }
70   
71    /**
72    * Sets the field pretty name.
73    *
74    * @param prettyName the new field pretty name
75    */
 
76  11 toggle public void setPrettyName(String prettyName)
77    {
78  11 WebElement prettyNameInput = getPropertyInput("prettyName");
79  11 prettyNameInput.clear();
80  11 prettyNameInput.sendKeys(prettyName);
81    }
82   
83    /**
84    * Sets the field default value
85    *
86    * @param defaultValue the new field default value
87    */
 
88  8 toggle public void setDefaultValue(String defaultValue)
89    {
90  8 WebElement defaultValueInput = getDefaultValueInput();
91  8 defaultValueInput.clear();
92  8 defaultValueInput.sendKeys(defaultValue);
93    }
94   
95    /**
96    * @return the default value of this field
97    */
 
98  4 toggle public String getDefaultValue()
99    {
100  4 return getDefaultValueInput().getAttribute("value");
101    }
102   
103    /**
104    * @return the element used to input the default field value
105    */
 
106  27 toggle protected WebElement getDefaultValueInput()
107    {
108    // Workaround for the fact that ends-with XPath function is not implemented.
109    // substring(@id, string-length(@id) - string-length(suffix) + 1)
110  27 String xpath = ".//*[substring(@id, string-length(@id) - %s - 2) = '_0_%s']";
111  27 try {
112  27 return getDriver().findElementWithoutWaiting(container,
113    By.xpath(String.format(xpath, fieldName.length(), fieldName)));
114    } catch (NoSuchElementException e) {
115    // Return the first input element from the field display. This is needed for the Title and Content fields.
116  4 return getDriver().findElementWithoutWaiting(container,
117    By.xpath(".//dl[@class = 'field-viewer']/dd//*[@name]"));
118    }
119    }
120   
121    /**
122    * Opens the field configuration panel.
123    */
 
124  24 toggle public void openConfigPanel()
125    {
126  24 clickToolBoxIcon("Configure");
127    }
128   
129    /**
130    * Closes the field configuration panel.
131    */
 
132  7 toggle public void closeConfigPanel()
133    {
134  7 clickToolBoxIcon("Preview");
135  7 String previewXPath = "//*[@id = 'field-" + fieldName + "']//dl[@class = 'field-viewer']/dd";
136  7 getDriver().waitUntilElementHasAttributeValue(By.xpath(previewXPath), "class", "");
137    }
138   
139    /**
140    * Clicks on the specified icon from the field tool box.
141    *
142    * @param alt the alternative text of the tool box icon to be clicked
143    */
 
144  33 toggle private void clickToolBoxIcon(String alt)
145    {
146    // This doesn't trigger the :hover CSS pseudo class so we're forced to manually set the display of the tool box.
147  33 new Actions(getDriver()).moveToElement(container).perform();
148   
149    // FIXME: The following line is a hack to overcome the fact that the previous line doesn't trigger the :hover
150    // CSS pseudo class on the field container (even if the mouse if moved over it).
151  33 showToolBox();
152   
153  33 toolBox.findElement(By.xpath("img[@alt = '" + alt + "']")).click();
154   
155    // Reset the tool box display. Remove this line when the :hover CSS class will be triggered by mouse over.
156  33 hideToolBox();
157    }
158   
159    /**
160    * Workaround for the fact that we can't yet hover the field container. We can move the mouse over the field
161    * container but its :hover CSS class is not triggered so the tool box stays hidden.
162    */
 
163  34 toggle private void showToolBox()
164    {
165  34 ((JavascriptExecutor) getDriver()).executeScript("arguments[0].style.display = 'block';", toolBox);
166    }
167   
168    /**
169    * Resets the tool box display.
170    *
171    * @see #showToolBox()
172    */
 
173  34 toggle private void hideToolBox()
174    {
175  34 ((JavascriptExecutor) getDriver()).executeScript("arguments[0].style.display = '';", toolBox);
176    }
177   
178    /**
179    * Sets the field name
180    *
181    * @param fieldName the new field name
182    */
 
183  16 toggle public void setName(String fieldName)
184    {
185  16 WebElement nameInput = getPropertyInput("name");
186  16 nameInput.clear();
187  16 nameInput.sendKeys(fieldName);
188    }
189   
190    /**
191    * @return the current value of the 'name' field meta property
192    */
 
193  12 toggle public String getName()
194    {
195  12 return getPropertyInput("name").getAttribute("value");
196    }
197   
198    /**
199    * @param propertyName the name of a class field meta property
200    * @return the form input used to edit the value of the specified meta property
201    */
 
202  59 toggle protected WebElement getPropertyInput(String propertyName)
203    {
204  59 return container.findElement(By.id(String.format("field-%s_%s", this.fieldName, propertyName)));
205    }
206   
207    /**
208    * Clicks on the delete field icon.
209    *
210    * @return the confirmation box to confirm the field delete
211    */
 
212  2 toggle public ConfirmationBox delete()
213    {
214  2 clickToolBoxIcon("Delete");
215  2 return new ConfirmationBox();
216    }
217   
218    /**
219    * Drag this field over the specified element. Use this method to reorder class fields.
220    *
221    * @param element the element to drag this field to
222    * @param xOffset offset from the top-left corner of the given element; a negative value means coordinates right
223    * from the given element
224    * @param yOffset offset from the top-left corner of the given element; a negative value means coordinates above the
225    * given element
226    */
 
227  1 toggle public void dragTo(WebElement element, int xOffset, int yOffset)
228    {
229    // This doesn't trigger the :hover CSS pseudo class so we're forced to manually set the display of the tool box.
230  1 new Actions(getDriver()).moveToElement(container).perform();
231   
232    // FIXME: The following line is a hack to overcome the fact that the previous line doesn't trigger the :hover
233    // CSS pseudo class on the field container (even if the mouse if moved over it).
234  1 showToolBox();
235   
236  1 WebElement dragHandler = toolBox.findElement(By.xpath("img[@alt = 'Move']"));
237  1 new Actions(getDriver()).clickAndHold(dragHandler).moveToElement(element, xOffset, yOffset).release().perform();
238   
239    // Reset the tool box display. Remove this line when the :hover CSS class will be triggered by mouse over.
240  1 hideToolBox();
241    }
242   
243    /**
244    * @return the element that wraps the class field edit pane
245    */
 
246  4 toggle protected WebElement getContainer()
247    {
248  4 return container;
249    }
250   
251    /**
252    * Do not mistaken this with {@link #getName()}.
253    *
254    * @return the name of the edited field
255    */
 
256  0 toggle protected String getFieldName()
257    {
258  0 return fieldName;
259    }
260    }