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.

TabSheetElement.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright 2000-2018 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.TestBench;
  23. import com.vaadin.testbench.TestBenchElement;
  24. import com.vaadin.testbench.elementsbase.AbstractElement;
  25. import com.vaadin.testbench.elementsbase.ServerClass;
  26. @ServerClass("com.vaadin.ui.TabSheet")
  27. public class TabSheetElement extends AbstractComponentContainerElement {
  28. // a locator that does not lead to selecting tabs from a contained inner
  29. // TabSheet (#13735)
  30. protected org.openqa.selenium.By byTabCell = By.xpath(
  31. "./div/table/tbody/tr/td[contains(normalize-space(concat(' ', @class, ' ')),"
  32. + "normalize-space(' v-tabsheet-tabitem '))]");
  33. private static org.openqa.selenium.By byCaption = By
  34. .className("v-captiontext");
  35. private static org.openqa.selenium.By byClosable = By
  36. .className("v-tabsheet-caption-close");
  37. /**
  38. * Gets a list of Tabs inside the Tab container.
  39. *
  40. * @return List of tabs
  41. */
  42. public List<String> getTabCaptions() {
  43. List<String> tabCaptions = new ArrayList<String>();
  44. for (WebElement tab : findElements(byTabCell)) {
  45. tabCaptions.add(getTabCaption(tab));
  46. }
  47. return tabCaptions;
  48. }
  49. /**
  50. * Gets the number of tabs contained in this tab sheet.
  51. *
  52. * @return Number of tabs.
  53. */
  54. public int getTabCount() {
  55. return findElements(byTabCell).size();
  56. }
  57. /**
  58. * Opens the tab with the given index.
  59. *
  60. * @param index
  61. * The zero-based index of the tab to be opened.
  62. */
  63. public void openTab(int index) {
  64. List<WebElement> tabs = findElements(byTabCell);
  65. if (index < 0 || index >= tabs.size()) {
  66. throw new NoSuchElementException(
  67. "The tab sheet does not contain a tab with index " + index
  68. + ".");
  69. }
  70. openTab(tabs.get(index));
  71. }
  72. /**
  73. * Opens a Tab that has caption equal to given tabCaption.
  74. *
  75. * @param tabCaption
  76. * Caption of the tab to be opened
  77. */
  78. public void openTab(String tabCaption) {
  79. for (WebElement tabCell : findElements(byTabCell)) {
  80. String currentCaption = getTabCaption(tabCell);
  81. boolean captionMatches = (currentCaption != null
  82. && currentCaption.equals(tabCaption))
  83. || (currentCaption == null && tabCaption == null);
  84. if (captionMatches) {
  85. openTab(tabCell);
  86. return;
  87. }
  88. }
  89. throw new NoSuchElementException(
  90. "Tab with caption " + tabCaption + " was not found.");
  91. }
  92. /**
  93. * Opens the given tab by clicking its caption text or icon. If the tab has
  94. * neither text caption nor icon, clicks at a fixed position.
  95. *
  96. * @param tabCell
  97. * The tab to be opened.
  98. */
  99. private void openTab(WebElement tabCell) {
  100. // Open the tab by clicking its caption text if it exists.
  101. List<WebElement> tabCaptions = tabCell.findElements(byCaption);
  102. if (!tabCaptions.isEmpty()) {
  103. tabCaptions.get(0).click();
  104. return;
  105. }
  106. // If no caption text was found, click the icon of the tab.
  107. List<WebElement> tabIcons = tabCell
  108. .findElements(By.className("v-icon"));
  109. if (!tabIcons.isEmpty()) {
  110. tabIcons.get(0).click();
  111. return;
  112. }
  113. // If neither text nor icon caption was found, click at a position that
  114. // is unlikely to close the tab.
  115. ((TestBenchElement) tabCell).click(-5, 0);
  116. }
  117. /**
  118. * If the tab with given index is closable, closes it.
  119. *
  120. * @param index
  121. * The index of the tab to be closed
  122. */
  123. public void closeTab(int index) {
  124. List<WebElement> tabs = findElements(byTabCell);
  125. if (index < 0 || index >= tabs.size()) {
  126. throw new NoSuchElementException(
  127. "The tab sheet does not contain a tab with index " + index
  128. + ".");
  129. }
  130. WebElement tabCell = tabs.get(index);
  131. closeTab(tabCell);
  132. }
  133. /**
  134. * If tab with given caption is closable, closes it.
  135. *
  136. * @param tabCaption
  137. * Caption of the tab to be closed
  138. */
  139. public void closeTab(String tabCaption) {
  140. for (WebElement tabCell : findElements(byTabCell)) {
  141. String currentCaption = getTabCaption(tabCell);
  142. boolean captionMatches = (currentCaption != null
  143. && currentCaption.equals(tabCaption))
  144. || (currentCaption == null && tabCaption == null);
  145. if (captionMatches) {
  146. closeTab(tabCell);
  147. return;
  148. }
  149. }
  150. }
  151. /**
  152. * Closes the given tab if it is closable.
  153. *
  154. * @param tabCell
  155. * The tab to be closed
  156. */
  157. private void closeTab(WebElement tabCell) {
  158. try {
  159. tabCell.findElement(byClosable).click();
  160. // Going further causes a StaleElementReferenceException.
  161. return;
  162. } catch (NoSuchElementException e) {
  163. // Do nothing.
  164. }
  165. }
  166. /**
  167. * Gets TabSheet content and wraps it in given class.
  168. *
  169. * @param clazz
  170. * Components element class
  171. * @return TabSheet content wrapped in given class
  172. */
  173. public <T extends AbstractElement> T getContent(Class<T> clazz) {
  174. return TestBench.createElement(clazz,
  175. $$(AbstractComponentElement.class).first().getWrappedElement(),
  176. getCommandExecutor());
  177. }
  178. /**
  179. * Returns the caption text of the given tab. If the tab has no caption,
  180. * returns null.
  181. *
  182. * @param tabCell
  183. * A web element representing a tab, as given by
  184. * findElements(byTabCell).get(index).
  185. * @return The caption of tabCell or null if tabCell has no caption.
  186. */
  187. private String getTabCaption(WebElement tabCell) {
  188. List<WebElement> captionElements = tabCell.findElements(byCaption);
  189. if (captionElements.isEmpty()) {
  190. return null;
  191. } else {
  192. return captionElements.get(0).getText();
  193. }
  194. }
  195. }