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.

BasicPersonFormTest.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.fieldgroup;
  17. import org.junit.Assert;
  18. import com.vaadin.testbench.TestBenchElement;
  19. import com.vaadin.testbench.elements.ButtonElement;
  20. import com.vaadin.testbench.elements.CheckBoxElement;
  21. import com.vaadin.testbench.elements.NotificationElement;
  22. import com.vaadin.testbench.elements.TableElement;
  23. import com.vaadin.testbench.elements.TableRowElement;
  24. import com.vaadin.testbench.elements.TextAreaElement;
  25. import com.vaadin.testbench.elements.TextFieldElement;
  26. import com.vaadin.tests.data.bean.Sex;
  27. import com.vaadin.tests.tb3.MultiBrowserTest;
  28. public abstract class BasicPersonFormTest extends MultiBrowserTest {
  29. private static final String BEAN_VALUES = "Person [firstName=John, lastName=Doe, email=john@doe.com, age=64, sex=Male, address=Address [streetAddress=John street, postalCode=11223, city=John's town, country=USA], deceased=false, salary=null, salaryDouble=null, rent=null]";
  30. private int logCounter;
  31. @Override
  32. public void setup() throws Exception {
  33. super.setup();
  34. logCounter = 0;
  35. }
  36. @Override
  37. protected Class<?> getUIClass() {
  38. return BasicPersonForm.class;
  39. }
  40. protected TextFieldElement getFirstNameField() {
  41. return $(TextFieldElement.class).caption("First Name").first();
  42. }
  43. protected TextAreaElement getLastNameArea() {
  44. return $(TextAreaElement.class).caption("Last Name").first();
  45. }
  46. protected TextFieldElement getEmailField() {
  47. return $(TextFieldElement.class).caption("Email").first();
  48. }
  49. protected TextFieldElement getAgeField() {
  50. return $(TextFieldElement.class).caption("Age").first();
  51. }
  52. protected TableElement getGenderTable() {
  53. return $(TableElement.class).caption("Sex").first();
  54. }
  55. protected TextFieldElement getDeceasedField() {
  56. return $(TextFieldElement.class).caption("Deceased").first();
  57. }
  58. protected void showBeanValues() {
  59. $(ButtonElement.class).caption("Show bean values").first().click();
  60. }
  61. protected CheckBoxElement getPreCommitFailsCheckBox() {
  62. return $(CheckBoxElement.class).get(1);
  63. }
  64. protected void commitChanges() {
  65. $(ButtonElement.class).caption("Commit").first().click();
  66. }
  67. protected void closeNotification() {
  68. $(NotificationElement.class).first().close();
  69. }
  70. protected CheckBoxElement getPostCommitFailsCheckBox() {
  71. return $(CheckBoxElement.class).get(0);
  72. }
  73. protected void discardChanges() {
  74. $(ButtonElement.class).caption("Discard").first().click();
  75. }
  76. protected void assertFirstNameValue(String expected) {
  77. assertFieldValue("First Name", expected, getFirstNameField());
  78. }
  79. protected void assertLastNameValue(String expected) {
  80. assertFieldValue("Last Name", expected, getLastNameArea());
  81. }
  82. protected void assertEmailValue(String expected) {
  83. assertFieldValue("Email", expected, getEmailField());
  84. }
  85. protected void assertAgeValue(String expected) {
  86. assertFieldValue("Age", expected, getAgeField());
  87. }
  88. protected void assertDeceasedValue(String expected) {
  89. assertFieldValue("Deceased", expected, getDeceasedField());
  90. }
  91. private void assertFieldValue(String caption, String expected,
  92. TestBenchElement field) {
  93. Assert.assertEquals(
  94. String.format("Unexpected value for field '%s',", caption),
  95. expected, field.getAttribute("value"));
  96. }
  97. protected void assertSelectedSex(Sex sex) {
  98. TableRowElement row = getGenderTable().getRow(getIndex(sex));
  99. Assert.assertTrue(
  100. String.format("Given sex (%s) isn't selected.",
  101. sex.getStringRepresentation()),
  102. hasCssClass(row, "v-selected"));
  103. }
  104. private int getIndex(Sex sex) {
  105. switch (sex) {
  106. case MALE:
  107. return 0;
  108. case FEMALE:
  109. return 1;
  110. default:
  111. return 2;
  112. }
  113. }
  114. protected void assertBeanValuesUnchanged() {
  115. showBeanValues();
  116. assertLogText(BEAN_VALUES);
  117. }
  118. protected void assertCommitFails() {
  119. commitChanges();
  120. closeNotification();
  121. assertLogText("Commit failed: Commit failed");
  122. }
  123. protected void assertCommitSuccessful() {
  124. commitChanges();
  125. closeNotification();
  126. assertLogText("Commit succesful");
  127. }
  128. protected void assertDiscardResetsFields() {
  129. discardChanges();
  130. assertLogText("Discarded changes");
  131. assertDefaults();
  132. }
  133. protected void assertLogText(String expected) {
  134. ++logCounter;
  135. Assert.assertEquals("Unexpected log contents,", logCounter + ". "
  136. + expected, getLogRow(0));
  137. }
  138. protected void assertDefaults() {
  139. assertFirstNameValue("John");
  140. assertLastNameValue("Doe");
  141. assertEmailValue("john@doe.com");
  142. assertAgeValue("64");
  143. assertSelectedSex(Sex.MALE);
  144. assertDeceasedValue("NAAAAAH");
  145. }
  146. }