Skip to content

Commit

Permalink
Add WebApplicationTest.testPlaceholder w/ Selenium
Browse files Browse the repository at this point in the history
Uses Selenium WebDriver and headless Chrome. Adds the "placeholder"
class to the <p> element enclosing "Hello, World!" to enable the test
assertion.

If this passes in Github Actions, we're ready to start adding and
testing actual web UI elements.
  • Loading branch information
mbland committed Nov 7, 2023
1 parent acb6d03 commit e09d051
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
5 changes: 5 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ tomcatVer = "10.1.15"
junitVer = "5.10.0"
hamcrestVer = "2.2"
antJunitVer = "1.10.14"
seleniumVer = "4.15.0"

[libraries.servlet]
module = "jakarta.servlet:jakarta.servlet-api"
Expand All @@ -28,3 +29,7 @@ version.ref = "hamcrestVer"
[libraries.antJunit]
module = "org.apache.ant:ant-junit"
version.ref = "antJunitVer"

[libraries.selenium]
module = "org.seleniumhq.selenium:selenium-java"
version.ref = "seleniumVer"
1 change: 1 addition & 0 deletions strcalc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies {
testImplementation(libs.hamcrest)
testImplementation(libs.tomcat)
testImplementation(libs.jasper)
testImplementation(libs.selenium)

testRuntimeOnly("org.junit.platform:junit-platform-launcher")

Expand Down
2 changes: 1 addition & 1 deletion strcalc/src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
<title>String Calculator - Mike Bland Training</title>
</head>
<body>
<p>Hello, World!</p>
<p class="placeholder">Hello, World!</p>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,66 @@
package com.mike_bland.training.testing.stringcalculator;

import com.mike_bland.training.testing.annotations.LargeTest;
import com.mike_bland.training.testing.utils.LocalServer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.IOException;
import java.net.URI;
import java.time.Duration;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class WebApplicationTest {
private static LocalServer app;
private static URI appUri;
private WebDriver driver;

@BeforeAll
static void setUpClass() throws IOException, InterruptedException {
app = new LocalServer(
"dockerfiles/Dockerfile.tomcat-test", 8080
);
appUri = app.start(2500);
}

@BeforeEach
void setUp() {
// - https://www.selenium.dev/documentation/webdriver/browsers/chrome/
// - https://github.com/GoogleChrome/chrome-launcher/blob/main/docs/chrome-flags-for-tools.md
// - https://peter.sh/experiments/chromium-command-line-switches/
var options = new ChromeOptions();
options.addArguments("--headless");
driver = new ChromeDriver(options);
}

@AfterEach
void tearDown() {
driver.quit();
}

@AfterAll
static void tearDownClass() throws IOException, InterruptedException {
app.stop(2500);
}

String endpoint(String relPath) {
if (!relPath.startsWith("/")) {
final var msg = "endpoint path should begin with '/', got: \"%s\"";
throw new IllegalArgumentException(msg.formatted(relPath));
}
return appUri
.resolve("%s%s".formatted(Servlet.DEFAULT_ROOT, relPath))
.toString();
}

// This placeholder test exists solely to allow the Gradle "test" task to
// pass until actual @SmallTests are present:
//
Expand All @@ -17,5 +75,11 @@ public class WebApplicationTest {
// Please replace or delete it when you're ready to add actual tests.
@LargeTest
void testPlaceholder() {
driver.get(endpoint("/"));
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));

WebElement body = driver.findElement(By.cssSelector("p.placeholder"));

assertEquals("Hello, World!", body.getText());
}
}

0 comments on commit e09d051

Please sign in to comment.