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
|
/*
* Copyright 2000-2014 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.tests.components.textfield;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import com.vaadin.testbench.elements.TextFieldElement;
import com.vaadin.tests.tb3.MultiBrowserTest;
public class AlternatingTextFieldsTest extends MultiBrowserTest {
private String RANDOM_INPUT = "Some input here";
private List<TextFieldElement> textfields;
@Test
public void testInputPrompt() {
openTestURL();
/*
* Starting positions
*/
createTextFields();
// test that both input prompts exist in the beginning
ensureTextFieldHasInputPrompt(0);
ensureTextFieldHasInputPrompt(1);
/*
* Write on and empty the first TextField
*/
// select first input prompt
ensureSelectionClearsPrompt(0);
// write on the first TextField
ensureWritingDisablesOther(0);
// empty the text on the first TextField
ensureEmptyingAddsPromptAndEnablesOther(0);
/*
* Write on and empty the second TextField
*/
// now select the second input prompt
ensureSelectionClearsPrompt(1);
// write on the second TextField
ensureWritingDisablesOther(1);
// empty the text on the second TextField
ensureEmptyingAddsPromptAndEnablesOther(1);
}
private void ensureEmptyingAddsPromptAndEnablesOther(int index) {
// remove the text from the TextField
emptyTextField(index);
// ensure that the TextField really is empty
ensureTextFieldEmpty(index);
// ensure that the other TextField has again been enabled and has an
// input prompt
if (index == 0) {
ensureTextFieldIsEnabledAndHasInputPrompt(1);
} else {
ensureTextFieldIsEnabledAndHasInputPrompt(0);
}
}
private void ensureWritingDisablesOther(int index) {
// write some text to the TextField
writeOnTextField(index);
// ensure that the other TextField is disabled and has no input prompt
if (index == 0) {
ensureTextFieldDisabledAndEmpty(1);
} else {
ensureTextFieldDisabledAndEmpty(0);
}
}
private void ensureSelectionClearsPrompt(int index) {
// select the TextField
textfields.get(index).click();
// check that the the prompt was removed
ensureTextFieldEmpty(index);
}
/**
* Check that the TextField has no input prompt
*
* @since
* @param index
* The TextField to be inspected
*/
private void ensureTextFieldEmpty(int index) {
assertEquals("TextField " + index + " was not empty,", "", textfields
.get(index).getValue());
}
/**
* Check that the TextField has been enabled and has correct input prompt
*
* @since
* @param index
* the TextField to be inspected
*/
private void ensureTextFieldIsEnabledAndHasInputPrompt(final int index) {
waitUntil(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver input) {
return textfields.get(index).isEnabled();
}
@Override
public String toString() {
// Timed out after 10 seconds waiting for ...
return "TextField " + index + " to be enabled";
}
});
ensureTextFieldHasInputPrompt(index);
}
/**
* Check that the TextField has the correct input prompt
*
* @since
* @param index
* The TextField to be inspected
*/
private void ensureTextFieldHasInputPrompt(final int index) {
if (index == 0) {
assertEquals("Incorrect or missing prompt,",
AlternatingTextFields.FIRST_TEXTFIELD_INPUT_PROMPT,
textfields.get(index).getValue());
} else {
assertEquals("Incorrect or missing prompt,",
AlternatingTextFields.SECOND_TEXTFIELD_INPUT_PROMPT,
textfields.get(index).getValue());
}
}
/**
* Check that the TextField has been disabled and has no input prompt
*
* @since
* @param index
* The TextField to be inspected
*/
private void ensureTextFieldDisabledAndEmpty(final int index) {
waitUntil(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver input) {
return !textfields.get(index).isEnabled();
}
@Override
public String toString() {
// Timed out after 10 seconds waiting for ...
return "TextField " + index + " to be disabled";
}
});
ensureTextFieldEmpty(index);
}
private void createTextFields() {
textfields = $(TextFieldElement.class).all();
}
private void writeOnTextField(int index) {
textfields.get(index).sendKeys(RANDOM_INPUT);
}
private void emptyTextField(int index) {
for (int i = 0; i < 15; i++) {
textfields.get(index).sendKeys(Keys.BACK_SPACE);
}
}
}
|