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.

GridElement.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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.testbench.elements;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import org.openqa.selenium.NoSuchElementException;
  20. import org.openqa.selenium.WebElement;
  21. import com.vaadin.testbench.By;
  22. import com.vaadin.testbench.TestBenchElement;
  23. import com.vaadin.testbench.elementsbase.AbstractElement;
  24. import com.vaadin.testbench.elementsbase.ServerClass;
  25. /**
  26. * TestBench Element API for Grid
  27. *
  28. * @since
  29. * @author Vaadin Ltd
  30. */
  31. @ServerClass("com.vaadin.ui.Grid")
  32. public class GridElement extends AbstractComponentElement {
  33. public static class GridCellElement extends AbstractElement {
  34. private static final String FOCUSED_CELL_CLASS_NAME = "-cell-focused";
  35. private static final String FROZEN_CLASS_NAME = "frozen";
  36. public boolean isFocused() {
  37. return getAttribute("class").contains(FOCUSED_CELL_CLASS_NAME);
  38. }
  39. public boolean isFrozen() {
  40. return getAttribute("class").contains(FROZEN_CLASS_NAME);
  41. }
  42. }
  43. public static class GridRowElement extends AbstractElement {
  44. private static final String FOCUSED_CLASS_NAME = "-row-focused";
  45. private static final String SELECTED_CLASS_NAME = "-row-selected";
  46. public boolean isFocused() {
  47. return getAttribute("class").contains(FOCUSED_CLASS_NAME);
  48. }
  49. @Override
  50. public boolean isSelected() {
  51. return getAttribute("class").contains(SELECTED_CLASS_NAME);
  52. }
  53. }
  54. public static class GridEditorElement extends AbstractElement {
  55. private GridElement grid;
  56. private GridEditorElement setGrid(GridElement grid) {
  57. this.grid = grid;
  58. return this;
  59. }
  60. /**
  61. * Gets the editor field for column in given index.
  62. *
  63. * @param colIndex
  64. * the column index
  65. * @return the editor field for given location
  66. *
  67. * @throws NoSuchElementException
  68. * if {@code isEditable(colIndex) == false}
  69. */
  70. public TestBenchElement getField(int colIndex) {
  71. return grid.getSubPart("#editor[" + colIndex + "]");
  72. }
  73. /**
  74. * Gets whether the column with the given index is editable, that is,
  75. * has an associated editor field.
  76. *
  77. * @param colIndex
  78. * the column index
  79. * @return {@code true} if the column has an editor field, {@code false}
  80. *  otherwise
  81. */
  82. public boolean isEditable(int colIndex) {
  83. return grid
  84. .isElementPresent(By.vaadin("#editor[" + colIndex + "]"));
  85. }
  86. /**
  87. * Checks whether a field is marked with an error.
  88. *
  89. * @param colIndex
  90. * column index
  91. * @return <code>true</code> iff the field is marked with an error
  92. */
  93. public boolean isFieldErrorMarked(int colIndex) {
  94. return getField(colIndex).getAttribute("class").contains("error");
  95. }
  96. /**
  97. * Saves the fields of this editor.
  98. * <p>
  99. * <em>Note:</em> that this closes the editor making this element
  100. * useless.
  101. */
  102. public void save() {
  103. findElement(By.className("v-grid-editor-save")).click();
  104. }
  105. /**
  106. * Cancels this editor.
  107. * <p>
  108. * <em>Note:</em> that this closes the editor making this element
  109. * useless.
  110. */
  111. public void cancel() {
  112. findElement(By.className("v-grid-editor-cancel")).click();
  113. }
  114. /**
  115. * Gets the error message text, or <code>null</code> if no message is
  116. * present.
  117. */
  118. public String getErrorMessage() {
  119. WebElement messageWrapper = findElement(By
  120. .className("v-grid-editor-message"));
  121. List<WebElement> divs = messageWrapper.findElements(By
  122. .tagName("div"));
  123. if (divs.isEmpty()) {
  124. return null;
  125. } else {
  126. return divs.get(0).getText();
  127. }
  128. }
  129. }
  130. /**
  131. * Scrolls Grid element so that wanted row is displayed
  132. *
  133. * @param index
  134. * Target row
  135. */
  136. public void scrollToRow(int index) {
  137. try {
  138. getSubPart("#cell[" + index + "]");
  139. } catch (NoSuchElementException e) {
  140. // Expected, ignore it.
  141. }
  142. }
  143. /**
  144. * Gets cell element with given row and column index.
  145. *
  146. * @param rowIndex
  147. * Row index
  148. * @param colIndex
  149. * Column index
  150. * @return Cell element with given indices.
  151. */
  152. public GridCellElement getCell(int rowIndex, int colIndex) {
  153. scrollToRow(rowIndex);
  154. return getSubPart("#cell[" + rowIndex + "][" + colIndex + "]").wrap(
  155. GridCellElement.class);
  156. }
  157. /**
  158. * Gets row element with given row index.
  159. *
  160. * @param index
  161. * Row index
  162. * @return Row element with given index.
  163. */
  164. public GridRowElement getRow(int index) {
  165. scrollToRow(index);
  166. return getSubPart("#cell[" + index + "]").wrap(GridRowElement.class);
  167. }
  168. /**
  169. * Gets header cell element with given row and column index.
  170. *
  171. * @param rowIndex
  172. * Row index
  173. * @param colIndex
  174. * Column index
  175. * @return Header cell element with given indices.
  176. */
  177. public GridCellElement getHeaderCell(int rowIndex, int colIndex) {
  178. return getSubPart("#header[" + rowIndex + "][" + colIndex + "]").wrap(
  179. GridCellElement.class);
  180. }
  181. /**
  182. * Gets footer cell element with given row and column index.
  183. *
  184. * @param rowIndex
  185. * Row index
  186. * @param colIndex
  187. * Column index
  188. * @return Footer cell element with given indices.
  189. */
  190. public GridCellElement getFooterCell(int rowIndex, int colIndex) {
  191. return getSubPart("#footer[" + rowIndex + "][" + colIndex + "]").wrap(
  192. GridCellElement.class);
  193. }
  194. /**
  195. * Gets list of header cell elements on given row.
  196. *
  197. * @param rowIndex
  198. * Row index
  199. * @return Header cell elements on given row.
  200. */
  201. public List<GridCellElement> getHeaderCells(int rowIndex) {
  202. List<GridCellElement> headers = new ArrayList<GridCellElement>();
  203. for (TestBenchElement e : TestBenchElement.wrapElements(
  204. getSubPart("#header[" + rowIndex + "]").findElements(
  205. By.xpath("./th")), getCommandExecutor())) {
  206. headers.add(e.wrap(GridCellElement.class));
  207. }
  208. return headers;
  209. }
  210. /**
  211. * Gets list of header cell elements on given row.
  212. *
  213. * @param rowIndex
  214. * Row index
  215. * @return Header cell elements on given row.
  216. */
  217. public List<GridCellElement> getFooterCells(int rowIndex) {
  218. List<GridCellElement> footers = new ArrayList<GridCellElement>();
  219. for (TestBenchElement e : TestBenchElement.wrapElements(
  220. getSubPart("#footer[" + rowIndex + "]").findElements(
  221. By.xpath("./td")), getCommandExecutor())) {
  222. footers.add(e.wrap(GridCellElement.class));
  223. }
  224. return footers;
  225. }
  226. /**
  227. * Get header row count
  228. *
  229. * @return Header row count
  230. */
  231. public int getHeaderCount() {
  232. return getSubPart("#header").findElements(By.xpath("./tr")).size();
  233. }
  234. /**
  235. * Get footer row count
  236. *
  237. * @return Footer row count
  238. */
  239. public int getFooterCount() {
  240. return getSubPart("#footer").findElements(By.xpath("./tr")).size();
  241. }
  242. /**
  243. * Get a header row by index
  244. *
  245. * @param rowIndex
  246. * Row index
  247. * @return The th element of the row
  248. */
  249. public TestBenchElement getHeaderRow(int rowIndex) {
  250. return getSubPart("#header[" + rowIndex + "]");
  251. }
  252. /**
  253. * Get a footer row by index
  254. *
  255. * @param rowIndex
  256. * Row index
  257. * @return The tr element of the row
  258. */
  259. public TestBenchElement getFooterRow(int rowIndex) {
  260. return getSubPart("#footer[" + rowIndex + "]");
  261. }
  262. /**
  263. * Get the vertical scroll element
  264. *
  265. * @return The element representing the vertical scrollbar
  266. */
  267. public TestBenchElement getVerticalScroller() {
  268. List<WebElement> rootElements = findElements(By.xpath("./div"));
  269. return (TestBenchElement) rootElements.get(0);
  270. }
  271. /**
  272. * Get the horizontal scroll element
  273. *
  274. * @return The element representing the horizontal scrollbar
  275. */
  276. public TestBenchElement getHorizontalScroller() {
  277. List<WebElement> rootElements = findElements(By.xpath("./div"));
  278. return (TestBenchElement) rootElements.get(1);
  279. }
  280. /**
  281. * Get the header element
  282. *
  283. * @return The thead element
  284. */
  285. public TestBenchElement getHeader() {
  286. return getSubPart("#header");
  287. }
  288. /**
  289. * Get the body element
  290. *
  291. * @return the tbody element
  292. */
  293. public TestBenchElement getBody() {
  294. return getSubPart("#cell");
  295. }
  296. /**
  297. * Get the footer element
  298. *
  299. * @return the tfoot element
  300. */
  301. public TestBenchElement getFooter() {
  302. return getSubPart("#footer");
  303. }
  304. /**
  305. * Get the element wrapping the table element
  306. *
  307. * @return The element that wraps the table element
  308. */
  309. public TestBenchElement getTableWrapper() {
  310. List<WebElement> rootElements = findElements(By.xpath("./div"));
  311. return (TestBenchElement) rootElements.get(2);
  312. }
  313. public GridEditorElement getEditor() {
  314. return getSubPart("#editor").wrap(GridEditorElement.class)
  315. .setGrid(this);
  316. }
  317. /**
  318. * Helper function to get Grid subparts wrapped correctly
  319. *
  320. * @param subPartSelector
  321. * SubPart to be used in ComponentLocator
  322. * @return SubPart element wrapped in TestBenchElement class
  323. */
  324. private TestBenchElement getSubPart(String subPartSelector) {
  325. return (TestBenchElement) findElement(By.vaadin(subPartSelector));
  326. }
  327. }