aboutsummaryrefslogtreecommitdiffstats
path: root/testbench-api/src/main/java/com/vaadin/testbench/elements/TabSheetElement.java
blob: 7f6004a5afa29e3876691d5fa611a9b91758a485 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Copyright 2000-2016 Vaadin Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.vaadin.testbench.elements;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;

import com.vaadin.testbench.By;
import com.vaadin.testbench.TestBench;
import com.vaadin.testbench.TestBenchElement;
import com.vaadin.testbench.elementsbase.AbstractElement;
import com.vaadin.testbench.elementsbase.ServerClass;

@ServerClass("com.vaadin.ui.TabSheet")
public class TabSheetElement extends AbstractComponentContainerElement {

    // a locator that does not lead to selecting tabs from a contained inner
    // TabSheet (#13735)
    protected org.openqa.selenium.By byTabCell = By.xpath(
            "./div/table/tbody/tr/td[contains(normalize-space(concat(' ', @class, ' ')),"
                    + "normalize-space(' v-tabsheet-tabitem '))]");
    private static org.openqa.selenium.By byCaption = By
            .className("v-captiontext");
    private static org.openqa.selenium.By byClosable = By
            .className("v-tabsheet-caption-close");

    /**
     * Gets a list of Tabs inside the Tab container.
     * 
     * @return List of tabs
     */
    public List<String> getTabCaptions() {
        List<String> tabCaptions = new ArrayList<String>();
        for (WebElement tab : findElements(byTabCell)) {
            tabCaptions.add(getTabCaption(tab));
        }
        return tabCaptions;
    }

    /**
     * Gets the number of tabs contained in this tab sheet.
     * 
     * @return Number of tabs.
     */
    public int getTabCount() {
        return findElements(byTabCell).size();
    }

    /**
     * Opens the tab with the given index.
     * 
     * @param index
     *            The zero-based index of the tab to be opened.
     */
    public void openTab(int index) {
        List<WebElement> tabs = findElements(byTabCell);
        if (index < 0 || index >= tabs.size()) {
            throw new NoSuchElementException(
                    "The tab sheet does not contain a tab with index " + index
                            + ".");
        }
        openTab(tabs.get(index));
    }

    /**
     * Opens a Tab that has caption equal to given tabCaption.
     * 
     * @param tabCaption
     *            Caption of the tab to be opened
     */
    public void openTab(String tabCaption) {
        for (WebElement tabCell : findElements(byTabCell)) {
            String currentCaption = getTabCaption(tabCell);
            boolean captionMatches = (currentCaption != null
                    && currentCaption.equals(tabCaption))
                    || (currentCaption == null && tabCaption == null);
            if (captionMatches) {
                openTab(tabCell);
                return;
            }
        }
        throw new NoSuchElementException(
                "Tab with caption " + tabCaption + " was not found.");
    }

    /**
     * Opens the given tab by clicking its caption text or icon. If the tab has
     * neither text caption nor icon, clicks at a fixed position.
     * 
     * @param tabCell
     *            The tab to be opened.
     */
    private void openTab(WebElement tabCell) {
        // Open the tab by clicking its caption text if it exists.
        List<WebElement> tabCaptions = tabCell.findElements(byCaption);
        if (!tabCaptions.isEmpty()) {
            tabCaptions.get(0).click();
            return;
        }
        // If no caption text was found, click the icon of the tab.
        List<WebElement> tabIcons = tabCell
                .findElements(By.className("v-icon"));
        if (!tabIcons.isEmpty()) {
            tabIcons.get(0).click();
            return;
        }
        // If neither text nor icon caption was found, click at a position that
        // is unlikely to close the tab.
        ((TestBenchElement) tabCell).click(10, 10);
    }

    /**
     * If the tab with given index is closable, closes it.
     * 
     * @param index
     *            The index of the tab to be closed
     */
    public void closeTab(int index) {
        List<WebElement> tabs = findElements(byTabCell);
        if (index < 0 || index >= tabs.size()) {
            throw new NoSuchElementException(
                    "The tab sheet does not contain a tab with index " + index
                            + ".");
        }
        WebElement tabCell = tabs.get(index);
        closeTab(tabCell);
    }

    /**
     * If tab with given caption is closable, closes it.
     * 
     * @param tabCaption
     *            Caption of the tab to be closed
     */
    public void closeTab(String tabCaption) {
        for (WebElement tabCell : findElements(byTabCell)) {
            String currentCaption = getTabCaption(tabCell);
            boolean captionMatches = (currentCaption != null
                    && currentCaption.equals(tabCaption))
                    || (currentCaption == null && tabCaption == null);
            if (captionMatches) {
                closeTab(tabCell);
                return;
            }
        }
    }

    /**
     * Closes the given tab if it is closable.
     * 
     * @param tabCell
     *            The tab to be closed
     */
    private void closeTab(WebElement tabCell) {
        try {
            tabCell.findElement(byClosable).click();
            // Going further causes a StaleElementReferenceException.
            return;
        } catch (NoSuchElementException e) {
            // Do nothing.
        }
    }

    /**
     * Gets TabSheet content and wraps it in given class.
     * 
     * @param clazz
     *            Components element class
     * @return TabSheet content wrapped in given class
     */
    public <T extends AbstractElement> T getContent(Class<T> clazz) {
        return TestBench.createElement(clazz,
                $$(AbstractComponentElement.class).first().getWrappedElement(),
                getCommandExecutor());
    }

    /**
     * Returns the caption text of the given tab. If the tab has no caption,
     * returns null.
     * 
     * @param tabCell
     *            A web element representing a tab, as given by
     *            findElements(byTabCell).get(index).
     * @return The caption of tabCell or null if tabCell has no caption.
     */
    private String getTabCaption(WebElement tabCell) {
        List<WebElement> captionElements = tabCell.findElements(byCaption);
        if (captionElements.isEmpty()) {
            return null;
        } else {
            return captionElements.get(0).getText();
        }
    }
}