aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/test/java/com/vaadin/tests/tb3/TooltipTest.java
blob: 4bb9f4fd5e30e91fed8e0a1f9bca85782d4a82a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.vaadin.tests.tb3;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.List;
import java.util.NoSuchElementException;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;

import com.vaadin.testbench.By;

/**
 * Base class for TestBench 3+ tests that use tooltips. This class contains
 * utility methods for testing tooltip use.
 *
 * @author Vaadin Ltd
 */
public abstract class TooltipTest extends MultiBrowserTest {

    protected void clearTooltip() throws Exception {
        moveToRoot();
        Thread.sleep(500);
        checkTooltipNotPresent();
    }

    protected void checkTooltip(String locator, String value) throws Exception {
        checkTooltip(By.vaadin(locator), value);
    }

    protected void checkTooltip(org.openqa.selenium.By by, String value)
            throws Exception {
        checkTooltip(getDriver().findElement(by), value);
    }

    protected void checkTooltip(WebElement element, String value)
            throws Exception {
        testBenchElement(element).showTooltip();
        if (null != value) {
            checkTooltip(value);
        } else {
            checkTooltipNotPresent();
        }
    }

    protected void checkTooltip(String value) throws Exception {
        WebElement body = findElement(By.cssSelector("body"));
        WebElement tooltip = getTooltip();
        assertEquals(value, tooltip.getText());
        assertTrue("Tooltip overflowed to the left",
                tooltip.getLocation().getX() >= 0);
        assertTrue("Tooltip overflowed up", tooltip.getLocation().getY() >= 0);
        assertTrue("Tooltip overflowed to the right",
                tooltip.getLocation().getX()
                        + tooltip.getSize().getWidth() < body.getSize()
                                .getWidth());
        assertTrue("Tooltip overflowed down", tooltip.getLocation().getY()
                + tooltip.getSize().getHeight() < body.getSize().getHeight());

    }

    protected void moveToRoot() {
        WebElement uiRoot = getDriver().findElement(By.vaadin("Root"));
        moveMouseToTopLeft(uiRoot);
    }

    protected WebElement getTooltip() throws InterruptedException {
        org.openqa.selenium.By tooltipBy = By.vaadin("Root/VTooltip[0]");
        return getDriver().findElement(tooltipBy);
    }

    protected void checkTooltipNotPresent() throws Exception {
        try {
            WebElement tooltip = getTooltip();
            if (!tooltip.getText().isEmpty()
                    || tooltip.getLocation().getX() > -999) {
                fail("Found tooltip that shouldn't be visible: "
                        + tooltip.getText() + " at " + tooltip.getLocation());
            }
        } catch (NoSuchElementException e) {
            fail("Tooltip element was removed completely, causing extra events to accessibility tools");
        }
    }

    protected void moveMouseToTopLeft(WebElement element) {
        moveMouseTo(element, 0, 0);
    }

    protected void moveMouseTo(WebElement element, int offsetX, int offsetY) {
        new Actions(getDriver()).moveToElement(element,
                getXOffset(element, offsetX), getYOffset(element, offsetY))
                .perform();
    }

    @Override
    public List<DesiredCapabilities> getBrowsersToTest() {
        return getBrowsersSupportingTooltip();
    }
}