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.

TooltipPositionTest.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.vaadin.tests.components;
  2. import static org.junit.Assert.assertTrue;
  3. import java.util.List;
  4. import org.junit.Test;
  5. import org.openqa.selenium.By;
  6. import org.openqa.selenium.Dimension;
  7. import org.openqa.selenium.Point;
  8. import org.openqa.selenium.StaleElementReferenceException;
  9. import org.openqa.selenium.WebDriver;
  10. import org.openqa.selenium.WebDriver.Window;
  11. import org.openqa.selenium.WebElement;
  12. import org.openqa.selenium.interactions.Actions;
  13. import org.openqa.selenium.support.ui.ExpectedCondition;
  14. import com.vaadin.testbench.elements.ButtonElement;
  15. import com.vaadin.tests.tb3.MultiBrowserTest;
  16. /**
  17. * Tests that the tooltip is positioned so that it fits in the displayed area.
  18. *
  19. * @author Vaadin Ltd
  20. */
  21. public class TooltipPositionTest extends MultiBrowserTest {
  22. @Test
  23. public void testRegression_EmptyTooltipShouldNotBeAppearedDuringInitialization()
  24. throws Exception {
  25. openTestURL();
  26. waitForElementPresent(By.cssSelector(".v-tooltip"));
  27. WebElement tooltip = driver.findElement(By.cssSelector(".v-tooltip"));
  28. assertTrue(
  29. "This init tooltip with text ' ' is present in the DOM and should be entirely outside the browser window",
  30. isOutsideOfWindow(tooltip));
  31. }
  32. @Test
  33. public void testTooltipPosition() throws Exception {
  34. openTestURL();
  35. for (int i = 0; i < TooltipPosition.NUMBER_OF_BUTTONS; i++) {
  36. ButtonElement button = $(ButtonElement.class).get(i);
  37. // Move the mouse to display the tooltip.
  38. Actions actions = new Actions(driver);
  39. actions.moveToElement(button, 10, 10);
  40. actions.build().perform();
  41. waitUntil(tooltipToBeInsideWindow(By.cssSelector(".v-tooltip"),
  42. driver.manage().window()));
  43. if (i < TooltipPosition.NUMBER_OF_BUTTONS - 1) {
  44. // Remove the tooltip by moving the mouse.
  45. actions = new Actions(driver);
  46. actions.moveByOffset(500, -50);
  47. actions.build().perform();
  48. waitUntil(tooltipNotToBeShown(By.cssSelector(".v-tooltip"),
  49. driver.manage().window()));
  50. }
  51. }
  52. }
  53. /*
  54. * An expectation for checking that the tooltip found by the given locator
  55. * is present in the DOM and entirely inside the browser window. The
  56. * coordinate of the top left corner of the window is supposed to be (0, 0).
  57. */
  58. private ExpectedCondition<Boolean> tooltipToBeInsideWindow(
  59. final By tooltipLocator, final Window window) {
  60. return new ExpectedCondition<Boolean>() {
  61. @Override
  62. public Boolean apply(WebDriver input) {
  63. List<WebElement> elements = findElements(tooltipLocator);
  64. if (elements.isEmpty()) {
  65. return false;
  66. }
  67. WebElement element = elements.get(0);
  68. try {
  69. if (!element.isDisplayed()) {
  70. return false;
  71. }
  72. Point topLeft = element.getLocation();
  73. int xLeft = topLeft.getX();
  74. int yTop = topLeft.getY();
  75. if (xLeft < 0 || yTop < 0) {
  76. return false;
  77. }
  78. Dimension elementSize = element.getSize();
  79. int xRight = xLeft + elementSize.getWidth() - 1;
  80. int yBottom = yTop + elementSize.getHeight() - 1;
  81. Dimension browserSize = window.getSize();
  82. return xRight < browserSize.getWidth()
  83. && yBottom < browserSize.getHeight();
  84. } catch (StaleElementReferenceException e) {
  85. return false;
  86. }
  87. }
  88. @Override
  89. public String toString() {
  90. return "the tooltip to be displayed inside the window";
  91. }
  92. };
  93. };
  94. /*
  95. * An expectation for checking that the tooltip found by the given locator
  96. * is not shown in the window, even partially. The top left corner of window
  97. * should have coordinates (0, 0).
  98. */
  99. private ExpectedCondition<Boolean> tooltipNotToBeShown(
  100. final By tooltipLocator, final Window window) {
  101. return new ExpectedCondition<Boolean>() {
  102. @Override
  103. public Boolean apply(WebDriver input) {
  104. List<WebElement> elements = findElements(tooltipLocator);
  105. if (elements.isEmpty()) {
  106. return true;
  107. }
  108. WebElement tooltip = elements.get(0);
  109. try {
  110. return isOutsideOfWindow(tooltip);
  111. } catch (StaleElementReferenceException e) {
  112. return true;
  113. }
  114. }
  115. @Override
  116. public String toString() {
  117. return "the tooltip not to be displayed inside the window";
  118. }
  119. };
  120. }
  121. private boolean isOutsideOfWindow(WebElement tooltip) {
  122. if (!tooltip.isDisplayed()) {
  123. return true;
  124. }
  125. // The tooltip is shown, at least partially, if
  126. // its intervals of both horizontal and vertical coordinates
  127. // overlap those of the window.
  128. Point topLeft = tooltip.getLocation();
  129. Dimension tooltipSize = tooltip.getSize();
  130. Dimension windowSize = driver.manage().window().getSize();
  131. int xLeft = topLeft.getX();
  132. int yTop = topLeft.getY();
  133. int xRight = xLeft + tooltipSize.getWidth() - 1;
  134. int yBottom = yTop + tooltipSize.getHeight() - 1;
  135. boolean overlapHorizontally = !(xRight < 0
  136. || xLeft >= windowSize.getWidth());
  137. boolean overlapVertically = !(yBottom < 0
  138. || yTop >= windowSize.getHeight());
  139. return !(overlapHorizontally && overlapVertically);
  140. }
  141. }