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
277
278
279
280
281
282
283
284
285
286
287
|
package com.vaadin.tests.components.combobox;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import com.vaadin.testbench.By;
import com.vaadin.testbench.elements.ComboBoxElement;
import com.vaadin.testbench.elements.LabelElement;
import com.vaadin.testbench.parallel.BrowserUtil;
import com.vaadin.tests.tb3.MultiBrowserTest;
public class ComboBoxSelectingWithNewItemsAllowedTest extends MultiBrowserTest {
private ComboBoxElement comboBoxElement;
private LabelElement labelElement;
@Override
public void setup() throws Exception {
super.setup();
openTestURL();
waitForElementPresent(By.className("v-filterselect"));
comboBoxElement = $(ComboBoxElement.class).first();
labelElement = $(LabelElement.class).id("count");
}
@Test
public void checkDefaults() {
assertInitialItemCount();
}
@Test
public void itemIsAddedWithEnter() {
typeInputAndHitEnter("a");
assertOneMoreThanInitial();
assertThatSelectedValueIs("a");
}
@Test
public void itemIsAddedWithTab() {
typeInputAndHitTab("a");
assertOneMoreThanInitial();
assertThatSelectedValueIs("a");
}
@Test
public void itemIsAddedWithClickOut() {
typeInputAndClickOut("a");
assertOneMoreThanInitial();
assertThatSelectedValueIs("a");
}
@Test
public void matchingSuggestionIsSelectedWithEnter() {
typeInputAndHitEnter("a0");
assertInitialItemCount();
assertThatSelectedValueIs("a0");
}
@Test
public void matchingSuggestionIsSelectedWithTab() {
typeInputAndHitTab("a0");
assertInitialItemCount();
assertThatSelectedValueIs("a0");
}
@Test
public void nullIsSelected() {
typeInputAndHitEnter("a");
assertOneMoreThanInitial();
assertThatSelectedValueIs("a");
clearInputAndHitEnter();
assertOneMoreThanInitial();
assertThatSelectedValueIs("", "null");
}
@Test
public void itemFromSecondPageIsSelected() {
typeInputAndHitEnter("a20");
assertInitialItemCount();
assertThatSelectedValueIs("a20");
}
@Test
public void selectingNullFromSecondPage() {
typeInputAndHitEnter("a20");
assertInitialItemCount();
assertThatSelectedValueIs("a20");
clearInputAndHitEnter();
assertInitialItemCount();
assertThatSelectedValueIs("", "null");
}
@Test
public void selectionRemainsAfterOpeningPopup() {
typeInputAndHitEnter("a20");
assertInitialItemCount();
assertThatSelectedValueIs("a20");
openPopup();
assertThatSelectedValueIs("a20");
}
@Test
public void selectionOnMouseOut() {
typeInputAndHitEnter("a20");
comboBoxElement.sendKeys(Keys.ARROW_DOWN, Keys.ARROW_DOWN);
findElement(By.className("v-app")).click();
assertInitialItemCount();
assertThatSelectedValueIs("", "null");
}
@Test
public void cancelResetsSelection() {
sendKeysToInput("a20");
cancelSelection();
assertInitialItemCount();
assertThatSelectedValueIs("");
}
@Test
public void inputFieldResetsToSelectedText() {
typeInputAndHitEnter("z5");
sendKeysToInput(Keys.BACK_SPACE, Keys.BACK_SPACE);
cancelSelection();
assertInitialItemCount();
assertThatSelectedValueIs("z5");
}
@Test
public void emptyValueIsSelectedWithTab() {
typeInputAndHitEnter("z5");
assertInitialItemCount();
assertThatSelectedValueIs("z5");
// longer delay for this one because otherwise it keeps failing when run
// on local machine
comboBoxElement.sendKeys(200, Keys.BACK_SPACE, Keys.BACK_SPACE,
Keys.TAB);
assertInitialItemCount();
assertThatSelectedValueIs("", "null");
sendKeysToInput("z5");
cancelSelection();
assertInitialItemCount();
assertThatSelectedValueIs("", "null");
}
@Test
public void arrowNavigatedValueIsSelectedWithEnter() {
sendKeysToInput("z");
sendKeysToInput(Keys.DOWN, Keys.DOWN, getReturn());
assertInitialItemCount();
assertThatSelectedValueIs("z1");
}
@Test
public void arrowNavigatedValueIsSelectedWithTab() {
sendKeysToInput("z");
sendKeysToInput(Keys.DOWN, Keys.DOWN, Keys.TAB);
assertInitialItemCount();
assertThatSelectedValueIs("z1");
}
private void clearInputAndHitEnter() {
sendKeysToInput(Keys.BACK_SPACE, Keys.BACK_SPACE, Keys.BACK_SPACE);
sendKeysToInput(getReturn());
}
private void typeInputAndHitEnter(String input) {
clearInputAndType(input);
sendKeysToInput(getReturn());
}
private void typeInputAndHitTab(String input) {
clearInputAndType(input);
sendKeysToInput(Keys.TAB);
}
private void typeInputAndClickOut(String input) {
clearInputAndType(input);
new Actions(getDriver()).moveToElement(comboBoxElement, 10, 10)
.moveByOffset(comboBoxElement.getSize().getWidth(), 0).click()
.perform();
}
private void clearInputAndType(String input) {
comboBoxElement.clear();
sendKeysToInput(input);
}
private void sendKeysToInput(CharSequence... keys) {
comboBoxElement.sendKeys(keys);
}
private Keys getReturn() {
if (BrowserUtil.isPhantomJS(getDesiredCapabilities())) {
return Keys.ENTER;
} else {
return Keys.RETURN;
}
}
private void openPopup() {
// Need to wait to make sure popup is closed first.
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
comboBoxElement.openPopup();
}
private void cancelSelection() {
sendKeysToInput(Keys.ESCAPE);
}
private void assertThatSelectedValueIs(final String value) {
assertThatSelectedValueIs(value, value);
}
private void assertThatSelectedValueIs(final String value,
final String labelValue) {
assertThat(comboBoxElement.getText(), is(value));
waitUntil(new ExpectedCondition<Boolean>() {
private String actualValue;
@Override
public Boolean apply(WebDriver input) {
actualValue = $(LabelElement.class).id("value").getText();
return actualValue.equals(labelValue);
}
@Override
public String toString() {
// Timed out after 10 seconds waiting for ...
return String.format("label value to match '%s' (was: '%s')",
labelValue, actualValue);
}
});
}
private void assertInitialItemCount() {
// wait for a bit in case the count is updating
sleep(1000);
assertThat("Wrong initial item count.", labelElement.getText(),
is("2600"));
}
private void assertOneMoreThanInitial() {
waitUntil(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver input) {
return "2601".equals(labelElement.getText());
}
@Override
public String toString() {
// Timed out after 10 seconds waiting for ...
return String.format("item count to become 2601 (was: %s)",
labelElement.getText());
}
});
}
}
|