Skip to content

Commit 74e13fd

Browse files
committed
test: configure TestBench integration tests
1 parent 0720760 commit 74e13fd

File tree

5 files changed

+229
-0
lines changed

5 files changed

+229
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
/package.json
88
/package-lock.json
99
/webpack.config.js
10+
/error-screenshots

pom.xml

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1616
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1717
<jetty.version>9.4.15.v20190215</jetty.version>
18+
<drivers.dir>${project.basedir}/drivers</drivers.dir>
1819
</properties>
1920

2021
<organization>
@@ -125,6 +126,23 @@
125126
<version>4.13.1</version>
126127
<scope>test</scope>
127128
</dependency>
129+
<dependency>
130+
<groupId>com.vaadin</groupId>
131+
<artifactId>vaadin-testbench</artifactId>
132+
<scope>test</scope>
133+
</dependency>
134+
<dependency>
135+
<groupId>org.hamcrest</groupId>
136+
<artifactId>hamcrest-library</artifactId>
137+
<version>1.3</version>
138+
<scope>test</scope>
139+
</dependency>
140+
<dependency>
141+
<groupId>io.github.bonigarcia</groupId>
142+
<artifactId>webdrivermanager</artifactId>
143+
<version>4.3.1</version>
144+
<scope>test</scope>
145+
</dependency>
128146
<dependency>
129147
<groupId>com.flowingcode.vaadin.addons.demo</groupId>
130148
<artifactId>commons-demo</artifactId>
@@ -317,6 +335,95 @@
317335
</plugins>
318336
</build>
319337
</profile>
338+
339+
<profile>
340+
<id>it</id>
341+
<build>
342+
<plugins>
343+
<plugin>
344+
<groupId>org.eclipse.jetty</groupId>
345+
<artifactId>jetty-maven-plugin</artifactId>
346+
<version>${jetty.version}</version>
347+
<configuration>
348+
<scanIntervalSeconds>0</scanIntervalSeconds>
349+
<supportedPackagings>
350+
<supportedPackaging>jar</supportedPackaging>
351+
</supportedPackagings>
352+
<stopKey>${project.artifactId}</stopKey>
353+
<stopPort>8081</stopPort>
354+
</configuration>
355+
<executions>
356+
<execution>
357+
<id>start-jetty</id>
358+
<phase>pre-integration-test</phase>
359+
<goals>
360+
<goal>start</goal>
361+
</goals>
362+
</execution>
363+
<execution>
364+
<id>stop-jetty</id>
365+
<phase>post-integration-test</phase>
366+
<goals>
367+
<goal>stop</goal>
368+
</goals>
369+
</execution>
370+
</executions>
371+
</plugin>
372+
373+
374+
<!-- Runs the integration tests (*IT) after the server is started -->
375+
<plugin>
376+
<groupId>org.apache.maven.plugins</groupId>
377+
<artifactId>maven-failsafe-plugin</artifactId>
378+
<version>2.22.2</version>
379+
<executions>
380+
<execution>
381+
<goals>
382+
<goal>integration-test</goal>
383+
<goal>verify</goal>
384+
</goals>
385+
</execution>
386+
</executions>
387+
<configuration>
388+
<trimStackTrace>false</trimStackTrace>
389+
<enableAssertions>true</enableAssertions>
390+
<systemPropertyVariables>
391+
<!-- Pass location of downloaded webdrivers to the tests -->
392+
<webdriver.chrome.driver>
393+
${webdriver.chrome.driver}
394+
</webdriver.chrome.driver>
395+
</systemPropertyVariables>
396+
</configuration>
397+
</plugin>
398+
399+
<plugin>
400+
<artifactId>maven-resources-plugin</artifactId>
401+
<version>3.1.0</version>
402+
<executions>
403+
<execution>
404+
<!-- Since the view class is defined in test, after compilation
405+
copy it to the runtime classpath to ensure it gets visited by
406+
vaadin-maven-plugin -->
407+
<id>copy-test-to-classes</id>
408+
<phase>process-test-classes</phase>
409+
<goals>
410+
<goal>copy-resources</goal>
411+
</goals>
412+
<configuration>
413+
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
414+
<resources>
415+
<resource>
416+
<directory>${project.build.testOutputDirectory}</directory>
417+
</resource>
418+
</resources>
419+
</configuration>
420+
</execution>
421+
</executions>
422+
</plugin>
423+
</plugins>
424+
</build>
425+
</profile>
426+
320427
</profiles>
321428
</project>
322429

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.flowingcode.vaadin.addons.chipfield.integration;
2+
3+
import static org.hamcrest.Matchers.is;
4+
import static org.hamcrest.Matchers.not;
5+
6+
import org.hamcrest.Description;
7+
import org.hamcrest.Matcher;
8+
import org.hamcrest.TypeSafeDiagnosingMatcher;
9+
import org.junit.Before;
10+
import org.junit.BeforeClass;
11+
import org.junit.Rule;
12+
import org.openqa.selenium.chrome.ChromeDriver;
13+
14+
import com.vaadin.testbench.ScreenshotOnFailureRule;
15+
import com.vaadin.testbench.TestBench;
16+
import com.vaadin.testbench.TestBenchElement;
17+
import com.vaadin.testbench.parallel.ParallelTest;
18+
19+
import io.github.bonigarcia.wdm.WebDriverManager;
20+
21+
/**
22+
* Base class for ITs. The tests use Chrome driver to run integration tests on a
23+
* headless Chrome.
24+
*/
25+
public abstract class AbstractViewTest extends ParallelTest {
26+
27+
private static final int SERVER_PORT = 8080;
28+
29+
private final String route;
30+
31+
protected static final Matcher<TestBenchElement> hasBeenUpgradedToCustomElement = new TypeSafeDiagnosingMatcher<TestBenchElement>() {
32+
33+
@Override
34+
public void describeTo(Description description) {
35+
description.appendText("a custom element");
36+
}
37+
38+
@Override
39+
protected boolean matchesSafely(TestBenchElement item, Description mismatchDescription) {
40+
String script = "let s=arguments[0].shadowRoot; return !!(s&&s.childElementCount)";
41+
if (!item.getTagName().contains("-")) {
42+
return true;
43+
}
44+
45+
if ((Boolean) item.getCommandExecutor().executeScript(script, item)) {
46+
return true;
47+
} else {
48+
mismatchDescription.appendText(item.getTagName() + " ");
49+
mismatchDescription.appendDescriptionOf(is(not(this)));
50+
return false;
51+
}
52+
}
53+
54+
};
55+
56+
@Rule
57+
public ScreenshotOnFailureRule rule = new ScreenshotOnFailureRule(this, true);
58+
59+
protected AbstractViewTest(String route) {
60+
this.route = route;
61+
}
62+
63+
@BeforeClass
64+
public static void setupClass() {
65+
WebDriverManager.chromedriver().setup();
66+
}
67+
68+
@Override
69+
@Before
70+
public void setup() throws Exception {
71+
setDriver(TestBench.createDriver(new ChromeDriver()));
72+
getDriver().get(getURL(route));
73+
}
74+
75+
/**
76+
* Returns deployment host name concatenated with route.
77+
*
78+
* @return URL to route
79+
*/
80+
private static String getURL(String route) {
81+
return String.format("http://%s:%d/%s", getDeploymentHostname(), SERVER_PORT, route);
82+
}
83+
84+
/**
85+
* If running on CI, get the host name from environment variable HOSTNAME
86+
*
87+
* @return the host name
88+
*/
89+
private static String getDeploymentHostname() {
90+
return "localhost";
91+
}
92+
93+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.flowingcode.vaadin.addons.chipfield.integration;
2+
3+
import com.vaadin.testbench.TestBenchElement;
4+
import com.vaadin.testbench.elementsbase.Element;
5+
6+
@Element("paper-chip-input-autocomplete")
7+
public class ChipFieldElement extends TestBenchElement {
8+
9+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.flowingcode.vaadin.addons.chipfield.integration;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
5+
import org.junit.Test;
6+
7+
public class ViewIT extends AbstractViewTest {
8+
9+
public ViewIT() {
10+
super("it");
11+
}
12+
13+
@Test
14+
public void upgradedToCustomElement() {
15+
ChipFieldElement chipfield = $(ChipFieldElement.class).first();
16+
assertThat(chipfield, hasBeenUpgradedToCustomElement);
17+
}
18+
19+
}

0 commit comments

Comments
 (0)