]> source.dussan.org Git - vaadin-framework.git/commitdiff
Make ComboBoxValueInputTest stable by simplifying the steps.(#3268)
authorSauli Tähkäpää <sauli@vaadin.com>
Thu, 11 Sep 2014 12:06:23 +0000 (15:06 +0300)
committerVaadin Code Review <review@vaadin.com>
Fri, 19 Sep 2014 05:37:41 +0000 (05:37 +0000)
Change-Id: Idd314e531659fbab701df117307b5c41fcac9ec3

uitest/src/com/vaadin/tests/components/combobox/ComboBoxValueInput.java
uitest/src/com/vaadin/tests/components/combobox/ComboBoxValueInputTest.java

index b0c1acb421e0b79895e1f0ff01037d1cd57cf546..1d2c85c9748c84a60b2157338a25963392da61f9 100644 (file)
@@ -11,30 +11,23 @@ public class ComboBoxValueInput extends AbstractTestUI {
     protected void setup(VaadinRequest request) {
         (getLayout()).setSpacing(true);
 
-        ComboBox cb = getComboBox("A combobox", false);
+        ComboBox cb = getComboBox("A combobox", false, "default");
         addComponent(cb);
 
-        cb = getComboBox("A combobox with input prompt", false);
+        cb = getComboBox("A combobox with input prompt", false, "default-prompt");
         cb.setInputPrompt("Please select");
         addComponent(cb);
 
-        cb = getComboBox("A combobox with null item", true);
+        cb = getComboBox("A combobox with null item", true, "null");
         addComponent(cb);
 
-        cb = getComboBox("A combobox with null item and input prompt", true);
+        cb = getComboBox("A combobox with null item and input prompt", true, "null-prompt");
         cb.setInputPrompt("Please select");
         addComponent(cb);
 
-        cb = getComboBox("A disabled combobox", true);
-        cb.setEnabled(false);
-        addComponent(cb);
-
-        cb = getComboBox("A read-only combobox", true);
-        cb.setReadOnly(true);
-        addComponent(cb);
-
-        cb = getComboBox("A combobox with filteringMode off", false);
+        cb = getComboBox("A combobox with filteringMode off", false, "filtering-off");
         cb.setFilteringMode(FilteringMode.OFF);
+        addComponent(cb);
 
     }
 
@@ -48,7 +41,7 @@ public class ComboBoxValueInput extends AbstractTestUI {
         return 3268;
     }
 
-    private ComboBox getComboBox(String caption, boolean addNullItem) {
+    private ComboBox getComboBox(String caption, boolean addNullItem, String id) {
         ComboBox cb = new ComboBox(caption);
         cb.setImmediate(true);
         if (addNullItem) {
@@ -59,6 +52,8 @@ public class ComboBoxValueInput extends AbstractTestUI {
         cb.addItem("Value 2");
         cb.addItem("Value 3");
 
+        cb.setId(id);
+
         return cb;
     }
 
index 74879f66e87d56bdb77364500bf28c1afe233a78..b2b3c29098dd2378dc19113ff7515ba48393373a 100644 (file)
  */
 package com.vaadin.tests.components.combobox;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-import java.util.List;
-
+import com.vaadin.testbench.By;
+import com.vaadin.testbench.elements.ComboBoxElement;
+import com.vaadin.testbench.elements.VerticalLayoutElement;
+import com.vaadin.tests.tb3.MultiBrowserTest;
 import org.junit.Before;
 import org.junit.Test;
-import org.openqa.selenium.Keys;
 import org.openqa.selenium.WebElement;
 
-import com.vaadin.testbench.By;
-import com.vaadin.testbench.elements.ComboBoxElement;
-import com.vaadin.tests.tb3.MultiBrowserTest;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
 
 /**
  * Tests ComboBox inputs and selections.
@@ -44,349 +42,105 @@ public class ComboBoxValueInputTest extends MultiBrowserTest {
         openTestURL();
     };
 
-    @Test
-    public void testOverall() {
-        List<ComboBoxElement> comboBoxes = $(ComboBoxElement.class).all();
-        assertEquals("unexpected amount of comboboxes found", 6,
-                comboBoxes.size());
+    private void sendKeysToComboBox(ComboBoxElement comboBox, CharSequence keys) {
+        getComboBoxInput(comboBox).sendKeys(keys);
     }
 
-    @Test
-    public void testFirstComboBoxInputs() {
-        ComboBoxElement comboBox = $(ComboBoxElement.class).all().get(0);
-
-        // check the initial selection
-        assertEquals("correct selection not found", "",
-                comboBox.findElement(By.tagName("input")).getAttribute("value"));
-
-        // ensure the combobox has suggested values
-        assertFalse("suggestions not found even if there should be several",
-                comboBox.getPopupSuggestions().isEmpty());
-
-        // check that the found items were correct ones
-        WebElement popup = driver.findElement(By
-                .className("v-filterselect-suggestpopup"));
-        List<WebElement> filteredItems = popup.findElement(
-                By.className("v-filterselect-suggestmenu")).findElements(
-                By.className("gwt-MenuItem"));
-        assertEquals("unexpected amount of suggestions found", 4,
-                filteredItems.size());
-        assertEquals("wrong filtering result", " ", filteredItems.get(0)
-                .getText());
-        assertEquals("wrong filtering result", "Value 1", filteredItems.get(1)
-                .getText());
-        assertEquals("wrong filtering result", "Value 3", filteredItems.get(3)
-                .getText());
-
-        // select the second last item
-        filteredItems.get(2).click();
-
-        // ensure there's no more popup
-        assertFalse("popup found when there should be none",
-                comboBox.isElementPresent(By.vaadin("#popup")));
-
-        // check the updated selection
-        assertEquals("correct selection not found", "Value 2", comboBox
-                .findElement(By.tagName("input")).getAttribute("value"));
+    private void removeFocusFromComboBoxes() {
+        $(VerticalLayoutElement.class).first().click();
+    }
+
+    private ComboBoxElement getComboBox(String id) {
+        return $(ComboBoxElement.class).id(id);
+    }
+
+    private String getComboBoxValue(ComboBoxElement comboBox) {
+        return getComboBoxInput(comboBox).getAttribute("value");
+    }
+
+    private WebElement getComboBoxInput(ComboBoxElement comboBox) {
+        return comboBox.findElement(By.tagName("input"));
     }
 
     @Test
-    public void testSecondComboBoxInputs() {
-        ComboBoxElement comboBox = $(ComboBoxElement.class).all().get(1);
-
-        // check the input prompt
-        assertEquals("correct input prompt not found", "Please select",
-                comboBox.findElement(By.tagName("input")).getAttribute("value"));
-
-        // focus the combobox
-        comboBox.findElement(By.tagName("input")).click();
-
-        // check the input prompt has disappeared
-        assertEquals("correct input prompt not found", "", comboBox
-                .findElement(By.tagName("input")).getAttribute("value"));
-
-        // ensure the combobox has suggested values
-        assertFalse("suggestions not found even if there should be several",
-                comboBox.getPopupSuggestions().isEmpty());
-
-        // check that the found items were correct ones
-        WebElement popup = driver.findElement(By
-                .className("v-filterselect-suggestpopup"));
-        List<WebElement> filteredItems = popup.findElement(
-                By.className("v-filterselect-suggestmenu")).findElements(
-                By.className("gwt-MenuItem"));
-        assertEquals("unexpected amount of suggestions found", 4,
-                filteredItems.size());
-        assertEquals("wrong filtering result", " ", filteredItems.get(0)
-                .getText());
-        assertEquals("wrong filtering result", "Value 1", filteredItems.get(1)
-                .getText());
-        assertEquals("wrong filtering result", "Value 3", filteredItems.get(3)
-                .getText());
-
-        // select the last item
-        filteredItems.get(3).click();
-
-        // ensure there's no more popup
-        assertFalse("popup found when there should be none",
-                comboBox.isElementPresent(By.vaadin("#popup")));
-
-        // check the updated selection
-        assertEquals("correct selection not found", "Value 3", comboBox
-                .findElement(By.tagName("input")).getAttribute("value"));
+    public void defaultComboBoxClearsInputOnInvalidValue() {
+        ComboBoxElement comboBox = getComboBox("default");
+
+        assertThat(getComboBoxValue(comboBox), is(""));
+
+        comboBox.selectByText("Value 1");
+        sendKeysToComboBox(comboBox, "abc");
+
+        removeFocusFromComboBoxes();
+
+        assertThat(getComboBoxValue(comboBox), is("Value 1"));
+        assertThatComboBoxSuggestionsAreHidden(comboBox);
+    }
+
+    private void assertThatComboBoxSuggestionsAreHidden(ComboBoxElement comboBox) {
+        assertThat(comboBox.isElementPresent(By.vaadin("#popup")), is(false));
     }
 
     @Test
-    public void testThirdComboBoxInputs() {
-        ComboBoxElement comboBox = $(ComboBoxElement.class).all().get(2);
-
-        // check the null selection
-        assertEquals("correct selection not found", "Null item", comboBox
-                .findElement(By.tagName("input")).getAttribute("value"));
-
-        // ensure the combobox has suggested values
-        assertFalse("suggestions not found even if there should be several",
-                comboBox.getPopupSuggestions().isEmpty());
-
-        // check that the found items were correct ones
-        WebElement popup = driver.findElement(By
-                .className("v-filterselect-suggestpopup"));
-        List<WebElement> filteredItems = popup.findElement(
-                By.className("v-filterselect-suggestmenu")).findElements(
-                By.className("gwt-MenuItem"));
-        assertEquals("unexpected amount of suggestions found", 4,
-                filteredItems.size());
-        assertEquals("wrong filtering result", "Null item", filteredItems
-                .get(0).getText());
-        assertEquals("wrong filtering result", "Value 1", filteredItems.get(1)
-                .getText());
-        assertEquals("wrong filtering result", "Value 3", filteredItems.get(3)
-                .getText());
-
-        // ensure the null item is marked as selected
-        assertEquals("wrong selection", "Null item",
-                popup.findElement(By.className("v-filterselect-suggestmenu"))
-                        .findElement(By.className("gwt-MenuItem-selected"))
-                        .getText());
-
-        // select the second last item
-        filteredItems.get(2).click();
-
-        // ensure there's no more popup
-        assertFalse("popup found when there should be none",
-                comboBox.isElementPresent(By.vaadin("#popup")));
-
-        // check the updated selection
-        assertEquals("correct selection not found", "Value 2", comboBox
-                .findElement(By.tagName("input")).getAttribute("value"));
-
-        // open the popup again
-        comboBox.openPopup();
+    public void comboBoxWithPromptClearsInputOnInvalidValue() {
+        ComboBoxElement comboBox = getComboBox("default-prompt");
+
+        assertThat(getComboBoxValue(comboBox), is("Please select"));
+
+        comboBox.selectByText("Value 2");
+        sendKeysToComboBox(comboBox, "def");
+
+        removeFocusFromComboBoxes();
 
-        // check that the found items were correct ones
-        popup = driver.findElement(By.className("v-filterselect-suggestpopup"));
-        filteredItems = popup.findElement(
-                By.className("v-filterselect-suggestmenu")).findElements(
-                By.className("gwt-MenuItem"));
-        assertEquals("unexpected amount of suggestions found", 4,
-                filteredItems.size());
-        assertEquals("wrong filtering result", "Null item", filteredItems
-                .get(0).getText());
-        assertEquals("wrong filtering result", "Value 1", filteredItems.get(1)
-                .getText());
-        assertEquals("wrong filtering result", "Value 3", filteredItems.get(3)
-                .getText());
-
-        // ensure the selected item is marked as selected
-        assertEquals("wrong selection", "Value 2",
-                popup.findElement(By.className("v-filterselect-suggestmenu"))
-                        .findElement(By.className("gwt-MenuItem-selected"))
-                        .getText());
-
-        // select the first item again
-        filteredItems.get(0).click();
-
-        // ensure there's no more popup
-        assertFalse("popup found when there should be none",
-                comboBox.isElementPresent(By.vaadin("#popup")));
-
-        // check the displayed value didn't change
-        assertEquals("correct selection not found", "Null item", comboBox
-                .findElement(By.tagName("input")).getAttribute("value"));
-
-        // enter a new filtering value
-        comboBox.findElement(By.tagName("input")).clear();
-        comboBox.findElement(By.tagName("input")).sendKeys("value");
-
-        // ensure there's a popup again
-        assertTrue("popup not found when there should be one",
-                comboBox.isElementPresent(By.vaadin("#popup")));
-
-        // check that the found items were correct ones
-        popup = driver.findElement(By.className("v-filterselect-suggestpopup"));
-        filteredItems = popup.findElement(
-                By.className("v-filterselect-suggestmenu")).findElements(
-                By.className("gwt-MenuItem"));
-        assertEquals("unexpected amount of suggestions found", 3,
-                filteredItems.size());
-        assertEquals("wrong filtering result", "Value 1", filteredItems.get(0)
-                .getText());
-        assertEquals("wrong filtering result", "Value 3", filteredItems.get(2)
-                .getText());
-
-        // check the displayed value updated
-        assertEquals("correct selection not found", "value", comboBox
-                .findElement(By.tagName("input")).getAttribute("value"));
-
-        comboBox.findElement(By.tagName("input")).sendKeys(Keys.ARROW_DOWN);
-
-        // ensure the first item is marked as selected
-        assertEquals("wrong selection", "Value 1",
-                popup.findElement(By.className("v-filterselect-suggestmenu"))
-                        .findElement(By.className("gwt-MenuItem-selected"))
-                        .getText());
-
-        // navigate with keys to the last item
-        comboBox.findElement(By.tagName("input")).sendKeys(Keys.ARROW_DOWN);
-        comboBox.findElement(By.tagName("input")).sendKeys(Keys.ARROW_DOWN);
-
-        // TODO: remove this limit once #14402 has been fixed
-        if (getBrowsersExcludingIE().contains(getDesiredCapabilities())) {
-            // ensure the last item is marked as selected
-            assertEquals(
-                    "wrong selection",
-                    "Value 3",
-                    popup.findElement(
-                            By.className("v-filterselect-suggestmenu"))
-                            .findElement(By.className("gwt-MenuItem-selected"))
-                            .getText());
-        }
-
-        // select the last item
-        comboBox.findElement(By.tagName("input")).sendKeys(Keys.RETURN);
-
-        // TODO: remove this limit once #14402 has been fixed
-        if (getBrowsersExcludingIE().contains(getDesiredCapabilities())) {
-            // check the updated selection
-            assertEquals("correct selection not found", "Value 3", comboBox
-                    .findElement(By.tagName("input")).getAttribute("value"));
-        }
-
-        // TODO: remove this altogether once #14402 has been fixed
-        if (Browser.PHANTOMJS.getDesiredCapabilities().equals(
-                getDesiredCapabilities())) {
-            // toggle popup closed since the RETURN didn't do it
-            comboBox.openPopup();
-        }
-
-        // ensure there's no more popup
-        assertFalse("popup found when there should be none",
-                comboBox.isElementPresent(By.vaadin("#popup")));
+        assertThat(getComboBoxValue(comboBox), is("Value 2"));
+        assertThatComboBoxSuggestionsAreHidden(comboBox);
     }
 
     @Test
-    public void testFourthComboBoxInputs() {
-        ComboBoxElement comboBox = $(ComboBoxElement.class).all().get(3);
-
-        // check the combobox displays the null selection instead of the input
-        // prompt
-        assertEquals("correct value not displayed", "Null item", comboBox
-                .findElement(By.tagName("input")).getAttribute("value"));
-
-        // ensure the combobox has suggested values
-        assertFalse("suggestions not found even if there should be several",
-                comboBox.getPopupSuggestions().isEmpty());
-
-        // check that the found items were correct ones
-        WebElement popup = driver.findElement(By
-                .className("v-filterselect-suggestpopup"));
-        List<WebElement> filteredItems = popup.findElement(
-                By.className("v-filterselect-suggestmenu")).findElements(
-                By.className("gwt-MenuItem"));
-        assertEquals("unexpected amount of suggestions found", 4,
-                filteredItems.size());
-        assertEquals("wrong filtering result", "Null item", filteredItems
-                .get(0).getText());
-        assertEquals("wrong filtering result", "Value 1", filteredItems.get(1)
-                .getText());
-        assertEquals("wrong filtering result", "Value 3", filteredItems.get(3)
-                .getText());
-
-        // ensure the null item is marked as selected
-        assertEquals("wrong selection", "Null item",
-                popup.findElement(By.className("v-filterselect-suggestmenu"))
-                        .findElement(By.className("gwt-MenuItem-selected"))
-                        .getText());
-
-        // select the second item
-        filteredItems.get(1).click();
+    public void comboBoxWithNullItemClearsInputOnInvalidValue() {
+        ComboBoxElement comboBox = getComboBox("null");
 
-        // ensure there's no more popup
-        assertFalse("popup found when there should be none",
-                comboBox.isElementPresent(By.vaadin("#popup")));
+        assertThat(getComboBoxValue(comboBox), is("Null item"));
 
-        // check the updated selection
-        assertEquals("correct selection not found", "Value 1", comboBox
-                .findElement(By.tagName("input")).getAttribute("value"));
+        sendKeysToComboBox(comboBox, "ghi");
 
-        // open the popup again
-        comboBox.openPopup();
-        waitForElementPresent(By.className("v-filterselect-suggestmenu"));
-        popup = driver.findElement(By.className("v-filterselect-suggestpopup"));
-
-        // ensure the full selection remains
-        filteredItems = popup.findElement(
-                By.className("v-filterselect-suggestmenu")).findElements(
-                By.className("gwt-MenuItem"));
-        assertEquals("unexpected amount of suggestions found", 4,
-                filteredItems.size());
-
-        // ensure the selected item is marked as selected
-        assertEquals("wrong selection", "Value 1",
-                popup.findElement(By.className("v-filterselect-suggestmenu"))
-                        .findElement(By.className("gwt-MenuItem-selected"))
-                        .getText());
-
-        // select the null selection item
-        filteredItems.get(0).click();
-
-        // ensure there's no more popup
-        assertFalse("popup found when there should be none",
-                comboBox.isElementPresent(By.vaadin("#popup")));
+        removeFocusFromComboBoxes();
+
+        assertThat(getComboBoxValue(comboBox), is("Null item"));
+        assertThatComboBoxSuggestionsAreHidden(comboBox);
     }
 
     @Test
-    public void testFifthComboBoxInputs() {
-        ComboBoxElement comboBox = $(ComboBoxElement.class).all().get(4);
+    public void comboBoxWithNullItemAndPromptClearsInputOnInvalidValue() {
+        ComboBoxElement comboBox = getComboBox("null-prompt");
 
-        // check the initial selection
-        assertEquals("correct selection not found", "Null item", comboBox
-                .findElement(By.tagName("input")).getAttribute("value"));
+        assertThat(getComboBoxValue(comboBox), is("Null item"));
 
-        // check the disabled status
-        assertEquals("correct selection not found", "true", comboBox
-                .findElement(By.tagName("input")).getAttribute("disabled"));
+        sendKeysToComboBox(comboBox, "jkl");
 
-        comboBox.openPopup();
+        removeFocusFromComboBoxes();
+
+        assertThat(getComboBoxValue(comboBox), is("Null item"));
+        assertThatComboBoxSuggestionsAreHidden(comboBox);
 
-        // ensure there's no popup
-        assertFalse("popup found when there should be none",
-                comboBox.isElementPresent(By.vaadin("#popup")));
     }
 
     @Test
-    public void testSixthComboBoxInputs() {
-        ComboBoxElement comboBox = $(ComboBoxElement.class).all().get(5);
+    public void comboBoxWithFilteringOffClearsInputOnInvalidValue() {
+        ComboBoxElement comboBox = getComboBox("filtering-off");
+
+        assertThat(getComboBoxValue(comboBox), is(""));
+
+        //selectByText doesn't work when filtering is off.
+        comboBox.openPopup();
+        List<WebElement> filteredItems = findElements(By.className("gwt-MenuItem"));
+        filteredItems.get(1).click();
 
-        // check the initial selection
-        assertEquals("correct selection not found", "Null item", comboBox
-                .findElement(By.tagName("input")).getAttribute("value"));
+        sendKeysToComboBox(comboBox, "mnop");
 
-        // check the readonly status
-        assertEquals("correct selection not found", "true", comboBox
-                .findElement(By.tagName("input")).getAttribute("readonly"));
+        removeFocusFromComboBoxes();
 
-        assertFalse("button visible on readonly combobox", comboBox
-                .findElement(By.vaadin("#button")).isDisplayed());
+        assertThat(getComboBoxValue(comboBox), is("Value 1"));
+        assertThatComboBoxSuggestionsAreHidden(comboBox);
     }
 }