2 * Copyright 2000-2016 Vaadin Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
16 package com.vaadin.tests.components.grid.basicfeatures;
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertTrue;
20 import static org.junit.Assert.fail;
22 import java.util.List;
24 import org.openqa.selenium.By;
25 import org.openqa.selenium.JavascriptExecutor;
26 import org.openqa.selenium.WebElement;
28 import com.vaadin.testbench.TestBenchElement;
29 import com.vaadin.testbench.parallel.TestCategory;
30 import com.vaadin.tests.tb3.MultiBrowserTest;
33 public abstract class EscalatorBasicClientFeaturesTest
34 extends MultiBrowserTest {
36 private static final String LOGICAL_ROW_ATTRIBUTE_NAME = "vLogicalRow";
37 private static final String SPACER_CSS_CLASS = "v-escalator-spacer";
39 protected static final String COLUMNS_AND_ROWS = "Columns and Rows";
41 protected static final String COLUMNS = "Columns";
42 protected static final String ADD_ONE_COLUMN_TO_BEGINNING = "Add one column to beginning";
43 protected static final String ADD_ONE_ROW_TO_BEGINNING = "Add one row to beginning";
44 protected static final String ADD_ONE_ROW_TO_END = "Add one row to end";
45 protected static final String REMOVE_ONE_COLUMN_FROM_BEGINNING = "Remove one column from beginning";
46 protected static final String REMOVE_ONE_ROW_FROM_BEGINNING = "Remove one row from beginning";
47 protected static final String REMOVE_ALL_ROWS = "Remove all rows";
48 protected static final String REMOVE_50_ROWS_FROM_BOTTOM = "Remove 50 rows from bottom";
49 protected static final String REMOVE_50_ROWS_FROM_ALMOST_BOTTOM = "Remove 50 rows from almost bottom";
50 protected static final String ADD_ONE_OF_EACH_ROW = "Add one of each row";
51 protected static final String RESIZE_FIRST_COLUMN_TO_MAX_WIDTH = "Resize first column to max width";
52 protected static final String RESIZE_FIRST_COLUMN_TO_100PX = "Resize first column to 100 px";
54 protected static final String HEADER_ROWS = "Header Rows";
55 protected static final String BODY_ROWS = "Body Rows";
56 protected static final String FOOTER_ROWS = "Footer Rows";
58 protected static final String SCROLL_TO = "Scroll to...";
60 protected static final String REMOVE_ALL_INSERT_SCROLL = "Remove all, insert 30 and scroll 40px";
62 protected static final String GENERAL = "General";
63 protected static final String DETACH_ESCALATOR = "Detach Escalator";
64 protected static final String ATTACH_ESCALATOR = "Attach Escalator";
65 protected static final String POPULATE_COLUMN_ROW = "Populate Escalator (columns, then rows)";
66 protected static final String POPULATE_ROW_COLUMN = "Populate Escalator (rows, then columns)";
67 protected static final String CLEAR_COLUMN_ROW = "Clear (columns, then rows)";
68 protected static final String CLEAR_ROW_COLUMN = "Clear (rows, then columns)";
70 protected static final String FEATURES = "Features";
71 protected static final String FROZEN_COLUMNS = "Frozen columns";
72 protected static final String FREEZE_1_COLUMN = "Freeze 1 column";
73 protected static final String FREEZE_0_COLUMNS = "Freeze 0 columns";
74 protected static final String COLUMN_SPANNING = "Column spanning";
75 protected static final String COLSPAN_NORMAL = "Apply normal colspan";
76 protected static final String COLSPAN_NONE = "Apply no colspan";
77 protected static final String SET_100PX = "Set 100px";
78 protected static final String SPACERS = "Spacers";
79 protected static final String FOCUSABLE_UPDATER = "Focusable Updater";
80 protected static final String SCROLL_HERE_ANY_0PADDING = "Scroll here (ANY, 0)";
81 protected static final String SCROLL_HERE_SPACERBELOW_ANY_0PADDING = "Scroll here row+spacer below (ANY, 0)";
82 protected static final String REMOVE = "Remove";
84 protected static final String ROW_MINUS1 = "Row -1";
85 protected static final String ROW_0 = "Row 0";
86 protected static final String ROW_1 = "Row 1";
87 protected static final String ROW_25 = "Row 25";
88 protected static final String ROW_50 = "Row 50";
89 protected static final String ROW_75 = "Row 75";
90 protected static final String ROW_99 = "Row 99";
93 public void setup() throws Exception {
100 protected Class<?> getUIClass() {
101 return EscalatorBasicClientFeatures.class;
104 protected TestBenchElement getEscalator() {
105 By className = By.className("v-escalator");
106 if (isElementPresent(className)) {
107 return (TestBenchElement) findElement(className);
114 * the index of the row element in the section. If negative, the
115 * calculation starts from the end (-1 is the last, -2 is the
116 * second-to-last etc)
118 protected TestBenchElement getHeaderRow(int row) {
119 return getRow("thead", row);
124 * the index of the row element in the section. If negative, the
125 * calculation starts from the end (-1 is the last, -2 is the
126 * second-to-last etc)
128 protected TestBenchElement getBodyRow(int row) {
129 return getRow("tbody", row);
134 * the index of the row element in the section. If negative, the
135 * calculation starts from the end (-1 is the last, -2 is the
136 * second-to-last etc)
138 protected TestBenchElement getFooterRow(int row) {
139 return getRow("tfoot", row);
144 * the index of the row element in the section. If negative, the
145 * calculation starts from the end (-1 is the last, -2 is the
146 * second-to-last etc)
148 protected TestBenchElement getHeaderCell(int row, int col) {
149 return getCell("thead", row, col);
154 * the index of the row element in the section. If negative, the
155 * calculation starts from the end (-1 is the last, -2 is the
156 * second-to-last etc)
158 protected TestBenchElement getBodyCell(int row, int col) {
159 return getCell("tbody", row, col);
164 * the index of the row element in the section. If negative, the
165 * calculation starts from the end (-1 is the last, -2 is the
166 * second-to-last etc)
168 protected TestBenchElement getFooterCell(int row, int col) {
169 return getCell("tfoot", row, col);
174 * the index of the row element in the section. If negative, the
175 * calculation starts from the end (-1 is the last, -2 is the
176 * second-to-last etc)
178 private TestBenchElement getCell(String sectionTag, int row, int col) {
179 TestBenchElement rowElement = getRow(sectionTag, row);
180 By xpath = By.xpath("*[" + (col + 1) + "]");
181 if (rowElement != null && rowElement.isElementPresent(xpath)) {
182 return (TestBenchElement) rowElement.findElement(xpath);
189 * the index of the row element in the section. If negative, the
190 * calculation starts from the end (-1 is the last, -2 is the
191 * second-to-last etc)
193 private TestBenchElement getRow(String sectionTag, int row) {
194 TestBenchElement escalator = getEscalator();
195 WebElement tableSection = escalator.findElement(By.tagName(sectionTag));
197 String xpathExpression = "tr[not(@class='" + SPACER_CSS_CLASS + "')]";
199 int fromFirst = row + 1;
200 xpathExpression += "[" + fromFirst + "]";
202 int fromLast = Math.abs(row + 1);
203 xpathExpression += "[last() - " + fromLast + "]";
205 By xpath = By.xpath(xpathExpression);
206 if (tableSection != null
207 && ((TestBenchElement) tableSection).isElementPresent(xpath)) {
208 return (TestBenchElement) tableSection.findElement(xpath);
214 protected void selectMenu(String menuCaption) {
215 // GWT menu does not need to be clicked.
216 selectMenu(menuCaption, false);
220 protected WebElement getMenuElement(String menuCaption) {
222 .findElement(By.xpath("//td[text() = '" + menuCaption + "']"));
225 protected void assertLogContains(String substring) {
226 assertTrue("log should've contained, but didn't: " + substring,
227 getLogText().contains(substring));
230 protected void assertLogDoesNotContain(String substring) {
231 assertFalse("log shouldn't have contained, but did: " + substring,
232 getLogText().contains(substring));
235 private String getLogText() {
236 WebElement log = findElement(By.cssSelector("#log"));
237 return log.getText();
240 protected void assertLogContainsInOrder(String... substrings) {
241 String log = getLogText();
243 for (String substring : substrings) {
244 String remainingLog = log.substring(cursor, log.length());
245 int substringIndex = remainingLog.indexOf(substring);
246 if (substringIndex == -1) {
247 fail("substring \"" + substring
248 + "\" was not found in order from log.");
251 cursor += substringIndex + substring.length();
255 protected void scrollVerticallyTo(int px) {
256 getVerticalScrollbar().scroll(px);
259 protected long getScrollTop() {
260 return ((Long) executeScript("return arguments[0].scrollTop;",
261 getVerticalScrollbar())).longValue();
264 private TestBenchElement getVerticalScrollbar() {
265 return (TestBenchElement) getEscalator()
266 .findElement(By.className("v-escalator-scroller-vertical"));
269 protected void scrollHorizontallyTo(int px) {
270 getHorizontalScrollbar().scrollLeft(px);
273 protected long getScrollLeft() {
274 return ((Long) executeScript("return arguments[0].scrollLeft;",
275 getHorizontalScrollbar())).longValue();
278 protected TestBenchElement getHorizontalScrollbar() {
279 return (TestBenchElement) getEscalator()
280 .findElement(By.className("v-escalator-scroller-horizontal"));
284 protected Object executeScript(String script, Object... args) {
285 return ((JavascriptExecutor) getDriver()).executeScript(script, args);
288 protected void populate() {
289 selectMenuPath(GENERAL, POPULATE_COLUMN_ROW);
292 private List<WebElement> getSpacers() {
293 return getEscalator().findElements(By.className(SPACER_CSS_CLASS));
296 protected boolean spacersAreFoundInDom() {
297 List<WebElement> spacers = getSpacers();
298 return spacers != null && !spacers.isEmpty();
301 @SuppressWarnings("boxing")
302 protected WebElement getSpacer(int logicalRowIndex) {
303 List<WebElement> spacers = getSpacers();
304 System.out.println("size: " + spacers.size());
305 for (WebElement spacer : spacers) {
306 System.out.println(spacer + ", " + logicalRowIndex);
307 Boolean isInDom = (Boolean) executeScript("return arguments[0]['"
308 + LOGICAL_ROW_ATTRIBUTE_NAME + "'] === arguments[1]",
309 spacer, logicalRowIndex);