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

File FormElement.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

20
55
19
2
202
154
31
0.56
2.89
9.5
1.63

Classes

Class Line # Actions
FormElement 39 27 0% 15 3
0.9333333493.3%
FormElement.SelectElement 121 28 0% 16 13
0.734693973.5%
 

Contributing tests

This file is covered by 47 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.test.ui.po;
21   
22    import java.util.ArrayList;
23    import java.util.HashMap;
24    import java.util.List;
25    import java.util.Map;
26    import java.util.Set;
27   
28    import org.openqa.selenium.By;
29    import org.openqa.selenium.WebDriverException;
30    import org.openqa.selenium.WebElement;
31    import org.openqa.selenium.support.ui.Select;
32   
33    /**
34    * Represents a Form.
35    *
36    * @version $Id: ed4662400f146bbe66036ddbd98d95eea259f83c $
37    * @since 3.2M3
38    */
 
39    public class FormElement extends BaseElement
40    {
41    private final WebElement form;
42   
 
43  141 toggle public FormElement(WebElement form)
44    {
45  141 this.form = form;
46    }
47   
 
48  4 toggle protected WebElement getForm()
49    {
50  4 return this.form;
51    }
52   
 
53  61 toggle public void fillFieldsByName(Map<String, String> valuesByNames)
54    {
55  61 Map<WebElement, String> valuesByElements = new HashMap<WebElement, String>((int) (valuesByNames.size() / 0.75));
56   
57  61 for (String name : valuesByNames.keySet()) {
58  211 valuesByElements.put(this.form.findElement(By.name(name)), valuesByNames.get(name));
59    }
60  61 fillFieldsByElements(valuesByElements);
61    }
62   
 
63  61 toggle public void fillFieldsByElements(Map<WebElement, String> valuesByElements)
64    {
65  61 for (WebElement el : valuesByElements.keySet()) {
66  211 try {
67  211 setFieldValue(el, valuesByElements.get(el));
68    } catch (Exception e) {
69  0 throw new WebDriverException("Couldn't set field \"" + el.getAttribute("name") + "\" to value \""
70    + valuesByElements.get(el) + "\"", e);
71    }
72    }
73    }
74   
 
75  41 toggle public String getFieldValue(By findElementBy)
76    {
77  41 return this.form.findElement(findElementBy).getAttribute("value");
78    }
79   
 
80  46 toggle public void setFieldValue(By findElementBy, String value)
81    {
82  46 setFieldValue(this.form.findElement(findElementBy), value);
83    }
84   
 
85  308 toggle public void setFieldValue(WebElement fieldElement, String value)
86    {
87  308 if ("checkbox".equals(fieldElement.getAttribute("type"))) {
88  13 setCheckBox(fieldElement, value.equals("true"));
89  295 } else if ("select".equals(fieldElement.getTagName())) {
90  43 Select select = new Select(fieldElement);
91  43 select.selectByValue(value);
92    } else {
93  252 fieldElement.clear();
94  252 fieldElement.sendKeys(value);
95    }
96    }
97   
 
98  2 toggle public void setCheckBox(By findElementBy, boolean checked)
99    {
100  2 setCheckBox(form.findElement(findElementBy), checked);
101    }
102   
 
103  15 toggle public void setCheckBox(WebElement checkBoxElement, boolean checked)
104    {
105  15 int x = 0;
106  27 while (checkBoxElement.isSelected() != checked) {
107  12 checkBoxElement.click();
108  12 if (x == 100) {
109  0 throw new WebDriverException("Unable to set checkbox at " + checkBoxElement.getAttribute("name")
110    + " to " + checked);
111    }
112  12 x++;
113    }
114    }
115   
 
116  5 toggle public SelectElement getSelectElement(By by)
117    {
118  5 return this.new SelectElement(this.form.findElement(by));
119    }
120   
 
121    public class SelectElement extends BaseElement
122    {
123    private final WebElement select;
124   
125    private Map<String, WebElement> optionsByValue;
126   
 
127  5 toggle public SelectElement(WebElement select)
128    {
129  5 if (!select.getTagName().toLowerCase().equals("select")) {
130  0 throw new WebDriverException("Can only create a select element from a webelement of tag name select.");
131    }
132  5 this.select = select;
133    }
134   
 
135  0 toggle public Set<String> getOptions()
136    {
137  0 return getOptionsByValue().keySet();
138    }
139   
 
140  5 toggle private Map<String, WebElement> getOptionsByValue()
141    {
142  5 if (this.optionsByValue != null) {
143  0 return this.optionsByValue;
144    }
145  5 List<WebElement> elements = this.select.findElements(By.tagName("option"));
146  5 this.optionsByValue = new HashMap<String, WebElement>((int) (elements.size() / 0.75));
147  5 for (WebElement el : elements) {
148  31 this.optionsByValue.put(el.getAttribute("value"), el);
149    }
150  5 return this.optionsByValue;
151    }
152   
 
153  4 toggle public void select(List<String> valuesToSelect)
154    {
155  4 if (valuesToSelect.size() > 1 && this.select.getAttribute("multiple") != "multiple") {
156  0 throw new WebDriverException("Cannot select multiple elements in drop down menu.");
157    }
158  4 Map<String, WebElement> optionsByValue = getOptionsByValue();
159  4 if (!optionsByValue.keySet().containsAll(valuesToSelect)) {
160  0 throw new WebDriverException("Select Element(s): " + optionsByValue.keySet().retainAll(valuesToSelect)
161    + " not found.");
162    }
163  4 for (String label : valuesToSelect) {
164  4 optionsByValue.get(label).click();
165    }
166    }
167   
 
168  4 toggle public void select(final String value)
169    {
170  4 select(new ArrayList<String>()
171    {
 
172  4 toggle {
173  4 add(value);
174    }
175    });
176    }
177   
 
178  1 toggle public void unSelect(List<String> valuesToUnSelect)
179    {
180  1 Map<String, WebElement> optionsByValue = getOptionsByValue();
181  1 if (!optionsByValue.keySet().containsAll(valuesToUnSelect)) {
182  0 throw new WebDriverException("Select Element(s) to unselect: "
183    + optionsByValue.keySet().retainAll(valuesToUnSelect) + " not found.");
184    }
185  1 for (String label : valuesToUnSelect) {
186  1 if (optionsByValue.get(label).isSelected()) {
187  1 optionsByValue.get(label).click();
188    }
189    }
190    }
191   
 
192  1 toggle public void unSelect(final String value)
193    {
194  1 unSelect(new ArrayList<String>()
195    {
 
196  1 toggle {
197  1 add(value);
198    }
199    });
200    }
201    }
202    }