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.

VaadinCDISmokeIT.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package com.vaadin.test.cdi;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertTrue;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.stream.Collectors;
  7. import org.junit.Rule;
  8. import org.junit.Test;
  9. import org.junit.runner.RunWith;
  10. import com.vaadin.test.cdi.ui.RootPathUI;
  11. import com.vaadin.test.cdi.views.GreetingService;
  12. import com.vaadin.test.cdi.views.GreetingView;
  13. import com.vaadin.testbench.ScreenshotOnFailureRule;
  14. import com.vaadin.testbench.annotations.RunLocally;
  15. import com.vaadin.testbench.elements.ButtonElement;
  16. import com.vaadin.testbench.elements.CssLayoutElement;
  17. import com.vaadin.testbench.elements.LabelElement;
  18. import com.vaadin.testbench.elements.NotificationElement;
  19. import com.vaadin.testbench.parallel.Browser;
  20. import com.vaadin.testbench.parallel.ParallelRunner;
  21. import com.vaadin.testbench.parallel.ParallelTest;
  22. @RunWith(ParallelRunner.class)
  23. @RunLocally(Browser.PHANTOMJS)
  24. public class VaadinCDISmokeIT extends ParallelTest {
  25. private static final String BASE_URL = "http://localhost:8080/";
  26. @Rule
  27. public ScreenshotOnFailureRule rule = new ScreenshotOnFailureRule(this,
  28. true);
  29. @Override
  30. public void setup() throws Exception {
  31. super.setup();
  32. testBench().resizeViewPortTo(1024, 600);
  33. }
  34. @Test
  35. public void testPageLoadsAndCanBeInterractedWith() {
  36. getDriver().navigate().to(BASE_URL);
  37. $(ButtonElement.class).caption("Click Me").first().click();
  38. assertTrue($(NotificationElement.class).exists());
  39. assertEquals(ThankYouServiceImpl.THANK_YOU_TEXT,
  40. $(NotificationElement.class).first().getText());
  41. }
  42. @Test
  43. public void testAlwaysNewViewIsRecrated() {
  44. getDriver().navigate().to(BASE_URL);
  45. navigateAndGetLogContent("new");
  46. // A new navigation event should happen when navigating to view again
  47. navigateAndGetLogContent("new");
  48. assertEquals("GreetingService should've been only called once.",
  49. String.format(GreetingView.CALL_COUNT_FORMAT, 1),
  50. $(LabelElement.class).id("callCount").getText());
  51. }
  52. @Test
  53. public void testParameterChangeRecreatedView() {
  54. GreetingService service = new GreetingService();
  55. getDriver().navigate().to(BASE_URL);
  56. navigateAndGetLogContent("param");
  57. assertEquals("Greeting service was not called with empty parameter.",
  58. service.getGreeting(""), $(CssLayoutElement.class).id("log")
  59. .$(LabelElement.class).first().getText());
  60. // Navigation event is fired with same view and different parameters
  61. navigateAndGetLogContent("param", "foo");
  62. assertEquals("Greeting service was not called with correct parameters.",
  63. service.getGreeting("foo"), $(CssLayoutElement.class).id("log")
  64. .$(LabelElement.class).first().getText());
  65. assertEquals("GreetingService should've been only called once.",
  66. String.format(GreetingView.CALL_COUNT_FORMAT, 1),
  67. $(LabelElement.class).id("callCount").getText());
  68. }
  69. @Test
  70. public void testParameterChangeUsesSameView() {
  71. GreetingService service = new GreetingService();
  72. getDriver().navigate().to(BASE_URL);
  73. navigateAndGetLogContent("name", "foo");
  74. assertEquals("Greeting service was not called with 'foo' parameter.",
  75. service.getGreeting("foo"), $(CssLayoutElement.class).id("log")
  76. .$(LabelElement.class).first().getText());
  77. // Navigation event fired with same view and different parameters
  78. navigateAndGetLogContent("name", "bar");
  79. assertEquals("GreetingService should've been only called twice.",
  80. String.format(GreetingView.CALL_COUNT_FORMAT, 2),
  81. $(LabelElement.class).id("callCount").getText());
  82. assertEquals("Greeting service was not called with 'bar' parameter.",
  83. service.getGreeting("bar"), $(CssLayoutElement.class).id("log")
  84. .$(LabelElement.class).get(1).getText());
  85. }
  86. @Test
  87. public void testUIScopedView() {
  88. GreetingService service = new GreetingService();
  89. List<String> expectedLogContents = new ArrayList<>();
  90. getDriver().navigate().to(BASE_URL);
  91. expectedLogContents
  92. .add(navigateAndGetLogContent("persisting", "foo", service));
  93. // Navigation event should with same view and different parameters
  94. expectedLogContents
  95. .add(navigateAndGetLogContent("persisting", "", service));
  96. // Navigating to another view should not lose the UI Scoped view.
  97. navigateAndGetLogContent("new");
  98. expectedLogContents
  99. .add(navigateAndGetLogContent("persisting", "", service));
  100. assertEquals("GreetingService unexpected call count",
  101. String.format(GreetingView.CALL_COUNT_FORMAT,
  102. service.getCallCount()),
  103. $(LabelElement.class).id("callCount").getText());
  104. assertEquals("Unexpected contents in the log.", expectedLogContents,
  105. $(CssLayoutElement.class).id("log").$(LabelElement.class).all()
  106. .stream().map(LabelElement::getText)
  107. .collect(Collectors.toList()));
  108. }
  109. @Test
  110. public void testPreserveOnRefreshWithViewName() {
  111. GreetingService service = new GreetingService();
  112. List<String> expectedLogContents = new ArrayList<>();
  113. getDriver().navigate().to(BASE_URL);
  114. expectedLogContents
  115. .add(navigateAndGetLogContent("name", "foo", service));
  116. // Navigation event should with same view and different parameters
  117. expectedLogContents
  118. .add(navigateAndGetLogContent("name", "bar", service));
  119. // Reload the page.
  120. getDriver().navigate().refresh();
  121. assertEquals("GreetingService should've been only called twice.",
  122. String.format(GreetingView.CALL_COUNT_FORMAT,
  123. service.getCallCount()),
  124. $(LabelElement.class).id("callCount").getText());
  125. assertEquals("Unexpected contents in the log.", expectedLogContents,
  126. $(CssLayoutElement.class).id("log").$(LabelElement.class).all()
  127. .stream().map(LabelElement::getText)
  128. .collect(Collectors.toList()));
  129. }
  130. private void assertNavigationNotification(String string) {
  131. waitUntil(e -> isElementPresent(NotificationElement.class));
  132. NotificationElement notification = $(NotificationElement.class).first();
  133. assertEquals(String.format(RootPathUI.NAVIGATION_TEXT, string),
  134. notification.getText());
  135. // Close all notifications.
  136. while (isElementPresent(NotificationElement.class)) {
  137. $(NotificationElement.class).first().close();
  138. }
  139. }
  140. private String navigateAndGetLogContent(String viewName) {
  141. return navigateAndGetLogContent(viewName, "", new GreetingService());
  142. }
  143. private String navigateAndGetLogContent(String viewName, String parameter) {
  144. return navigateAndGetLogContent(viewName, parameter, new GreetingService());
  145. }
  146. private String navigateAndGetLogContent(String viewName, String parameter,
  147. GreetingService service) {
  148. String fullViewName = viewName
  149. + (parameter.isEmpty() ? "" : "/" + parameter);
  150. $(ButtonElement.class).caption(fullViewName).first().click();
  151. assertNavigationNotification(viewName);
  152. return service.getGreeting(parameter);
  153. }
  154. }