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.

MenuBarUITest.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package com.vaadin.tests.elements.menubar;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertFalse;
  4. import static org.junit.Assert.assertTrue;
  5. import java.util.List;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import org.openqa.selenium.By;
  9. import org.openqa.selenium.WebDriverException;
  10. import org.openqa.selenium.WebElement;
  11. import com.vaadin.testbench.elements.MenuBarElement;
  12. import com.vaadin.tests.tb3.MultiBrowserTest;
  13. public class MenuBarUITest extends MultiBrowserTest {
  14. @Override
  15. protected boolean requireWindowFocusForIE() {
  16. return true;
  17. }
  18. @Before
  19. public void init() {
  20. openTestURL();
  21. }
  22. // Tests against bug #14568
  23. @Test
  24. public void testClickTopLevelItemHavingSubmenuItemFocused() {
  25. MenuBarElement menuBar = $(MenuBarElement.class).first();
  26. menuBar.clickItem("File");
  27. waitForElementPresent(By.className("v-menubar-popup"));
  28. assertTrue(isItemVisible("Export.."));
  29. menuBar.clickItem("Export..");
  30. sleep(100);
  31. assertTrue(isItemVisible("As PDF..."));
  32. menuBar.clickItem("File");
  33. sleep(100);
  34. assertFalse(isItemVisible("Export.."));
  35. }
  36. /**
  37. * Validates clickItem(String) of MenuBarElement.
  38. */
  39. @Test
  40. public void testMenuBarClick() {
  41. MenuBarElement menuBar = $(MenuBarElement.class).first();
  42. menuBar.clickItem("File");
  43. waitForElementPresent(By.className("v-menubar-popup"));
  44. assertTrue(isItemVisible("Save As.."));
  45. menuBar.clickItem("Export..");
  46. sleep(100);
  47. assertTrue(isItemVisible("As PDF..."));
  48. // The Edit menu will be opened by moving the mouse over the item (done
  49. // by clickItem). The first click then actually closes the menu.
  50. menuBar.clickItem("Edit");
  51. waitForElementNotPresent(By.className("v-menubar-popup"));
  52. menuBar.clickItem("Edit");
  53. waitForElementPresent(By.className("v-menubar-popup"));
  54. assertFalse(isItemVisible("Save As.."));
  55. assertTrue(isItemVisible("Paste"));
  56. menuBar.clickItem("Edit");
  57. sleep(100);
  58. assertFalse(isItemVisible("Save As.."));
  59. assertFalse(isItemVisible("Paste"));
  60. menuBar.clickItem("Edit");
  61. sleep(100);
  62. assertFalse(isItemVisible("Save As.."));
  63. assertTrue(isItemVisible("Paste"));
  64. // Menu does not contain a submenu, no need to click twice.
  65. menuBar.clickItem("Help");
  66. sleep(100);
  67. assertFalse(isItemVisible("Save As.."));
  68. assertFalse(isItemVisible("Paste"));
  69. // No submenu is open, so click only once to open the File menu.
  70. menuBar.clickItem("File");
  71. sleep(100);
  72. assertTrue(isItemVisible("Save As.."));
  73. }
  74. /**
  75. * Validates menuBar.clickItem(String...) feature.
  76. */
  77. @Test
  78. public void testMenuBarClickPath() {
  79. MenuBarElement menuBar = $(MenuBarElement.class).first();
  80. menuBar.clickItem("File", "Export..");
  81. waitForElementPresent(By.className("v-menubar-popup"));
  82. assertTrue(isItemVisible("As Doc..."));
  83. }
  84. /**
  85. * Tests whether the selected MenuBar and its items are the correct ones.
  86. */
  87. @Test
  88. public void testMenuBarSelector() {
  89. MenuBarElement menuBar = $(MenuBarElement.class).get(2);
  90. menuBar.clickItem("File");
  91. waitForElementPresent(By.className("v-menubar-popup"));
  92. assertTrue(isItemVisible("Open2"));
  93. // Close the menu item
  94. menuBar.clickItem("File");
  95. waitForElementNotPresent(By.className("v-menubar-popup"));
  96. menuBar = $(MenuBarElement.class).get(1);
  97. menuBar.clickItem("Edit2");
  98. waitForElementPresent(By.className("v-menubar-popup"));
  99. assertTrue(isItemVisible("Cut"));
  100. menuBar.clickItem("Edit2");
  101. waitForElementNotPresent(By.className("v-menubar-popup"));
  102. menuBar = $(MenuBarElement.class).first();
  103. menuBar.clickItem("File");
  104. waitForElementPresent(By.className("v-menubar-popup"));
  105. assertTrue(isItemVisible("Open"));
  106. }
  107. @Test
  108. public void testMenuItemTooltips() {
  109. MenuBarElement first = $(MenuBarElement.class).first();
  110. first.clickItem("File");
  111. waitForElementPresent(By.className("v-menubar-popup"));
  112. assertTooltip("Open", "<b>Preformatted</b>\ndescription");
  113. assertTooltip("Save", "plain description, <b>HTML</b> is visible");
  114. assertTooltip("Exit", "HTML\ndescription");
  115. }
  116. private void assertTooltip(String menuItem, String expectedTooltipText) {
  117. testBenchElement(getMenuElement(menuItem)).showTooltip();
  118. assertEquals("Unexpected tooltip when hovering '" + menuItem + "'",
  119. expectedTooltipText,
  120. findElement(By.className("v-tooltip-text")).getText());
  121. }
  122. private boolean isItemVisible(String item) {
  123. for (WebElement webElement : getItemCaptions()) {
  124. try {
  125. if (webElement.getText().equals(item)) {
  126. return true;
  127. }
  128. } catch (WebDriverException e) {
  129. // stale, detached element is not visible
  130. return false;
  131. }
  132. }
  133. return false;
  134. }
  135. private List<WebElement> getItemCaptions() {
  136. return findElements(By.className("v-menubar-menuitem-caption"));
  137. }
  138. }