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.

TooltipTest.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.vaadin.tests.tb3;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertTrue;
  4. import static org.junit.Assert.fail;
  5. import java.util.List;
  6. import java.util.NoSuchElementException;
  7. import org.openqa.selenium.WebElement;
  8. import org.openqa.selenium.interactions.Actions;
  9. import org.openqa.selenium.remote.DesiredCapabilities;
  10. import com.vaadin.testbench.By;
  11. /**
  12. * Base class for TestBench 3+ tests that use tooltips. This class contains
  13. * utility methods for testing tooltip use.
  14. *
  15. * @author Vaadin Ltd
  16. */
  17. public abstract class TooltipTest extends MultiBrowserTest {
  18. protected void clearTooltip() throws Exception {
  19. moveToRoot();
  20. Thread.sleep(500);
  21. checkTooltipNotPresent();
  22. }
  23. protected void checkTooltip(String locator, String value) throws Exception {
  24. checkTooltip(By.vaadin(locator), value);
  25. }
  26. protected void checkTooltip(org.openqa.selenium.By by, String value)
  27. throws Exception {
  28. checkTooltip(getDriver().findElement(by), value);
  29. }
  30. protected void checkTooltip(WebElement element, String value)
  31. throws Exception {
  32. testBenchElement(element).showTooltip();
  33. if (null != value) {
  34. checkTooltip(value);
  35. } else {
  36. checkTooltipNotPresent();
  37. }
  38. }
  39. protected void checkTooltip(String value) throws Exception {
  40. WebElement body = findElement(By.cssSelector("body"));
  41. WebElement tooltip = getTooltip();
  42. assertEquals(value, tooltip.getText());
  43. assertTrue("Tooltip overflowed to the left",
  44. tooltip.getLocation().getX() >= 0);
  45. assertTrue("Tooltip overflowed up", tooltip.getLocation().getY() >= 0);
  46. assertTrue("Tooltip overflowed to the right",
  47. tooltip.getLocation().getX()
  48. + tooltip.getSize().getWidth() < body.getSize()
  49. .getWidth());
  50. assertTrue("Tooltip overflowed down", tooltip.getLocation().getY()
  51. + tooltip.getSize().getHeight() < body.getSize().getHeight());
  52. }
  53. protected void moveToRoot() {
  54. WebElement uiRoot = getDriver().findElement(By.vaadin("Root"));
  55. moveMouseToTopLeft(uiRoot);
  56. }
  57. protected WebElement getTooltip() throws InterruptedException {
  58. org.openqa.selenium.By tooltipBy = By.vaadin("Root/VTooltip[0]");
  59. return getDriver().findElement(tooltipBy);
  60. }
  61. protected void checkTooltipNotPresent() throws Exception {
  62. try {
  63. WebElement tooltip = getTooltip();
  64. if (!tooltip.getText().isEmpty()
  65. || tooltip.getLocation().getX() > -999) {
  66. fail("Found tooltip that shouldn't be visible: "
  67. + tooltip.getText() + " at " + tooltip.getLocation());
  68. }
  69. } catch (NoSuchElementException e) {
  70. fail("Tooltip element was removed completely, causing extra events to accessibility tools");
  71. }
  72. }
  73. protected void moveMouseToTopLeft(WebElement element) {
  74. moveMouseTo(element, 0, 0);
  75. }
  76. protected void moveMouseTo(WebElement element, int offsetX, int offsetY) {
  77. new Actions(getDriver()).moveToElement(element,
  78. getXOffset(element, offsetX), getYOffset(element, offsetY))
  79. .perform();
  80. }
  81. @Override
  82. public List<DesiredCapabilities> getBrowsersToTest() {
  83. return getBrowsersSupportingTooltip();
  84. }
  85. }