You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EscalatorBasicClientFeaturesTest.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  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
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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
  14. * the License.
  15. */
  16. package com.vaadin.tests.components.grid.basicfeatures;
  17. import static org.junit.Assert.assertFalse;
  18. import static org.junit.Assert.assertTrue;
  19. import static org.junit.Assert.fail;
  20. import org.openqa.selenium.By;
  21. import org.openqa.selenium.Dimension;
  22. import org.openqa.selenium.JavascriptExecutor;
  23. import org.openqa.selenium.WebElement;
  24. import org.openqa.selenium.interactions.Actions;
  25. import com.vaadin.testbench.TestBenchElement;
  26. import com.vaadin.testbench.parallel.TestCategory;
  27. import com.vaadin.tests.tb3.MultiBrowserTest;
  28. @TestCategory("escalator")
  29. public abstract class EscalatorBasicClientFeaturesTest extends MultiBrowserTest {
  30. protected static final String COLUMNS_AND_ROWS = "Columns and Rows";
  31. protected static final String COLUMNS = "Columns";
  32. protected static final String ADD_ONE_COLUMN_TO_BEGINNING = "Add one column to beginning";
  33. protected static final String ADD_ONE_ROW_TO_BEGINNING = "Add one row to beginning";
  34. protected static final String REMOVE_ONE_COLUMN_FROM_BEGINNING = "Remove one column from beginning";
  35. protected static final String REMOVE_ONE_ROW_FROM_BEGINNING = "Remove one row from beginning";
  36. protected static final String REMOVE_50_ROWS_FROM_BOTTOM = "Remove 50 rows from bottom";
  37. protected static final String REMOVE_50_ROWS_FROM_ALMOST_BOTTOM = "Remove 50 rows from almost bottom";
  38. protected static final String ADD_ONE_OF_EACH_ROW = "Add one of each row";
  39. protected static final String RESIZE_FIRST_COLUMN_TO_MAX_WIDTH = "Resize first column to max width";
  40. protected static final String RESIZE_FIRST_COLUMN_TO_100PX = "Resize first column to 100 px";
  41. protected static final String HEADER_ROWS = "Header Rows";
  42. protected static final String BODY_ROWS = "Body Rows";
  43. protected static final String FOOTER_ROWS = "Footer Rows";
  44. protected static final String REMOVE_ALL_INSERT_SCROLL = "Remove all, insert 30 and scroll 40px";
  45. protected static final String GENERAL = "General";
  46. protected static final String DETACH_ESCALATOR = "Detach Escalator";
  47. protected static final String POPULATE_COLUMN_ROW = "Populate Escalator (columns, then rows)";
  48. protected static final String POPULATE_ROW_COLUMN = "Populate Escalator (rows, then columns)";
  49. protected static final String CLEAR_COLUMN_ROW = "Clear (columns, then rows)";
  50. protected static final String CLEAR_ROW_COLUMN = "Clear (rows, then columns)";
  51. protected static final String FEATURES = "Features";
  52. protected static final String FROZEN_COLUMNS = "Frozen columns";
  53. protected static final String FREEZE_1_COLUMN = "Freeze 1 column";
  54. protected static final String FREEZE_0_COLUMNS = "Freeze 0 columns";
  55. protected static final String COLUMN_SPANNING = "Column spanning";
  56. protected static final String COLSPAN_NORMAL = "Apply normal colspan";
  57. protected static final String COLSPAN_NONE = "Apply no colspan";
  58. @Override
  59. protected Class<?> getUIClass() {
  60. return EscalatorBasicClientFeatures.class;
  61. }
  62. protected TestBenchElement getEscalator() {
  63. By className = By.className("v-escalator");
  64. if (isElementPresent(className)) {
  65. return (TestBenchElement) findElement(className);
  66. }
  67. return null;
  68. }
  69. /**
  70. * @param row
  71. * the index of the row element in the section. If negative, the
  72. * calculation starts from the end (-1 is the last, -2 is the
  73. * second-to-last etc)
  74. */
  75. protected TestBenchElement getHeaderRow(int row) {
  76. return getRow("thead", row);
  77. }
  78. /**
  79. * @param row
  80. * the index of the row element in the section. If negative, the
  81. * calculation starts from the end (-1 is the last, -2 is the
  82. * second-to-last etc)
  83. */
  84. protected TestBenchElement getBodyRow(int row) {
  85. return getRow("tbody", row);
  86. }
  87. /**
  88. * @param row
  89. * the index of the row element in the section. If negative, the
  90. * calculation starts from the end (-1 is the last, -2 is the
  91. * second-to-last etc)
  92. */
  93. protected TestBenchElement getFooterRow(int row) {
  94. return getRow("tfoot", row);
  95. }
  96. /**
  97. * @param row
  98. * the index of the row element in the section. If negative, the
  99. * calculation starts from the end (-1 is the last, -2 is the
  100. * second-to-last etc)
  101. */
  102. protected TestBenchElement getHeaderCell(int row, int col) {
  103. return getCell("thead", row, col);
  104. }
  105. /**
  106. * @param row
  107. * the index of the row element in the section. If negative, the
  108. * calculation starts from the end (-1 is the last, -2 is the
  109. * second-to-last etc)
  110. */
  111. protected TestBenchElement getBodyCell(int row, int col) {
  112. return getCell("tbody", row, col);
  113. }
  114. /**
  115. * @param row
  116. * the index of the row element in the section. If negative, the
  117. * calculation starts from the end (-1 is the last, -2 is the
  118. * second-to-last etc)
  119. */
  120. protected TestBenchElement getFooterCell(int row, int col) {
  121. return getCell("tfoot", row, col);
  122. }
  123. /**
  124. * @param row
  125. * the index of the row element in the section. If negative, the
  126. * calculation starts from the end (-1 is the last, -2 is the
  127. * second-to-last etc)
  128. */
  129. private TestBenchElement getCell(String sectionTag, int row, int col) {
  130. TestBenchElement rowElement = getRow(sectionTag, row);
  131. By xpath = By.xpath("*[" + (col + 1) + "]");
  132. if (rowElement != null && rowElement.isElementPresent(xpath)) {
  133. return (TestBenchElement) rowElement.findElement(xpath);
  134. }
  135. return null;
  136. }
  137. /**
  138. * @param row
  139. * the index of the row element in the section. If negative, the
  140. * calculation starts from the end (-1 is the last, -2 is the
  141. * second-to-last etc)
  142. */
  143. private TestBenchElement getRow(String sectionTag, int row) {
  144. TestBenchElement escalator = getEscalator();
  145. WebElement tableSection = escalator.findElement(By.tagName(sectionTag));
  146. By xpath;
  147. if (row >= 0) {
  148. int fromFirst = row + 1;
  149. xpath = By.xpath("tr[" + fromFirst + "]");
  150. } else {
  151. int fromLast = Math.abs(row + 1);
  152. xpath = By.xpath("tr[last() - " + fromLast + "]");
  153. }
  154. if (tableSection != null
  155. && ((TestBenchElement) tableSection).isElementPresent(xpath)) {
  156. return (TestBenchElement) tableSection.findElement(xpath);
  157. }
  158. return null;
  159. }
  160. protected void selectMenu(String menuCaption) {
  161. TestBenchElement menuElement = getMenuElement(menuCaption);
  162. Dimension size = menuElement.getSize();
  163. new Actions(getDriver()).moveToElement(menuElement, size.width - 10,
  164. size.height / 2).perform();
  165. }
  166. private TestBenchElement getMenuElement(String menuCaption) {
  167. return (TestBenchElement) findElement(By.xpath("//td[text() = '"
  168. + menuCaption + "']"));
  169. }
  170. protected void selectMenuPath(String... menuCaptions) {
  171. new Actions(getDriver()).moveToElement(getMenuElement(menuCaptions[0]))
  172. .click().perform();
  173. for (int i = 1; i < menuCaptions.length - 1; ++i) {
  174. selectMenu(menuCaptions[i]);
  175. new Actions(getDriver()).moveByOffset(20, 0).perform();
  176. }
  177. new Actions(getDriver())
  178. .moveToElement(
  179. getMenuElement(menuCaptions[menuCaptions.length - 1]))
  180. .click().perform();
  181. }
  182. protected void assertLogContains(String substring) {
  183. assertTrue("log should've contained, but didn't: " + substring,
  184. getLogText().contains(substring));
  185. }
  186. protected void assertLogDoesNotContain(String substring) {
  187. assertFalse("log shouldn't have contained, but did: " + substring,
  188. getLogText().contains(substring));
  189. }
  190. private String getLogText() {
  191. WebElement log = findElement(By.cssSelector("#log"));
  192. return log.getText();
  193. }
  194. protected void assertLogContainsInOrder(String... substrings) {
  195. String log = getLogText();
  196. int cursor = 0;
  197. for (String substring : substrings) {
  198. String remainingLog = log.substring(cursor, log.length());
  199. int substringIndex = remainingLog.indexOf(substring);
  200. if (substringIndex == -1) {
  201. fail("substring \"" + substring
  202. + "\" was not found in order from log.");
  203. }
  204. cursor += substringIndex + substring.length();
  205. }
  206. }
  207. protected void scrollVerticallyTo(int px) {
  208. executeScript("arguments[0].scrollTop = " + px, getVeticalScrollbar());
  209. }
  210. private TestBenchElement getVeticalScrollbar() {
  211. return (TestBenchElement) getEscalator().findElement(
  212. By.className("v-escalator-scroller-vertical"));
  213. }
  214. protected void scrollHorizontallyTo(int px) {
  215. executeScript("arguments[0].scrollLeft = " + px,
  216. getHorizontalScrollbar());
  217. }
  218. private TestBenchElement getHorizontalScrollbar() {
  219. return (TestBenchElement) getEscalator().findElement(
  220. By.className("v-escalator-scroller-horizontal"));
  221. }
  222. protected Object executeScript(String script, Object... args) {
  223. return ((JavascriptExecutor) getDriver()).executeScript(script, args);
  224. }
  225. protected void populate() {
  226. selectMenuPath(GENERAL, POPULATE_COLUMN_ROW);
  227. }
  228. }