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
|
package com.vaadin.tests.components.datefield;
import static com.vaadin.tests.components.datefield.DateFieldChangeResolution.BUTTON_BASE_ID;
import static com.vaadin.tests.components.datefield.DateFieldChangeResolution.DATEFIELD_ID;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import com.vaadin.shared.ui.datefield.DateResolution;
import com.vaadin.testbench.By;
import com.vaadin.tests.tb3.MultiBrowserTest;
public class DateFieldChangeResolutionTest extends MultiBrowserTest {
private WebElement dateFieldButton, textField;
private WebElement resolutionDay, resolutionMonth, resolutionYear;
@Test
public void changeResolutionBetweenYearAndMonth() throws Exception {
initialize();
click(resolutionMonth);
checkHeaderAndBody(DateResolution.MONTH, true);
click(resolutionYear);
checkHeaderAndBody(DateResolution.YEAR, true);
}
@Test
public void changeResolutionToDayThenMonth() throws Exception {
initialize();
checkHeaderAndBody(DateResolution.YEAR, true); // check the initial
// state
click(resolutionDay);
checkHeaderAndBody(DateResolution.DAY, true);
click(resolutionMonth);
checkHeaderAndBody(DateResolution.MONTH, true);
}
@Test
public void setDateAndChangeResolution() throws Exception {
initialize();
// Set the date to previous month.
click(resolutionMonth);
openPopupDateField();
click(driver.findElement(By.className("v-button-prevmonth")));
closePopupDateField();
assertFalse(
"The text field of the calendar should not be empty after selecting a date",
textField.getAttribute("value").isEmpty());
// Change resolutions and check that the selected date is not lost and
// that the calendar has the correct resolution.
click(resolutionYear);
checkHeaderAndBody(DateResolution.YEAR, false);
}
private void initialize() {
openTestURL();
WebElement dateField = driver.findElement(By.id(DATEFIELD_ID));
dateFieldButton = dateField
.findElement(By.className("v-datefield-button"));
textField = dateField
.findElement(By.className("v-datefield-textfield"));
resolutionDay = driver.findElement(By.id(BUTTON_BASE_ID + "day"));
resolutionMonth = driver.findElement(By.id(BUTTON_BASE_ID + "month"));
resolutionYear = driver.findElement(By.id(BUTTON_BASE_ID + "year"));
}
private void checkHeaderAndBody(DateResolution resolution,
boolean textFieldIsEmpty) throws Exception {
// Popup date field has all kinds of strange timers on the
// client side
sleep(100);
// Open the popup calendar, perform checks and close the popup.
openPopupDateField();
if (resolution.compareTo(DateResolution.MONTH) <= 0) {
checkMonthHeader();
} else {
checkYearHeader();
}
if (resolution.compareTo(DateResolution.DAY) <= 0) {
assertTrue(
"A calendar with the chosen resolution should have a body",
calendarHasBody());
} else {
assertFalse(
"A calendar with the chosen resolution should not have a body",
calendarHasBody());
}
if (textFieldIsEmpty) {
assertTrue("The text field of the calendar should be empty",
textField.getAttribute("value").isEmpty());
} else {
assertFalse("The text field of the calendar should not be empty",
textField.getAttribute("value").isEmpty());
}
closePopupDateField();
}
private void checkMonthHeader() {
checkHeaderForYear();
checkHeaderForMonth(true);
}
private void checkYearHeader() {
checkHeaderForYear();
checkHeaderForMonth(false);
}
private boolean calendarHasBody() {
return isElementPresent(By.className("v-datefield-calendarpanel-body"));
}
private void checkHeaderForMonth(boolean buttonsExpected) {
// If buttonsExpected is true, check that there are buttons for changing
// the month. Otherwise check that there are no such buttons.
if (buttonsExpected) {
assertTrue(
"The calendar should have a button for switching to the previous month",
isElementPresent(By.cssSelector(
".v-datefield-calendarpanel-header .v-datefield-calendarpanel-prevmonth .v-button-prevmonth")));
assertTrue(
"The calendar should have a button for switching to the next month",
isElementPresent(By.cssSelector(
".v-datefield-calendarpanel-header .v-datefield-calendarpanel-nextmonth .v-button-nextmonth")));
} else {
assertFalse(
"The calendar should not have a button for switching to the previous month",
isElementPresent(By.cssSelector(
".v-datefield-calendarpanel-header .v-datefield-calendarpanel-prevmonth .v-button-prevmonth")));
assertFalse(
"The calendar should not have a button for switching to the next month",
isElementPresent(By.cssSelector(
".v-datefield-calendarpanel-header .v-datefield-calendarpanel-nextmonth .v-button-nextmonth")));
}
}
private void checkHeaderForYear() {
assertTrue(
"The calendar should have a button for switching to the previous year",
isElementPresent(By.cssSelector(
".v-datefield-calendarpanel-header .v-datefield-calendarpanel-prevyear .v-button-prevyear")));
assertTrue("The calendar header should show the selected year",
isElementPresent(By.cssSelector(
".v-datefield-calendarpanel-header .v-datefield-calendarpanel-month")));
assertTrue(
"The calendar should have a button for switching to the next year",
isElementPresent(By.cssSelector(
".v-datefield-calendarpanel-header .v-datefield-calendarpanel-nextyear .v-button-nextyear")));
}
private void click(WebElement element) {
testBenchElement(element).click(5, 5);
}
private void openPopupDateField() {
click(dateFieldButton);
}
private void closePopupDateField() {
WebElement element = driver
.findElement(By.cssSelector(".v-datefield-calendarpanel"));
element.sendKeys(Keys.ESCAPE);
}
}
|