您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ComboBoxSelectingNewItemValueChangeTest.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package com.vaadin.tests.components.combobox;
  2. import static org.junit.Assert.assertEquals;
  3. import org.junit.Test;
  4. import org.openqa.selenium.Keys;
  5. import org.openqa.selenium.WebDriver;
  6. import org.openqa.selenium.interactions.Actions;
  7. import org.openqa.selenium.support.ui.ExpectedCondition;
  8. import com.vaadin.testbench.By;
  9. import com.vaadin.testbench.elements.ButtonElement;
  10. import com.vaadin.testbench.elements.CheckBoxElement;
  11. import com.vaadin.testbench.elements.ComboBoxElement;
  12. import com.vaadin.testbench.elements.LabelElement;
  13. import com.vaadin.testbench.parallel.BrowserUtil;
  14. import com.vaadin.tests.tb3.MultiBrowserTest;
  15. public class ComboBoxSelectingNewItemValueChangeTest extends MultiBrowserTest {
  16. private enum SelectionType {
  17. ENTER, TAB, CLICK_OUT;
  18. }
  19. private ComboBoxElement comboBoxElement;
  20. private LabelElement valueLabelElement;
  21. private LabelElement changeLabelElement;
  22. private String[] defaultInputs = new String[] { "foo", "bar", "baz",
  23. "fie" };
  24. private String[] shortInputs = new String[] { "a", "b", "c", "d" };
  25. @Override
  26. public void setup() throws Exception {
  27. super.setup();
  28. openTestURL();
  29. waitForElementPresent(By.className("v-filterselect"));
  30. comboBoxElement = $(ComboBoxElement.class).first();
  31. valueLabelElement = $(LabelElement.class).id("value");
  32. changeLabelElement = $(LabelElement.class).id("change");
  33. }
  34. @Test
  35. public void newItemHandlingWithEnter() {
  36. itemHandling(SelectionType.ENTER, defaultInputs);
  37. }
  38. @Test
  39. public void newItemHandlingWithTab() {
  40. itemHandling(SelectionType.TAB, defaultInputs);
  41. }
  42. @Test
  43. public void newItemHandlingWithClickingOut() {
  44. itemHandling(SelectionType.CLICK_OUT, defaultInputs);
  45. }
  46. @Test
  47. public void slowNewItemHandlingWithEnter() {
  48. delay(true);
  49. itemHandling(SelectionType.ENTER, defaultInputs);
  50. }
  51. @Test
  52. public void slowNewItemHandlingWithTab() {
  53. delay(true);
  54. itemHandling(SelectionType.TAB, defaultInputs);
  55. }
  56. @Test
  57. public void slowNewItemHandlingWithClickingOut() {
  58. delay(true);
  59. itemHandling(SelectionType.CLICK_OUT, defaultInputs);
  60. }
  61. @Test
  62. public void shortNewItemHandlingWithEnter() {
  63. itemHandling(SelectionType.ENTER, shortInputs);
  64. }
  65. @Test
  66. public void shortNewItemHandlingWithTab() {
  67. itemHandling(SelectionType.TAB, shortInputs);
  68. }
  69. @Test
  70. public void shortNewItemHandlingWithClickingOut() {
  71. itemHandling(SelectionType.CLICK_OUT, shortInputs);
  72. }
  73. public void itemHandling(SelectionType selectionType, String[] inputs) {
  74. assertThatSelectedValueIs("");
  75. // new item, no existing selection
  76. typeInputAndSelect(inputs[0], selectionType);
  77. assertThatSelectedValueIs(inputs[0]);
  78. assertValueChange(1);
  79. // new item, existing selection
  80. typeInputAndSelect(inputs[1], selectionType);
  81. assertThatSelectedValueIs(inputs[1]);
  82. assertValueChange(2);
  83. reject(true);
  84. // item adding blocked, existing selection
  85. typeInputAndSelect(inputs[2], selectionType);
  86. assertThatSelectedValueIs(inputs[1]);
  87. assertRejected(inputs[2]);
  88. reset();
  89. // item adding blocked, no existing selection
  90. typeInputAndSelect(inputs[2], selectionType);
  91. assertThatSelectedValueIs("");
  92. assertRejected(inputs[2]);
  93. reject(false);
  94. blockSelection(true);
  95. // item adding allowed, selection blocked, no existing selection
  96. typeInputAndSelect(inputs[2], selectionType);
  97. assertThatSelectedValueIs("");
  98. assertItemCount(2601);
  99. // second attempt selects
  100. typeInputAndSelect(inputs[2], selectionType);
  101. assertThatSelectedValueIs(inputs[2]);
  102. assertValueChange(1);
  103. // item adding allowed, selection blocked, existing selection
  104. typeInputAndSelect(inputs[3], selectionType);
  105. assertThatSelectedValueIs(inputs[2]);
  106. assertItemCount(2602);
  107. }
  108. private void typeInputAndSelect(String input, SelectionType selectionType) {
  109. comboBoxElement.clear();
  110. sendKeysToInput(input);
  111. switch (selectionType) {
  112. case ENTER:
  113. sendKeysToInput(getReturn());
  114. break;
  115. case TAB:
  116. sendKeysToInput(Keys.TAB);
  117. break;
  118. case CLICK_OUT:
  119. new Actions(getDriver()).moveToElement(comboBoxElement, 10, 10)
  120. .moveByOffset(comboBoxElement.getSize().getWidth(), 0)
  121. .click().perform();
  122. break;
  123. }
  124. }
  125. private void sendKeysToInput(CharSequence... keys) {
  126. // ensure mouse is located over the ComboBox to avoid hover issues
  127. new Actions(getDriver()).moveToElement(comboBoxElement).perform();
  128. comboBoxElement.sendKeys(keys);
  129. }
  130. private Keys getReturn() {
  131. if (BrowserUtil.isPhantomJS(getDesiredCapabilities())) {
  132. return Keys.ENTER;
  133. } else {
  134. return Keys.RETURN;
  135. }
  136. }
  137. private void assertThatSelectedValueIs(final String value) {
  138. waitUntil(new ExpectedCondition<Boolean>() {
  139. private String actualComboBoxValue;
  140. private String actualLabelValue;
  141. @Override
  142. public Boolean apply(WebDriver input) {
  143. actualLabelValue = valueLabelElement.getText();
  144. actualComboBoxValue = comboBoxElement.getText();
  145. return actualComboBoxValue.equals(value)
  146. && actualLabelValue.equals(value);
  147. }
  148. @Override
  149. public String toString() {
  150. // Timed out after 10 seconds waiting for ...
  151. return String.format(
  152. "combobox and label value to match '%s' (was: '%s' and '%s')",
  153. value, actualComboBoxValue, actualLabelValue);
  154. }
  155. });
  156. }
  157. private void assertValueChange(int count) {
  158. assertEquals(String.format(
  159. "Value change count: %s Selection change count: %s user originated: true",
  160. count, count), changeLabelElement.getText());
  161. }
  162. private void assertRejected(String value) {
  163. assertEquals(String.format("item %s discarded", value),
  164. changeLabelElement.getText());
  165. }
  166. private void assertItemCount(int count) {
  167. assertEquals(String.format("adding new item... count: %s", count),
  168. changeLabelElement.getText());
  169. }
  170. private void reject(boolean reject) {
  171. CheckBoxElement checkBox = $(CheckBoxElement.class).id("reject");
  172. if (reject != checkBox.isChecked()) {
  173. checkBox.click();
  174. }
  175. }
  176. private void delay(boolean delay) {
  177. CheckBoxElement checkBox = $(CheckBoxElement.class).id("delay");
  178. if (delay != checkBox.isChecked()) {
  179. checkBox.click();
  180. }
  181. }
  182. private void blockSelection(boolean noSelection) {
  183. CheckBoxElement checkBox = $(CheckBoxElement.class).id("noSelection");
  184. if (noSelection != checkBox.isChecked()) {
  185. checkBox.click();
  186. }
  187. }
  188. private void reset() {
  189. $(ButtonElement.class).id("reset").click();
  190. }
  191. }