1+ package org .vaadin .ext .javier ;
2+
3+ import org .junit .Before ;
4+ import org .junit .Rule ;
5+ import org .openqa .selenium .chrome .ChromeDriver ;
6+
7+ import com .vaadin .testbench .ScreenshotOnFailureRule ;
8+ import com .vaadin .testbench .TestBench ;
9+ import com .vaadin .testbench .parallel .ParallelTest ;
10+
11+ /**
12+ * Base class for ITs
13+ * <p>
14+ * The tests use Chrome driver (see pom.xml for integration-tests profile) to
15+ * run integration tests on a headless Chrome. If a property {@code test.use
16+ * .hub} is set to true, {@code AbstractViewTest} will assume that the
17+ * TestBench test is running in a CI environment. In order to keep the this
18+ * class light, it makes certain assumptions about the CI environment (such
19+ * as available environment variables). It is not advisable to use this class
20+ * as a base class for you own TestBench tests.
21+ * <p>
22+ * To learn more about TestBench, visit
23+ * <a href="https://vaadin.com/docs/v10/testbench/testbench-overview.html">Vaadin TestBench</a>.
24+ */
25+ public abstract class AbstractViewTest extends ParallelTest {
26+ private static final int SERVER_PORT = 8080 ;
27+
28+ private final String route ;
29+
30+ @ Rule
31+ public ScreenshotOnFailureRule rule = new ScreenshotOnFailureRule (this ,
32+ true );
33+
34+ public AbstractViewTest () {
35+ this ("" );
36+ }
37+
38+ protected AbstractViewTest (String route ) {
39+ this .route = route ;
40+ }
41+
42+ @ Before
43+ public void setup () throws Exception {
44+ if (isUsingHub ()) {
45+ super .setup ();
46+ } else {
47+ setDriver (TestBench .createDriver (new ChromeDriver ()));
48+ }
49+ getDriver ().get (getURL (route ));
50+ }
51+
52+ /**
53+ * Returns deployment host name concatenated with route.
54+ *
55+ * @return URL to route
56+ */
57+ private static String getURL (String route ) {
58+ return String .format ("http://%s:%d/%s" , getDeploymentHostname (),
59+ SERVER_PORT , route );
60+ }
61+
62+ /**
63+ * Property set to true when running on a test hub.
64+ */
65+ private static final String USE_HUB_PROPERTY = "test.use.hub" ;
66+
67+ /**
68+ * Returns whether we are using a test hub. This means that the starter
69+ * is running tests in Vaadin's CI environment, and uses TestBench to
70+ * connect to the testing hub.
71+ *
72+ * @return whether we are using a test hub
73+ */
74+ private static boolean isUsingHub () {
75+ return Boolean .TRUE .toString ().equals (
76+ System .getProperty (USE_HUB_PROPERTY ));
77+ }
78+
79+ /**
80+ * If running on CI, get the host name from environment variable HOSTNAME
81+ *
82+ * @return the host name
83+ */
84+ private static String getDeploymentHostname () {
85+ return isUsingHub () ? System .getenv ("HOSTNAME" ) : "localhost" ;
86+ }
87+ }
0 commit comments