aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/test/java/com/vaadin/tests/components/combobox/ComboBoxSelectingTest.java
blob: dfb55e4a07817c491eff870345ea3db411b5e910 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package com.vaadin.tests.components.combobox;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;

import com.vaadin.testbench.By;
import com.vaadin.testbench.elements.ComboBoxElement;
import com.vaadin.testbench.elements.LabelElement;
import com.vaadin.testbench.parallel.BrowserUtil;
import com.vaadin.tests.tb3.MultiBrowserTest;

public class ComboBoxSelectingTest extends MultiBrowserTest {

    private ComboBoxElement comboBoxElement;

    @Override
    public void setup() throws Exception {
        super.setup();

        openTestURL();
        waitForElementPresent(By.className("v-filterselect"));
        comboBoxElement = $(ComboBoxElement.class).first();
    }

    @Test
    public void ensureOldFilterIsCleared() {
        comboBoxElement.openPopup();
        int initialVisibleOptions = countVisibleOptions();
        clearInputAndType("b11");
        int visibleOptionsAfterFiltering = countVisibleOptions();
        assertEquals(1, visibleOptionsAfterFiltering);
        clickOnLabel();
        sleep(1000);
        // no selection was made, clicking on arrow should show all options
        // again
        comboBoxElement.openPopup();

        int visibleOptions = countVisibleOptions();
        assertEquals(initialVisibleOptions, visibleOptions);
    }

    private int countVisibleOptions() {
        return comboBoxElement.getPopupSuggestions().size();
    }

    private void clickOnLabel() {
        getDriver().findElement(By.cssSelector(".v-label")).click();
    }

    @Test
    public void firstSuggestionIsSelectedWithEnter() {
        typeInputAndHitEnter("a");

        assertThatSelectedValueIs("a0");
    }

    @Test
    public void firstSuggestionIsSelectedWithTab() {
        typeInputAndHitTab("a");

        assertThatSelectedValueIs("a0");
    }

    @Test
    public void nullIsSelected() {
        typeInputAndHitEnter("a");
        assertThatSelectedValueIs("a0");

        clearInputAndHitEnter();

        assertThatSelectedValueIs("", "null");
    }

    @Test
    public void itemFromSecondPageIsSelected() {
        typeInputAndHitEnter("a20");

        assertThatSelectedValueIs("a20");
    }

    @Test
    public void selectingNullFromSecondPage() {
        typeInputAndHitEnter("a20");
        assertThatSelectedValueIs("a20");

        clearInputAndHitEnter();
        assertThatSelectedValueIs("", "null");
    }

    @Test
    public void selectionRemainsAfterOpeningPopup() {
        typeInputAndHitEnter("a20");
        assertThatSelectedValueIs("a20");

        openPopup();
        assertThatSelectedValueIs("a20");
    }

    @Test
    public void noSelectionAfterMouseOut() {
        typeInputAndHitEnter("a20");
        comboBoxElement.sendKeys(Keys.ARROW_DOWN, Keys.ARROW_DOWN);

        findElement(By.className("v-app")).click();

        assertThatSelectedValueIs("a20");
    }

    @Test
    public void cancelResetsSelection() {
        sendKeysToInput("a20");
        cancelSelection();

        assertThatSelectedValueIs("");
    }

    @Test
    public void inputFieldResetsToSelectedText() {
        typeInputAndHitEnter("z5");

        sendKeysToInput(Keys.BACK_SPACE, Keys.BACK_SPACE);
        cancelSelection();

        assertThatSelectedValueIs("z5");
    }

    @Test
    public void emptyValueIsSelectedWithTab() {
        typeInputAndHitEnter("z5");

        assertThatSelectedValueIs("z5");
        // longer delay for this one because otherwise it keeps failing when run
        // on local machine
        int delay = 200;
        if (BrowserUtil.isPhantomJS(getDesiredCapabilities())) {
            delay = 500;
        }
        comboBoxElement.sendKeys(delay, Keys.BACK_SPACE, Keys.BACK_SPACE,
                Keys.TAB);
        assertThatSelectedValueIs("", "null");

        sendKeysToInput("z5");
        cancelSelection();
        assertThatSelectedValueIs("", "null");
    }

    @Test
    public void arrowNavigatedValueIsSelectedWithEnter() {
        sendKeysToInput("z");
        sendKeysToInput(Keys.DOWN, Keys.DOWN, getReturn());

        assertThatSelectedValueIs("z2");
    }

    @Test
    public void arrowNavigatedValueIsSelectedWithTab() {
        sendKeysToInput("z");
        sendKeysToInput(Keys.DOWN, Keys.DOWN, Keys.TAB);

        assertThatSelectedValueIs("z2");
    }

    private void clearInputAndHitEnter() {
        sendKeysToInput(Keys.BACK_SPACE, Keys.BACK_SPACE, Keys.BACK_SPACE);
        sendKeysToInput(getReturn());
    }

    private void typeInputAndHitEnter(String input) {
        clearInputAndType(input);
        sendKeysToInput(getReturn());
    }

    private void typeInputAndHitTab(String input) {
        clearInputAndType(input);
        sendKeysToInput(Keys.TAB);
    }

    private void clearInputAndType(String input) {
        comboBoxElement.clear();
        sendKeysToInput(input);
    }

    private void sendKeysToInput(CharSequence... keys) {
        comboBoxElement.sendKeys(keys);
    }

    private Keys getReturn() {
        if (BrowserUtil.isPhantomJS(getDesiredCapabilities())) {
            return Keys.ENTER;
        } else {
            return Keys.RETURN;
        }
    }

    private void openPopup() {
        // Need to wait to make sure popup is closed first.
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        comboBoxElement.openPopup();
    }

    private void cancelSelection() {
        if (BrowserUtil.isFirefox(getDesiredCapabilities())) {
            findElement(By.className("v-app")).click();
        } else {
            sendKeysToInput(Keys.ESCAPE);
        }
    }

    private void assertThatSelectedValueIs(final String value) {
        assertThatSelectedValueIs(value, value);
    }

    private void assertThatSelectedValueIs(final String value,
            final String labelValue) {
        assertThat(comboBoxElement.getText(), is(value));

        waitUntil(new ExpectedCondition<Boolean>() {
            private String actualValue;

            @Override
            public Boolean apply(WebDriver input) {
                actualValue = $(LabelElement.class).id("value").getText();
                return actualValue.equals(labelValue);
            }

            @Override
            public String toString() {
                // Timed out after 10 seconds waiting for ...
                return String.format("label value to match '%s' (was: '%s')",
                        labelValue, actualValue);
            }
        });
    }
}