Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TreeGridElement.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2000-2016 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.Arrays;
  18. import java.util.List;
  19. import org.openqa.selenium.NoSuchElementException;
  20. import org.openqa.selenium.WebElement;
  21. import com.vaadin.testbench.By;
  22. /**
  23. * TestBench Element API for TreeGrid
  24. *
  25. * @author Vaadin Ltd
  26. */
  27. public class TreeGridElement extends GridElement {
  28. /**
  29. * Expands the row at the given index in the grid. This expects the first
  30. * column to have the hierarchy data.
  31. *
  32. * @param rowIndex
  33. * 0-based row index to expand
  34. * @see #expandWithClick(int, int)
  35. */
  36. public void expandWithClick(int rowIndex) {
  37. expandWithClick(rowIndex, 0);
  38. }
  39. /**
  40. * Expands the row at the given index in the grid with the given
  41. * hierarchical column index.
  42. *
  43. * @param rowIndex
  44. * 0-based row index to expand
  45. * @param hierarchyColumnIndex
  46. * 0-based index of the hierarchy column
  47. */
  48. public void expandWithClick(int rowIndex, int hierarchyColumnIndex) {
  49. if (isRowExpanded(rowIndex, hierarchyColumnIndex)) {
  50. throw new IllegalStateException(
  51. "The element at row " + rowIndex + " was expanded already");
  52. }
  53. getExpandElement(rowIndex, hierarchyColumnIndex).click();
  54. }
  55. /**
  56. * Collapses the row at the given index in the grid. This expects the first
  57. * column to have the hierarchy data.
  58. *
  59. * @param rowIndex
  60. * 0-based row index to collapse
  61. * @see #collapseWithClick(int, int)
  62. */
  63. public void collapseWithClick(int rowIndex) {
  64. collapseWithClick(rowIndex, 0);
  65. }
  66. /**
  67. * Collapses the row at the given index in the grid with the given
  68. * hierarchical column index.
  69. *
  70. * @param rowIndex
  71. * 0-based row index to collapse
  72. * @param hierarchyColumnIndex
  73. * 0-based index of the hierarchy column
  74. */
  75. public void collapseWithClick(int rowIndex, int hierarchyColumnIndex) {
  76. if (isRowCollapsed(rowIndex, hierarchyColumnIndex)) {
  77. throw new IllegalStateException("The element at row " + rowIndex
  78. + " was collapsed already");
  79. }
  80. getExpandElement(rowIndex, hierarchyColumnIndex).click();
  81. }
  82. /**
  83. * Returns whether the row at the given index is expanded or not.
  84. *
  85. * @param rowIndex
  86. * 0-based row index
  87. * @param hierarchyColumnIndex
  88. * 0-based index of the hierarchy column
  89. * @return {@code true} if expanded, {@code false} if collapsed
  90. */
  91. public boolean isRowExpanded(int rowIndex, int hierarchyColumnIndex) {
  92. WebElement expandElement = getExpandElement(rowIndex,
  93. hierarchyColumnIndex);
  94. List<String> classes = Arrays
  95. .asList(expandElement.getAttribute("class").split(" "));
  96. return classes.contains("expanded") && !classes.contains("collapsed");
  97. }
  98. /**
  99. * Returns whether the row at the given index is collapsed or not.
  100. *
  101. * @param rowIndex
  102. * 0-based row index
  103. * @param hierarchyColumnIndex
  104. * 0-based index of the hierarchy column
  105. * @return {@code true} if collapsed, {@code false} if expanded
  106. */
  107. public boolean isRowCollapsed(int rowIndex, int hierarchyColumnIndex) {
  108. return !isRowExpanded(rowIndex, hierarchyColumnIndex);
  109. }
  110. /**
  111. * Gets the expand/collapse element for the given row.
  112. *
  113. * @param rowIndex
  114. * 0-based row index
  115. * @param hierarchyColumnIndex
  116. * 0-based index of the hierarchy column
  117. * @return the {@code span} element that is clicked for expanding/collapsing
  118. * a rows
  119. * @throws NoSuchElementException
  120. * if there is no expand element for this row
  121. */
  122. public WebElement getExpandElement(int rowIndex, int hierarchyColumnIndex) {
  123. return getCell(rowIndex, hierarchyColumnIndex)
  124. .findElement(By.className("v-tree-grid-expander"));
  125. }
  126. }