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.

MenuBarElement.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.List;
  18. import java.util.NoSuchElementException;
  19. import org.openqa.selenium.By;
  20. import org.openqa.selenium.Point;
  21. import org.openqa.selenium.WebElement;
  22. import org.openqa.selenium.interactions.Actions;
  23. import com.vaadin.testbench.elementsbase.ServerClass;
  24. @ServerClass("com.vaadin.ui.MenuBar")
  25. public class MenuBarElement extends AbstractComponentElement {
  26. private Point lastItemLocationMovedTo = null;
  27. /**
  28. * Clicks on a visible item.<br>
  29. * If the item is a top level menu, the submenu is opened if it was closed,
  30. * or closed if it was opened.<br>
  31. * If the item is another submenu, that submenu is opened.<br>
  32. * If the item is not a submenu, it will be clicked and trigger any actions
  33. * associated to it.
  34. *
  35. * @param item
  36. * name of the item to click
  37. * @throws NullPointerException
  38. * if item does not exist or is not visible
  39. */
  40. private void clickItem(String item) {
  41. WebElement webElement = getVisibleItem("#" + item);
  42. if (webElement == null) {
  43. throw new NoSuchElementException(
  44. "Menu item " + item + " is not available.");
  45. }
  46. activateOrOpenSubmenu(webElement, true);
  47. }
  48. /**
  49. * Clicks the item specified by a full path given as variable arguments.<br>
  50. * Fails if path given is not full (ie: last submenu is already opened, and
  51. * path given is last item only).
  52. * <p>
  53. * Example:
  54. * </p>
  55. *
  56. * <pre>
  57. * // clicks on &quot;File&quot; item
  58. * menuBarElement.click(&quot;File&quot;);
  59. * // clicks on &quot;Copy&quot; item in &quot;File&quot; top level menu.
  60. * menuBarElement.click(&quot;File&quot;, &quot;Copy&quot;);
  61. * </pre>
  62. *
  63. * @param path
  64. * Array of items to click through
  65. */
  66. public void clickItem(String... path) {
  67. if (path.length > 1) {
  68. closeAll();
  69. }
  70. for (String itemName : path) {
  71. clickItem(itemName);
  72. }
  73. }
  74. /**
  75. * Closes all submenus, if any is open.<br>
  76. * This is done by clicking on the currently selected top level item.
  77. */
  78. private void closeAll() {
  79. lastItemLocationMovedTo = null;
  80. WebElement selectedItem = getSelectedTopLevelItem();
  81. if (selectedItem != null) {
  82. activateOrOpenSubmenu(selectedItem, true);
  83. }
  84. }
  85. private WebElement getSelectedTopLevelItem() {
  86. List<WebElement> selectedItems = findElements(
  87. By.className("v-menubar-menuitem-selected"));
  88. if (selectedItems.isEmpty()) {
  89. return null;
  90. }
  91. return selectedItems.get(0);
  92. }
  93. private WebElement getVisibleItem(String item) {
  94. return findElement(com.vaadin.testbench.By.vaadin(item));
  95. }
  96. private void activateOrOpenSubmenu(WebElement item, boolean alwaysClick) {
  97. if (lastItemLocationMovedTo == null || !isAnySubmenuVisible()) {
  98. item.click();
  99. if (hasSubmenu(item)) {
  100. lastItemLocationMovedTo = item.getLocation();
  101. }
  102. return;
  103. }
  104. // Assumes mouse is still at position of last clicked element
  105. Actions action = new Actions(getDriver());
  106. action.moveToElement(item);
  107. action.build().perform();
  108. if (isLeaf(item) || isSelectedTopLevelItem(item)) {
  109. lastItemLocationMovedTo = null;
  110. } else {
  111. lastItemLocationMovedTo = item.getLocation();
  112. }
  113. if (alwaysClick || isLeaf(item) || !isAnySubmenuVisible()) {
  114. action = new Actions(getDriver());
  115. action.click();
  116. action.build().perform();
  117. }
  118. }
  119. private boolean isSelectedTopLevelItem(WebElement item) {
  120. WebElement selectedItem = getSelectedTopLevelItem();
  121. if (selectedItem == null) {
  122. return false;
  123. }
  124. String itemCaption = item
  125. .findElements(By.className("v-menubar-menuitem-caption")).get(0)
  126. .getAttribute("innerHTML");
  127. String selectedItemCaption = selectedItem
  128. .findElements(By.className("v-menubar-menuitem-caption")).get(0)
  129. .getAttribute("innerHTML");
  130. return itemCaption.equals(selectedItemCaption);
  131. }
  132. private boolean isAnySubmenuVisible() {
  133. WebElement selectedItem = getSelectedTopLevelItem();
  134. if (selectedItem == null) {
  135. return false;
  136. }
  137. return hasSubmenu(selectedItem);
  138. }
  139. private boolean hasSubmenu(WebElement item) {
  140. List<WebElement> submenuIndicatorElements = item
  141. .findElements(By.className("v-menubar-submenu-indicator"));
  142. return submenuIndicatorElements.size() != 0;
  143. }
  144. private boolean isLeaf(WebElement item) {
  145. return !hasSubmenu(item);
  146. }
  147. }