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 10.0KB

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