aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/test/java/com/vaadin/tests/components/tabsheet/NewSelectionAfterTabRemoveTest.java
blob: 607baafbd45a245ee64643bce5c205770f75136f (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
103
104
105
106
package com.vaadin.tests.components.tabsheet;

import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.List;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.vaadin.testbench.TestBenchElement;
import com.vaadin.tests.tb3.MultiBrowserTest;

/**
 * Automatic test of the default TabSheet selection algorithm when removing a
 * selected tab.
 *
 * @author Vaadin Ltd
 */
public class NewSelectionAfterTabRemoveTest extends MultiBrowserTest {

    @Test
    public void testSelection() throws IOException, InterruptedException {
        openTestURL();

        while (scrollRight()) {
        }

        selectAndClose(tab(19));

        assertTrue("Tab 18 selected", isTabSelected(tab(18)));

        selectAndClose(tab(16));

        assertTrue("Tab 17 selected", isTabSelected(tab(17)));
    }

    /*
     * Select the specified tab and close it.
     */
    private void selectAndClose(TestBenchElement tab)
            throws InterruptedException {
        tab.click(5, 5);

        sleep(10);

        tabClose(tab).click(2, 2);

        sleep(10);
    }

    /*
     * Gets the selected state of the specified tab.
     */
    private boolean isTabSelected(TestBenchElement tab) {
        return tab.getAttribute("class")
                .contains("v-tabsheet-tabitemcell-selected")
                && tab.getAttribute("class")
                        .contains("v-tabsheet-tabitemcell-focus");
    }

    /*
     * Scroll the tabsheet bar to the right.
     */
    private boolean scrollRight() {
        List<WebElement> scrollElements = getDriver()
                .findElements(By.className("v-tabsheet-scrollerNext"));
        if (!scrollElements.isEmpty()) {
            TestBenchElement rightScrollElement = (TestBenchElement) scrollElements
                    .get(0);
            rightScrollElement.click(5, 5);
            return true;
        } else {
            return false;
        }
    }

    /*
     * Provide the tab close button for the specified tab.
     */
    private TestBenchElement tabClose(TestBenchElement tab) {
        return (TestBenchElement) tab
                .findElement(By.className("v-tabsheet-caption-close"));
    }

    /*
     * Provide the tab at specified index.
     */
    private TestBenchElement tab(int index) {
        By by = By.className("v-tabsheet-tabitemcell");

        List<WebElement> tabs = getDriver().findElements(by);

        String expected = "Tab " + index;

        for (WebElement tab : tabs) {
            if (tab.getText().startsWith(expected)) {
                return (TestBenchElement) tab;
            }
        }

        return null;
    }

}