You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestBenchElementClearValueTest.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.elements;
  17. import java.time.format.DateTimeFormatter;
  18. import org.junit.Assert;
  19. import org.junit.Before;
  20. import org.junit.Test;
  21. import com.vaadin.testbench.elements.AbstractTextFieldElement;
  22. import com.vaadin.testbench.elements.CheckBoxElement;
  23. import com.vaadin.testbench.elements.CheckBoxGroupElement;
  24. import com.vaadin.testbench.elements.ComboBoxElement;
  25. import com.vaadin.testbench.elements.DateFieldElement;
  26. import com.vaadin.testbench.elements.LabelElement;
  27. import com.vaadin.testbench.elements.ListSelectElement;
  28. import com.vaadin.testbench.elements.NativeSelectElement;
  29. import com.vaadin.testbench.elements.PasswordFieldElement;
  30. import com.vaadin.testbench.elements.RadioButtonGroupElement;
  31. import com.vaadin.testbench.elements.TextAreaElement;
  32. import com.vaadin.testbench.elements.TextFieldElement;
  33. import com.vaadin.testbench.elements.TwinColSelectElement;
  34. import com.vaadin.tests.tb3.MultiBrowserTest;
  35. /**
  36. * Test clear method. Checks that value of the component was changed both on
  37. * client and server side Testing of the client side done by comparing first
  38. * with initial value then calling clear and comparing with empty value. Testing
  39. * of the server side by checking that changeValue even was raised on the server
  40. * side. Each element has changeValue listener added in the UI class. Compare
  41. * labelChangeValue value with the value used in the listener of the UI class.
  42. *
  43. * @since
  44. * @author Vaadin Ltd
  45. */
  46. public class TestBenchElementClearValueTest extends MultiBrowserTest {
  47. // The label text is changed on element component ValueChange event
  48. // Used to test that element.clear() method has actually triggered the
  49. // server side code
  50. private LabelElement labelChangeValue;
  51. // use same TestUI class as for getValue method
  52. @Override
  53. protected Class<?> getUIClass() {
  54. return ComponentElementGetValue.class;
  55. }
  56. @Before
  57. public void init() {
  58. openTestURL();
  59. labelChangeValue = $(LabelElement.class).get(1);
  60. }
  61. @Test
  62. public void clearTextField() {
  63. TextFieldElement elem = $(TextFieldElement.class).get(0);
  64. checkElementValue(elem);
  65. Assert.assertEquals(ComponentElementGetValue.FIELD_VALUES[0],
  66. labelChangeValue.getText());
  67. }
  68. @Test
  69. public void clearTextArea() {
  70. TextAreaElement elem = $(TextAreaElement.class).get(0);
  71. checkElementValue(elem);
  72. Assert.assertEquals(ComponentElementGetValue.FIELD_VALUES[1],
  73. labelChangeValue.getText());
  74. }
  75. @Test
  76. public void clearPasswordField() {
  77. PasswordFieldElement elem = $(PasswordFieldElement.class).get(0);
  78. checkElementValue(elem);
  79. Assert.assertEquals(ComponentElementGetValue.FIELD_VALUES[2],
  80. labelChangeValue.getText());
  81. }
  82. @Test
  83. public void clearDateField() {
  84. DateFieldElement df = $(DateFieldElement.class).get(0);
  85. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  86. String expected = formatter
  87. .format(ComponentElementGetValue.TEST_DATE_VALUE);
  88. String initial = df.getValue();
  89. Assert.assertEquals(expected, initial);
  90. df.clear();
  91. Assert.assertEquals("", df.getValue());
  92. Assert.assertEquals(ComponentElementGetValue.DATEFIELD_VALUE_CHANGE,
  93. labelChangeValue.getText());
  94. }
  95. // Clear method should do not raise exception
  96. public void clearComboBox() {
  97. ComboBoxElement elem = $(ComboBoxElement.class).get(0);
  98. elem.clear();
  99. }
  100. public void clearNativeSelect() {
  101. NativeSelectElement elem = $(NativeSelectElement.class).get(0);
  102. elem.clear();
  103. }
  104. public void clearListSelect() {
  105. ListSelectElement elem = $(ListSelectElement.class).get(0);
  106. elem.clear();
  107. }
  108. public void clearCheckBoxGroup() {
  109. CheckBoxGroupElement elem = $(CheckBoxGroupElement.class).get(0);
  110. elem.clear();
  111. }
  112. public void clearRadioButtonGroup() {
  113. RadioButtonGroupElement elem = $(RadioButtonGroupElement.class).get(0);
  114. elem.clear();
  115. }
  116. @Test
  117. public void clearCheckBox() {
  118. CheckBoxElement elem = $(CheckBoxElement.class).get(0);
  119. elem.clear();
  120. Assert.assertTrue(elem.getValue().equals("unchecked"));
  121. Assert.assertEquals(ComponentElementGetValue.CHECKBOX_VALUE_CHANGE,
  122. labelChangeValue.getText());
  123. }
  124. @Test
  125. public void clearTwinCol() {
  126. TwinColSelectElement elem = $(TwinColSelectElement.class).get(0);
  127. elem.clear();
  128. Assert.assertEquals("", elem.getValue());
  129. Assert.assertEquals(ComponentElementGetValue.MULTI_SELECT_VALUE_CHANGE,
  130. labelChangeValue.getText());
  131. }
  132. // helper functions
  133. private void checkElementValue(AbstractTextFieldElement elem) {
  134. String initial = ComponentElementGetValue.TEST_STRING_VALUE;
  135. checkElementValue(elem, initial);
  136. }
  137. private void checkElementValue(AbstractTextFieldElement elem,
  138. String expected) {
  139. // check initial element value
  140. String actual = elem.getValue();
  141. Assert.assertEquals(expected, actual);
  142. // check cleared element value
  143. elem.clear();
  144. expected = "";
  145. actual = elem.getValue();
  146. Assert.assertEquals(expected, actual);
  147. }
  148. }