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.

TreeGridElement.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 org.openqa.selenium.NoSuchElementException;
  18. import org.openqa.selenium.WebElement;
  19. import com.vaadin.testbench.By;
  20. /**
  21. * TestBench Element API for TreeGrid
  22. *
  23. * @author Vaadin Ltd
  24. */
  25. public class TreeGridElement extends GridElement {
  26. /**
  27. * Expands the row at the given index in the grid. This expects the first
  28. * column to have the hierarchy data.
  29. *
  30. * @param rowIndex
  31. * 0-based row index to expand
  32. * @see #expandWithClick(int, int)
  33. */
  34. public void expandWithClick(int rowIndex) {
  35. expandWithClick(rowIndex, 0);
  36. }
  37. /**
  38. * Expands the row at the given index in the grid with the given
  39. * hierarchical column index.
  40. *
  41. * @param rowIndex
  42. * 0-based row index to expand
  43. * @param hierarchyColumnIndex
  44. * 0-based index of the hierarchy column
  45. */
  46. public void expandWithClick(int rowIndex, int hierarchyColumnIndex) {
  47. if (isRowExpanded(rowIndex, hierarchyColumnIndex)) {
  48. throw new IllegalStateException(
  49. "The element at row " + rowIndex + " was expanded already");
  50. }
  51. getExpandElement(rowIndex, hierarchyColumnIndex).click();
  52. }
  53. /**
  54. * Collapses the row at the given index in the grid. This expects the first
  55. * column to have the hierarchy data.
  56. *
  57. * @param rowIndex
  58. * 0-based row index to collapse
  59. * @see #collapseWithClick(int, int)
  60. */
  61. public void collapseWithClick(int rowIndex) {
  62. collapseWithClick(rowIndex, 0);
  63. }
  64. /**
  65. * Collapses the row at the given index in the grid with the given
  66. * hierarchical column index.
  67. *
  68. * @param rowIndex
  69. * 0-based row index to collapse
  70. * @param hierarchyColumnIndex
  71. * 0-based index of the hierarchy column
  72. */
  73. public void collapseWithClick(int rowIndex, int hierarchyColumnIndex) {
  74. if (isRowCollapsed(rowIndex, hierarchyColumnIndex)) {
  75. throw new IllegalStateException("The element at row " + rowIndex
  76. + " was collapsed already");
  77. }
  78. getExpandElement(rowIndex, hierarchyColumnIndex).click();
  79. }
  80. /**
  81. * Returns whether the row at the given index is expanded or not.
  82. *
  83. * @param rowIndex
  84. * 0-based row index
  85. * @param hierarchyColumnIndex
  86. * 0-based index of the hierarchy column
  87. * @return {@code true} if expanded, {@code false} if collapsed
  88. */
  89. public boolean isRowExpanded(int rowIndex, int hierarchyColumnIndex) {
  90. WebElement expandElement = getExpandElement(rowIndex,
  91. hierarchyColumnIndex);
  92. return expandElement.getAttribute("expanded") != null
  93. && expandElement.getAttribute("collapsed") == null;
  94. }
  95. /**
  96. * Returns whether the row at the given index is collapsed or not.
  97. *
  98. * @param rowIndex
  99. * 0-based row index
  100. * @param hierarchyColumnIndex
  101. * 0-based index of the hierarchy column
  102. * @return {@code true} if collapsed, {@code false} if expanded
  103. */
  104. public boolean isRowCollapsed(int rowIndex, int hierarchyColumnIndex) {
  105. WebElement expandElement = getExpandElement(rowIndex,
  106. hierarchyColumnIndex);
  107. return expandElement.getAttribute("collapsed") != null
  108. && expandElement.getAttribute("expanded") == null;
  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. }