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.Arrays; |
24 |
|
import java.util.HashMap; |
25 |
|
import java.util.List; |
26 |
|
import java.util.Map; |
27 |
|
|
28 |
|
import org.openqa.selenium.By; |
29 |
|
import org.openqa.selenium.NoSuchElementException; |
30 |
|
import org.openqa.selenium.WebDriver; |
31 |
|
import org.openqa.selenium.WebElement; |
32 |
|
import org.openqa.selenium.support.ui.ExpectedCondition; |
33 |
|
import org.xwiki.test.ui.XWikiWebDriver; |
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
@version |
39 |
|
@since |
40 |
|
@since |
41 |
|
|
|
|
| 93.4% |
Uncovered Elements: 5 (76) |
Complexity: 21 |
Complexity Density: 0.42 |
|
42 |
|
public class BootstrapSelect |
43 |
|
{ |
44 |
|
private WebElement element; |
45 |
|
|
46 |
|
private WebElement button; |
47 |
|
|
48 |
|
private WebElement menu; |
49 |
|
|
50 |
|
private WebElement hiddenSelect; |
51 |
|
|
52 |
|
private XWikiWebDriver driver; |
53 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
54 |
32 |
public BootstrapSelect(WebElement element, XWikiWebDriver driver)... |
55 |
|
{ |
56 |
32 |
this.driver = driver; |
57 |
32 |
this.element = element; |
58 |
32 |
this.button = element.findElement(By.tagName("button")); |
59 |
32 |
this.menu = element.findElement(By.cssSelector(".dropdown-menu")); |
60 |
32 |
this.hiddenSelect = element.findElement(By.tagName("select")); |
61 |
|
} |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
63 |
32 |
public boolean isMultiple()... |
64 |
|
{ |
65 |
32 |
String value = this.hiddenSelect.getAttribute("multiple"); |
66 |
32 |
return value != null && !"false".equals(value); |
67 |
|
} |
68 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
69 |
27 |
public void selectByValue(String value)... |
70 |
|
{ |
71 |
27 |
selectByValues(Arrays.asList(value)); |
72 |
|
} |
73 |
|
|
|
|
| 88.6% |
Uncovered Elements: 5 (44) |
Complexity: 9 |
Complexity Density: 0.3 |
|
74 |
32 |
public void selectByValues(List<String> values)... |
75 |
|
{ |
76 |
32 |
Map<String, String> valueTitles = new HashMap(); |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
32 |
List<String> options = new ArrayList<>(); |
82 |
32 |
for (WebElement element : this.hiddenSelect.findElements(By.tagName("option"))) { |
83 |
3060 |
String value = element.getAttribute("value"); |
84 |
3060 |
options.add(value); |
85 |
3060 |
valueTitles.put(value, element.getText()); |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
32 |
openMenu(); |
90 |
|
|
91 |
32 |
if (isMultiple()) { |
92 |
|
|
93 |
5 |
for (WebElement element : this.menu.findElements(By.tagName("li"))) { |
94 |
|
|
95 |
795 |
int index = Integer.parseInt(element.getAttribute("data-original-index")); |
96 |
795 |
String value = options.get(index); |
97 |
|
|
98 |
|
|
99 |
795 |
if (isElementSelected(element) != values.contains(value)) { |
100 |
6 |
element.findElement(By.tagName("a")).click(); |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
6 |
if (!this.menu.isDisplayed()) { |
106 |
|
|
107 |
0 |
openMenu(); |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
0 |
element.findElement(By.tagName("a")).click(); |
112 |
|
} |
113 |
|
} |
114 |
|
} |
115 |
|
} else { |
116 |
27 |
WebElement filterInput = getFilterInput(); |
117 |
27 |
if (filterInput != null) { |
118 |
14 |
filterInput.sendKeys(valueTitles.get(values.get(0))); |
119 |
|
} |
120 |
|
|
121 |
27 |
for (WebElement element : this.menu.findElements(By.tagName("li"))) { |
122 |
|
|
123 |
641 |
int index = Integer.parseInt(element.getAttribute("data-original-index")); |
124 |
641 |
String value = options.get(index); |
125 |
|
|
126 |
|
|
127 |
641 |
if (element.isDisplayed() && values.contains(value)) { |
128 |
27 |
element.findElement(By.tagName("a")).click(); |
129 |
27 |
waitUntilMenuIsClosed(); |
130 |
27 |
if (!button.getAttribute("title").startsWith(valueTitles.get(value))) { |
131 |
0 |
throw new RuntimeException( |
132 |
|
String.format("Failed to set the value [%s] with the title [%s]. Got [%s] instead.", |
133 |
|
value, valueTitles.get(value), button.getAttribute("title"))); |
134 |
|
} |
135 |
27 |
break; |
136 |
|
} |
137 |
|
} |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
32 |
if (this.menu.isDisplayed()) { |
142 |
5 |
closeMenu(); |
143 |
|
} |
144 |
|
} |
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
146 |
27 |
private WebElement getFilterInput()... |
147 |
|
{ |
148 |
27 |
try { |
149 |
27 |
return this.driver.findElementWithoutWaiting(this.menu, By.tagName("input")); |
150 |
|
} catch (NoSuchElementException e) { |
151 |
13 |
return null; |
152 |
|
} |
153 |
|
} |
154 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
155 |
32 |
private void waitUntilMenuIsOpened()... |
156 |
|
{ |
157 |
|
|
158 |
32 |
driver.waitUntilCondition(new ExpectedCondition<Object>() |
159 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
160 |
32 |
@Override... |
161 |
|
public Object apply(WebDriver webDriver) |
162 |
|
{ |
163 |
32 |
return menu.isDisplayed(); |
164 |
|
} |
165 |
|
}); |
166 |
|
} |
167 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
168 |
32 |
private void waitUntilMenuIsClosed()... |
169 |
|
{ |
170 |
32 |
driver.waitUntilCondition(new ExpectedCondition<Object>() |
171 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
172 |
32 |
@Override... |
173 |
|
public Object apply(WebDriver webDriver) |
174 |
|
{ |
175 |
32 |
return !menu.isDisplayed(); |
176 |
|
} |
177 |
|
}); |
178 |
|
} |
179 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
180 |
32 |
private void openMenu()... |
181 |
|
{ |
182 |
32 |
this.button.click(); |
183 |
32 |
waitUntilMenuIsOpened(); |
184 |
|
} |
185 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
186 |
5 |
private void closeMenu()... |
187 |
|
{ |
188 |
5 |
this.button.click(); |
189 |
5 |
waitUntilMenuIsClosed(); |
190 |
|
} |
191 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
192 |
795 |
private boolean isElementSelected(WebElement element)... |
193 |
|
{ |
194 |
795 |
return element.getAttribute("class").contains("selected"); |
195 |
|
} |
196 |
|
} |