| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
|
| 35 |
|
|
| 36 |
|
@version |
| 37 |
|
@since |
| 38 |
|
|
| |
|
| 93.3% |
Uncovered Elements: 3 (45) |
Complexity: 15 |
Complexity Density: 0.56 |
|
| 39 |
|
public class FormElement extends BaseElement |
| 40 |
|
{ |
| 41 |
|
private final WebElement form; |
| 42 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 43 |
141 |
public FormElement(WebElement form)... |
| 44 |
|
{ |
| 45 |
141 |
this.form = form; |
| 46 |
|
} |
| 47 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 48 |
4 |
protected WebElement getForm()... |
| 49 |
|
{ |
| 50 |
4 |
return this.form; |
| 51 |
|
} |
| 52 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 53 |
61 |
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 |
|
|
| |
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 63 |
61 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 75 |
41 |
public String getFieldValue(By findElementBy)... |
| 76 |
|
{ |
| 77 |
41 |
return this.form.findElement(findElementBy).getAttribute("value"); |
| 78 |
|
} |
| 79 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 80 |
46 |
public void setFieldValue(By findElementBy, String value)... |
| 81 |
|
{ |
| 82 |
46 |
setFieldValue(this.form.findElement(findElementBy), value); |
| 83 |
|
} |
| 84 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
| 85 |
308 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 98 |
2 |
public void setCheckBox(By findElementBy, boolean checked)... |
| 99 |
|
{ |
| 100 |
2 |
setCheckBox(form.findElement(findElementBy), checked); |
| 101 |
|
} |
| 102 |
|
|
| |
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 103 |
15 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 116 |
5 |
public SelectElement getSelectElement(By by)... |
| 117 |
|
{ |
| 118 |
5 |
return this.new SelectElement(this.form.findElement(by)); |
| 119 |
|
} |
| 120 |
|
|
| |
|
| 73.5% |
Uncovered Elements: 13 (49) |
Complexity: 16 |
Complexity Density: 0.57 |
|
| 121 |
|
public class SelectElement extends BaseElement |
| 122 |
|
{ |
| 123 |
|
private final WebElement select; |
| 124 |
|
|
| 125 |
|
private Map<String, WebElement> optionsByValue; |
| 126 |
|
|
| |
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 127 |
5 |
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 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 135 |
0 |
public Set<String> getOptions()... |
| 136 |
|
{ |
| 137 |
0 |
return getOptionsByValue().keySet(); |
| 138 |
|
} |
| 139 |
|
|
| |
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 140 |
5 |
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 |
|
|
| |
|
| 63.6% |
Uncovered Elements: 4 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
| 153 |
4 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 168 |
4 |
public void select(final String value)... |
| 169 |
|
{ |
| 170 |
4 |
select(new ArrayList<String>() |
| 171 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 172 |
4 |
{... |
| 173 |
4 |
add(value); |
| 174 |
|
} |
| 175 |
|
}); |
| 176 |
|
} |
| 177 |
|
|
| |
|
| 70% |
Uncovered Elements: 3 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 178 |
1 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 192 |
1 |
public void unSelect(final String value)... |
| 193 |
|
{ |
| 194 |
1 |
unSelect(new ArrayList<String>() |
| 195 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 196 |
1 |
{... |
| 197 |
1 |
add(value); |
| 198 |
|
} |
| 199 |
|
}); |
| 200 |
|
} |
| 201 |
|
} |
| 202 |
|
} |