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.

TreeElement.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 java.util.stream.Collectors;
  20. import org.openqa.selenium.By;
  21. import org.openqa.selenium.NoSuchElementException;
  22. import org.openqa.selenium.WebElement;
  23. import com.vaadin.testbench.TestBenchElement;
  24. import com.vaadin.testbench.elementsbase.ServerClass;
  25. /**
  26. * Testbench Element API for {@code Tree}.
  27. * <p>
  28. * <strong>Note:</strong> This TreeElement is for the Vaadin 8 version of Tree.
  29. * Use {@link com.vaadin.v7.testbench.elements.TreeElement} for the
  30. * compatibility version.
  31. *
  32. * @author Vaadin Ltd.
  33. * @since 8.1
  34. */
  35. @ServerClass("com.vaadin.ui.Tree")
  36. public class TreeElement extends AbstractComponentElement {
  37. /**
  38. * Expands the row at the given index in the tree.
  39. *
  40. * @param index
  41. * 0-based row index to expand
  42. */
  43. public void expand(int index) {
  44. if (isExpanded(index)) {
  45. throw new IllegalStateException(
  46. "The element at row " + index + " was expanded already");
  47. }
  48. getExpandElement(index).click();
  49. }
  50. /**
  51. * Returns whether the row at the given index is expanded or not.
  52. *
  53. * @param index
  54. * 0-based row index
  55. * @return {@code true} if expanded, {@code false} if collapsed
  56. */
  57. public boolean isExpanded(int index) {
  58. WebElement expandElement = getExpandElement(index);
  59. List<String> classes = Arrays
  60. .asList(expandElement.getAttribute("class").split(" "));
  61. return classes.contains("expanded") && !classes.contains("collapsed");
  62. }
  63. /**
  64. * Returns whether the row at the given index is collapsed or not.
  65. *
  66. * @param rowIndex
  67. * 0-based row index
  68. *
  69. * @return {@code true} if collapsed, {@code false} if expanded
  70. */
  71. public boolean isCollapsed(int rowIndex) {
  72. return !isExpanded(rowIndex);
  73. }
  74. /**
  75. * Gets the expand/collapse element for the given row.
  76. *
  77. * @param rowIndex
  78. * 0-based row index
  79. * @return the {@code span} element that is clicked for expanding/collapsing
  80. * a row
  81. * @throws NoSuchElementException
  82. * if there is no expand element for this row
  83. */
  84. public WebElement getExpandElement(int rowIndex) {
  85. return asTreeGrid().getCell(rowIndex, 0)
  86. .findElement(By.className("v-tree8-expander"));
  87. }
  88. /**
  89. * Collapses the row at the given index in the tree.
  90. *
  91. * @param index
  92. * 0-based row index to collapse
  93. */
  94. public void collapse(int index) {
  95. if (isCollapsed(index)) {
  96. throw new IllegalStateException(
  97. "The element at row " + index + " was collapsed already");
  98. }
  99. getExpandElement(index).click();
  100. }
  101. /**
  102. * Gets all items currently shown in this tree. The returned element objects
  103. * are the rendered contents for each item.
  104. *
  105. * @return list of content elements for all items
  106. */
  107. public List<TestBenchElement> getAllItems() {
  108. return asTreeGrid().getBody().findElements(By.tagName("tr")).stream()
  109. .map(this::findCellContentFromRow).collect(Collectors.toList());
  110. }
  111. /**
  112. * Gets an item at given index. The returned element object is the rendered
  113. * content in the given index.
  114. *
  115. * @param index
  116. * 0-based row index
  117. * @return content element for item at given index
  118. */
  119. public TestBenchElement getItem(int index) {
  120. return findCellContentFromRow(asTreeGrid().getRow(index));
  121. }
  122. /**
  123. * Finds the rendered cell content from given row element. This expects the
  124. * row to contain only a single column rendered with TreeRenderer.
  125. *
  126. * @param rowElement
  127. * the row element
  128. * @return cell content element
  129. */
  130. protected TestBenchElement findCellContentFromRow(WebElement rowElement) {
  131. return TestBenchElement.wrapElement(
  132. rowElement.findElement(By.className("gwt-HTML")),
  133. getCommandExecutor());
  134. }
  135. /**
  136. * Convenience method for accessing the underlying TreeGrid.
  137. *
  138. * @return this tree element as a tree grid element.
  139. */
  140. protected TreeGridElement asTreeGrid() {
  141. return wrap(TreeGridElement.class);
  142. }
  143. }