Demonstrating Selenium standalone Chrome in a Spring Boot project.
You got a Spring Boot application. You want to integration-test this application with Selenium using a real browser like Google Chrome. You are using Docker(-compose), but you don't have an OperatingSystem-GUI available.
For this demo, Spring Boot is used, which ships with a lot of build-in integration-test features. The application is very simple, it serves http://localhost:8080/ as a "Hello World"-endpoint, see HelloController.java.
Required
- Maven
- Docker, including Docker-compose
selenium-standalone-chrome-spring-boot-demo $ mvn verify
...
Running schulte.markus.seleniumstandalonechromespringboot.controller.HelloControllerIT
...
[INFO] BUILD SUCCESS
$ docker ps # While "mvn verify" runs
CONTAINER ID IMAGE ... PORTS NAMES
2ec2cd60f14a selenium/standalone-chrome-debug ... 0.0.0.0:32779->4444/tcp, 0.0.0.0:32778->5900/tcp a5fc9c37_selenium-standalone-chrome_1
I am describing basic setup here. For details, have a look at HelloControllerIT.java, which is straight-forward.
Selenium provides a docker-image selenium/standalone-chrome. You can use this docker-image within your test by using Docker Compose JUnit Rule. You have to use selenium-java, of course. By using WebDriverManager, there is no need to care for downloading a Selenium WebDriver yourself.
When running HelloControllerIT.java, a selenium/standalone-chrome -container is started, while this Spring Boot application is launched with a random port on localhost. Now in the test, you can use Selenium-RemoteWebDriver to connect to the running Selenium-Hub within the selenium/standalone-chrome -container. Selenium/standalone-chrome-debug docker-images is used here, so you can access VNC on the running container by accessing the published port 5900.