1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/*
* Copyright 2000-2021 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.testbench.elements;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import com.vaadin.testbench.By;
import com.vaadin.testbench.TestBenchElement;
import com.vaadin.testbench.elementsbase.ServerClass;
@ServerClass("com.vaadin.ui.ComboBox")
public class ComboBoxElement extends AbstractSingleSelectElement {
private static org.openqa.selenium.By bySuggestionPopup = By
.vaadin("#popup");
private static org.openqa.selenium.By byNextPage = By
.className("v-filterselect-nextpage");
private static org.openqa.selenium.By byPrevPage = By
.className("v-filterselect-prevpage");
/**
* Selects the first option in the ComboBox which matches the given text.
*
* @param text
* the text of the option to select
*/
public void selectByText(String text) {
if (isReadOnly()) {
throw new ReadOnlyException();
}
if (!isTextInputAllowed()) {
selectByTextFromPopup(text);
return;
}
clear();
sendKeys(text);
selectSuggestion(text);
}
/**
* Selects, without filtering, the first option in the ComboBox which
* matches the given text.
*
* @param text
* the text of the option to select
*/
private void selectByTextFromPopup(String text) {
// This method assumes there is no need to touch the filter string
// 1. Find first page
// 2. Select first matching text if found
// 3. Iterate towards end
while (openPrevPage()) {
// Scroll until beginning
}
do {
if (selectSuggestion(text)) {
return;
}
} while (openNextPage());
}
private boolean selectSuggestion(String text) {
for (WebElement suggestion : getPopupSuggestionElements()) {
if (text.equals(suggestion.getText())) {
clickElement(suggestion);
return true;
}
}
return false;
}
private boolean isReadOnly(WebElement elem) {
JavascriptExecutor js = (JavascriptExecutor) getDriver();
return (Boolean) js.executeScript("return arguments[0].readOnly", elem);
}
/**
* Checks if text input is allowed for the combo box.
*
* @return <code>true</code> if text input is allowed, <code>false</code>
* otherwise
*/
public boolean isTextInputAllowed() {
return !isReadOnly(getInputField());
}
/**
* Checks whether the suggestion popup is open or not.
*
* @return {@code true} if popup is open, {@code false if not}
*/
public boolean isPopupOpen() {
return isElementPresent(bySuggestionPopup);
}
/**
* Open the suggestion popup.
*/
public void openPopup() {
findElement(By.vaadin("#button")).click();
}
/**
* Gets the text representation of all suggestions on the current page.
*
* @return List of suggestion texts
*/
public List<String> getPopupSuggestions() {
List<String> suggestionsTexts = new ArrayList<>();
List<WebElement> suggestions = getPopupSuggestionElements();
for (WebElement suggestion : suggestions) {
String text = suggestion.getText();
if (!text.isEmpty()) {
suggestionsTexts.add(text);
}
}
return suggestionsTexts;
}
/**
* Gets the elements of all suggestions on the current page.
* <p>
* Opens the popup if not already open.
*
* @return a list of elements for the suggestions on the current page
*/
public List<WebElement> getPopupSuggestionElements() {
List<WebElement> tables = getSuggestionPopup()
.findElements(By.tagName("table"));
if (tables == null || tables.isEmpty()) {
return Collections.emptyList();
}
WebElement table = tables.get(0);
return table.findElements(By.tagName("td"));
}
/**
* Opens next popup page.
*
* @return True if next page opened. false if doesn't have next page
*/
public boolean openNextPage() {
try {
clickElement(getSuggestionPopup().findElement(byNextPage));
return true;
} catch (WebDriverException e) {
// PhantomJS driver can throw WDE instead of the more specific
// NoSuchElementException
return false;
}
}
/**
* Open previous popup page.
*
* @return True if previous page opened. False if doesn't have previous page
*/
public boolean openPrevPage() {
try {
clickElement(getSuggestionPopup().findElement(byPrevPage));
return true;
} catch (WebDriverException e) {
// PhantomJS driver can throw WDE instead of the more specific
// NoSuchElementException
return false;
}
}
/**
* Returns the suggestion popup element.
*/
public WebElement getSuggestionPopup() {
ensurePopupOpen();
return findElement(bySuggestionPopup);
}
/**
* Return value of the combo box element.
*
* @return value of the combo box element
*/
public String getValue() {
return getInputField().getAttribute("value");
}
/**
* Returns the text input field element, used for entering text into the
* combo box.
*
* @return the input field element
*/
public WebElement getInputField() {
return findElement(By.vaadin("#textbox"));
}
private void ensurePopupOpen() {
if (!isElementPresent(bySuggestionPopup)) {
openPopup();
}
}
@Override
public String getText() {
return getInputField().getAttribute("value");
}
@Override
public void clear() {
getInputField().clear();
String value = getText();
if (value != null && !value.isEmpty()) {
((JavascriptExecutor) getDriver())
.executeScript("arguments[0].value = ''", getInputField());
}
}
@Override
public void sendKeys(CharSequence... keysToSend) {
sendKeys(50, keysToSend);
}
/**
* Use this method to simulate typing into an element, which may set its
* value.
*
* @param delay
* delay after sending each individual key (mainly needed for
* PhantomJS)
* @param keysToSend
* keys to type into the element
*/
public void sendKeys(int delay, CharSequence... keysToSend) {
WebElement input = getInputField();
for (CharSequence key : keysToSend) {
input.sendKeys(key);
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
}
}
}
private void clickElement(WebElement element) {
if (isFirefox()) {
// Workaround for Selenium/TB and Firefox 45 issue
((TestBenchElement) element).clickHiddenElement();
} else {
element.click();
}
}
}
|