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
|
/*
* 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.tabsheet;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import com.vaadin.testbench.TestBenchElement;
import com.vaadin.tests.tb3.MultiBrowserTest;
/**
* Add TB3 test as the TB2 one failed on keyboard events.
*
* @since
* @author Vaadin Ltd
*/
public class TabKeyboardNavigationTest extends MultiBrowserTest {
@Test
public void testFocus() throws InterruptedException, IOException {
openTestURL();
click(1);
sendKeys(1, Keys.ARROW_RIGHT);
assertSheet(1);
sendKeys(2, Keys.SPACE);
assertSheet(2);
compareScreen("tab2");
sendKeys(2, Keys.ARROW_RIGHT);
sendKeys(3, Keys.ARROW_RIGHT);
assertSheet(2);
sendKeys(5, Keys.SPACE);
assertSheet(5);
compareScreen("skip-disabled-to-tab5");
TestBenchElement addTabButton = (TestBenchElement) getDriver()
.findElements(By.className("v-button")).get(0);
click(addTabButton);
click(5);
sendKeys(5, Keys.ARROW_RIGHT);
assertSheet(5);
sendKeys(6, Keys.SPACE);
assertSheet(6);
click(addTabButton);
click(addTabButton);
click(addTabButton);
click(addTabButton);
click(addTabButton);
click(addTabButton);
click(8);
compareScreen("click-tab-8");
sendKeys(8, Keys.ARROW_RIGHT);
sendKeys(9, Keys.SPACE);
click(9);
compareScreen("tab-9");
sendKeys(9, Keys.ARROW_RIGHT);
Thread.sleep(DELAY);
sendKeys(10, Keys.ARROW_RIGHT);
// Here PhantomJS used to fail. Or when accessing tab2. The fix was to
// call the elem.click(x, y) using the (x, y) position instead of the
// elem.click() without any arguments.
sendKeys(11, Keys.ARROW_RIGHT);
assertSheet(9);
sendKeys(12, Keys.SPACE);
assertSheet(12);
compareScreen("scrolled-right-to-tab-12");
click(5);
sendKeys(5, Keys.ARROW_LEFT);
// Here IE8 used to fail. A hidden <div> in IE8 would have the bounds of
// it's parent, and when trying to see in which direction to scroll
// (left or right) to make the key selected tab visible, the
// VTabSheet.scrollIntoView(Tab) used to check first whether the tab
// isClipped. On IE8 this will always return true for both hidden tabs
// on the left and clipped tabs on the right. So instead of going to
// left, it'll search all the way to the right.
sendKeys(3, Keys.ARROW_LEFT);
sendKeys(2, Keys.ARROW_LEFT);
assertSheet(5);
sendKeys(1, Keys.SPACE);
assertSheet(1);
compareScreen("scrolled-left-to-tab-1");
}
/*
* Press key on the element.
*/
private void sendKeys(int tabIndex, Keys key) throws InterruptedException {
sendKeys(tab(tabIndex), key);
}
/*
* Press key on the element.
*/
private void sendKeys(TestBenchElement element, Keys key)
throws InterruptedException {
element.sendKeys(key);
if (DELAY > 0) {
sleep(DELAY);
}
}
/*
* Click on the element.
*/
private void click(int tabIndex) throws InterruptedException {
click(tab(tabIndex));
}
/*
* Click on the element.
*/
private void click(TestBenchElement element) throws InterruptedException {
element.click(10, 10);
if (DELAY > 0) {
sleep(DELAY);
}
}
/*
* Delay for PhantomJS.
*/
private final static int DELAY = 10;
private void assertSheet(int index) {
String labelCaption = "Tab " + index;
By id = By.id(TabKeyboardNavigation.labelID(index));
WebElement labelElement = getDriver().findElement(id);
waitForElementPresent(id);
Assert.assertEquals(labelCaption, labelCaption, labelElement.getText());
}
/*
* Provide the tab at specified index.
*/
private TestBenchElement tab(int index) {
By by = By.className("v-tabsheet-tabitemcell");
TestBenchElement element = (TestBenchElement) getDriver().findElements(
by).get(index - 1);
String expected = "Tab " + index;
Assert.assertEquals(expected,
element.getText().substring(0, expected.length()));
return element;
}
}
|