fix: integration-tests WebDriverException + add logging
* use own docker network
* log warnings from selenium/tomcat container
* some improvements (wait for container, getTomcatUrl(), container aliases, logback-test.xml)
diff --git a/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/integration/BasicTest.java b/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/integration/BasicTest.java
index 19ae800..7c0999f 100644
--- a/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/integration/BasicTest.java
+++ b/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/integration/BasicTest.java
@@ -62,18 +62,15 @@
@Test
void verifyExceptionTest() throws MalformedURLException, UnknownHostException, UnsupportedEncodingException {
- final String host = InetAddress.getLocalHost().getHostAddress();
- final int tomcatPort = getTomcatPort();
final String path = "error/exception.xhtml";
final String base = path.substring(0, path.length() - 6);
- final String url = "http://" + host + ":" + tomcatPort + "/test.xhtml?base="
- + URLEncoder.encode(base, "UTF-8") + "&basicTest=true";
+ final String testUrl = getTomcatUrl() + "/test.xhtml?base=" + URLEncoder.encode(base, "UTF-8") + "&basicTest=true";
- WebDriver webDriver = getWebDriver(host, getFirefoxPort());
- webDriver.get(url);
+ WebDriver webDriver = getWebDriver();
+ webDriver.get(testUrl);
- List<WebElement> results = getJasmineResults(webDriver, url);
+ List<WebElement> results = getJasmineResults(webDriver, testUrl);
Assertions.assertTrue(results.size() > 0, "no results detected");
for (WebElement result : results) {
if ("has no exception".equals(result.getAttribute("title"))) {
@@ -86,18 +83,15 @@
@Test
void verify404Test() throws MalformedURLException, UnknownHostException, UnsupportedEncodingException {
- final String host = InetAddress.getLocalHost().getHostAddress();
- final int tomcatPort = getTomcatPort();
final String path = "error/404.xhtml";
final String base = path.substring(0, path.length() - 6);
- final String url = "http://" + host + ":" + tomcatPort + "/test.xhtml?base="
- + URLEncoder.encode(base, "UTF-8") + "&basicTest=true";
+ final String testUrl = getTomcatUrl() + "/test.xhtml?base=" + URLEncoder.encode(base, "UTF-8") + "&basicTest=true";
- WebDriver webDriver = getWebDriver(host, getFirefoxPort());
- webDriver.get(url);
+ WebDriver webDriver = getWebDriver();
+ webDriver.get(testUrl);
- List<WebElement> results = getJasmineResults(webDriver, url);
+ List<WebElement> results = getJasmineResults(webDriver, testUrl);
Assertions.assertTrue(results.size() > 0, "no results detected");
for (WebElement result : results) {
if ("has no 404".equals(result.getAttribute("title"))) {
@@ -117,17 +111,14 @@
throws MalformedURLException, UnknownHostException, UnsupportedEncodingException {
final String timeLeft = getTimeLeft(startTime, testSize, testNumber);
- final String host = InetAddress.getLocalHost().getHostAddress();
- final int tomcatPort = getTomcatPort();
- final String pageUrl = "http://" + host + ":" + tomcatPort + "/" + path;
+ final String pageUrl = getTomcatUrl() + "/" + path;
LOG.info("(" + testNumber + "/" + testSize + " | time left: " + timeLeft + ") - url: " + pageUrl);
final String base = path.substring(0, path.length() - 6);
- final String testUrl = "http://" + host + ":" + tomcatPort + "/test.xhtml?base="
- + URLEncoder.encode(base, "UTF-8") + "&basicTest=true";
+ final String testUrl = getTomcatUrl() + "/test.xhtml?base=" + URLEncoder.encode(base, "UTF-8") + "&basicTest=true";
- WebDriver webDriver = getWebDriver(host, getFirefoxPort());
+ WebDriver webDriver = getWebDriver();
webDriver.get(testUrl);
List<WebElement> results = getJasmineResults(webDriver, pageUrl);
diff --git a/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/integration/FrontendBase.java b/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/integration/FrontendBase.java
index 999bdd9..f9d6744 100644
--- a/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/integration/FrontendBase.java
+++ b/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/integration/FrontendBase.java
@@ -33,13 +33,18 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.Network;
+import org.testcontainers.containers.output.Slf4jLogConsumer;
+import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.utility.DockerImageName;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
+import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
+import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -52,17 +57,25 @@
private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
- @SuppressWarnings("rawtypes") // this is how to use testcontainers
- @Container
- private static final GenericContainer SELENIUM_FIREFOX =
- new GenericContainer<>(DockerImageName.parse("henningn/selenium-standalone-firefox"))
- .withExposedPorts(4444);
+ private static final Network NETWORK = Network.newNetwork();
+ private static final String SELENIUM_ALIAS = "selenium";
+ private static final String TOMCAT_ALIAS = "tomcat";
+ public static final int SELENIUM_PORT = 4444;
+ public static final int TOMCAT_PORT = 8080;
- @SuppressWarnings("rawtypes") // this is how to use testcontainers
@Container
- private static final GenericContainer TOMCAT =
- new GenericContainer(DockerImageName.parse("myfaces/tobago-example-demo"))
- .withExposedPorts(8080);
+ private static final GenericContainer<?> SELENIUM_FIREFOX =
+ new GenericContainer<>(DockerImageName.parse("henningn/selenium-standalone-firefox"))
+ .withNetwork(NETWORK).withNetworkAliases(SELENIUM_ALIAS).withExposedPorts(SELENIUM_PORT)
+ .waitingFor(Wait.forHttp("/").forPort(SELENIUM_PORT))
+ .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(SELENIUM_ALIAS)).withSeparateOutputStreams());
+
+ @Container
+ private static final GenericContainer<?> TOMCAT =
+ new GenericContainer<>(DockerImageName.parse("myfaces/tobago-example-demo"))
+ .withNetwork(NETWORK).withNetworkAliases(TOMCAT_ALIAS).withExposedPorts(TOMCAT_PORT)
+ .waitingFor(Wait.forHttp("/").forPort(TOMCAT_PORT))
+ .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(TOMCAT_ALIAS)).withSeparateOutputStreams());
private static WebDriver firefoxDriver;
@@ -84,8 +97,10 @@
.collect(Collectors.toList());
}
- WebDriver getWebDriver(final String host, final Integer port) throws MalformedURLException {
+ WebDriver getWebDriver() throws MalformedURLException, UnknownHostException {
if (firefoxDriver == null || ((RemoteWebDriver) firefoxDriver).getSessionId() == null) {
+ final String host = InetAddress.getLocalHost().getHostAddress();
+ final int port = SELENIUM_FIREFOX.getFirstMappedPort();
firefoxDriver = new RemoteWebDriver(new URL("http://" + host + ":" + port + "/wd/hub"), new FirefoxOptions());
}
return firefoxDriver;
@@ -146,11 +161,7 @@
}
}
- int getFirefoxPort() {
- return SELENIUM_FIREFOX.getFirstMappedPort();
- }
-
- int getTomcatPort() {
- return TOMCAT.getFirstMappedPort();
+ String getTomcatUrl() {
+ return "http://" + TOMCAT_ALIAS + ":" + TOMCAT_PORT;
}
}
diff --git a/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/integration/FrontendTest.java b/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/integration/FrontendTest.java
index e314b32..37a41cb 100644
--- a/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/integration/FrontendTest.java
+++ b/tobago-example/tobago-example-demo/src/test/java/org/apache/myfaces/tobago/example/demo/integration/FrontendTest.java
@@ -32,7 +32,6 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.invoke.MethodHandles;
-import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.net.UnknownHostException;
@@ -62,16 +61,14 @@
throws MalformedURLException, UnknownHostException, UnsupportedEncodingException {
final String timeLeft = getTimeLeft(FrontendTest.startTime, testSize, testNumber);
- final String host = InetAddress.getLocalHost().getHostAddress();
- final int tomcatPort = getTomcatPort();
- final String pageUrl = "http://" + host + ":" + tomcatPort + "/" + path;
+ final String pageUrl = getTomcatUrl() + "/" + path;
LOG.info("(" + testNumber + "/" + testSize + " | time left: " + timeLeft + ") - url: " + pageUrl);
final String base = path.substring(0, path.length() - 6);
- final String testUrl = "http://" + host + ":" + tomcatPort + "/test.xhtml?base=" + URLEncoder.encode(base, "UTF-8");
+ final String testUrl = getTomcatUrl() + "/test.xhtml?base=" + URLEncoder.encode(base, "UTF-8");
- WebDriver webDriver = getWebDriver(host, getFirefoxPort());
+ WebDriver webDriver = getWebDriver();
webDriver.get(testUrl);
List<WebElement> results = getJasmineResults(webDriver, pageUrl);
diff --git a/tobago-example/tobago-example-demo/src/test/java/org/junit/rules/ExternalResource.java b/tobago-example/tobago-example-demo/src/test/java/org/junit/rules/ExternalResource.java
new file mode 100644
index 0000000..810e944
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/test/java/org/junit/rules/ExternalResource.java
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.junit.rules;
+
+/*
+ * the fake class is needed for testcontainers, because of an dependency to junit4
+ * https://github.com/testcontainers/testcontainers-java/issues/970
+ */
+public abstract class ExternalResource implements TestRule {
+}
diff --git a/tobago-example/tobago-example-demo/src/test/resources/logback-test.xml b/tobago-example/tobago-example-demo/src/test/resources/logback-test.xml
new file mode 100644
index 0000000..1b09d19
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/test/resources/logback-test.xml
@@ -0,0 +1,35 @@
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements. See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership. The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied. See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ -->
+
+<configuration>
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder>
+ <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
+ </encoder>
+ </appender>
+
+ <root level="info">
+ <appender-ref ref="STDOUT"/>
+ </root>
+
+ <logger name="org.testcontainers" level="INFO"/>
+ <logger name="com.github.dockerjava" level="WARN"/>
+ <logger name="tomcat" level="WARN"/>
+ <logger name="selenium" level="WARN"/>
+</configuration>