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
|
/*
* 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.fieldgroup;
import org.junit.Assert;
import com.vaadin.testbench.TestBenchElement;
import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.CheckBoxElement;
import com.vaadin.testbench.elements.TableElement;
import com.vaadin.testbench.elements.TableRowElement;
import com.vaadin.testbench.elements.TextAreaElement;
import com.vaadin.testbench.elements.TextFieldElement;
import com.vaadin.tests.data.bean.Sex;
import com.vaadin.tests.tb3.MultiBrowserTest;
import com.vaadin.tests.tb3.newelements.FixedNotificationElement;
public abstract class BasicPersonFormTest extends MultiBrowserTest {
private static final String BEAN_VALUES = "Person [firstName=John, lastName=Doe, email=john@doe.com, age=64, sex=Male, address=Address [streetAddress=John street, postalCode=11223, city=John's town, country=USA], deceased=false, salary=null, salaryDouble=null, rent=null]";
private int logCounter;
@Override
public void setup() throws Exception {
super.setup();
logCounter = 0;
}
@Override
protected Class<?> getUIClass() {
return BasicPersonForm.class;
}
protected TextFieldElement getFirstNameField() {
return $(TextFieldElement.class).caption("First Name").first();
}
protected TextAreaElement getLastNameArea() {
return $(TextAreaElement.class).caption("Last Name").first();
}
protected TextFieldElement getEmailField() {
return $(TextFieldElement.class).caption("Email").first();
}
protected TextFieldElement getAgeField() {
return $(TextFieldElement.class).caption("Age").first();
}
protected TableElement getGenderTable() {
return $(TableElement.class).caption("Sex").first();
}
protected TextFieldElement getDeceasedField() {
return $(TextFieldElement.class).caption("Deceased").first();
}
protected void showBeanValues() {
$(ButtonElement.class).caption("Show bean values").first().click();
}
protected CheckBoxElement getPreCommitFailsCheckBox() {
return $(CheckBoxElement.class).get(1);
}
protected void commitChanges() {
$(ButtonElement.class).caption("Commit").first().click();
}
protected void closeNotification() {
$(FixedNotificationElement.class).first().close();
}
protected CheckBoxElement getPostCommitFailsCheckBox() {
return $(CheckBoxElement.class).get(0);
}
protected void discardChanges() {
$(ButtonElement.class).caption("Discard").first().click();
}
protected void assertFirstNameValue(String expected) {
assertFieldValue("First Name", expected, getFirstNameField());
}
protected void assertLastNameValue(String expected) {
assertFieldValue("Last Name", expected, getLastNameArea());
}
protected void assertEmailValue(String expected) {
assertFieldValue("Email", expected, getEmailField());
}
protected void assertAgeValue(String expected) {
assertFieldValue("Age", expected, getAgeField());
}
protected void assertDeceasedValue(String expected) {
assertFieldValue("Deceased", expected, getDeceasedField());
}
private void assertFieldValue(String caption, String expected,
TestBenchElement field) {
Assert.assertEquals(
String.format("Unexpected value for field '%s',", caption),
expected, field.getAttribute("value"));
}
protected void assertSelectedSex(Sex sex) {
TableRowElement row = getGenderTable().getRow(getIndex(sex));
Assert.assertTrue(
String.format("Given sex (%s) isn't selected.",
sex.getStringRepresentation()),
hasCssClass(row, "v-selected"));
}
private int getIndex(Sex sex) {
switch (sex) {
case MALE:
return 0;
case FEMALE:
return 1;
default:
return 2;
}
}
protected void assertBeanValuesUnchanged() {
showBeanValues();
assertLogText(BEAN_VALUES);
}
protected void assertCommitFails() {
commitChanges();
closeNotification();
assertLogText("Commit failed: Commit failed");
}
protected void assertCommitSuccessful() {
commitChanges();
closeNotification();
assertLogText("Commit succesful");
}
protected void assertDiscardResetsFields() {
discardChanges();
assertLogText("Discarded changes");
assertDefaults();
}
protected void assertLogText(String expected) {
++logCounter;
Assert.assertEquals("Unexpected log contents,", logCounter + ". "
+ expected, getLogRow(0));
}
protected void assertDefaults() {
assertFirstNameValue("John");
assertLastNameValue("Doe");
assertEmailValue("john@doe.com");
assertAgeValue("64");
assertSelectedSex(Sex.MALE);
assertDeceasedValue("NAAAAAH");
}
}
|