Skip to content

Instantly share code, notes, and snippets.

@axdotl
Last active July 19, 2017 15:11
Show Gist options
  • Save axdotl/465b235fff97167ef6bff377dc31eb78 to your computer and use it in GitHub Desktop.
Save axdotl/465b235fff97167ef6bff377dc31eb78 to your computer and use it in GitHub Desktop.

Introduction

Here is described how to setup Selenium for a SpringBoot maven-project.

Setup

Maven

Add following dependency to projects pom.xml

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.4.0</version>
    <scope>test</scope>
</dependency>

Chromedriver has to be executable. Unfortunatly file-mod is lost when maven copy resources to target directory. This can be fixed by adding following snippet to the build section in pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>set-webdriver-filemod</id>
            <phase>process-test-classes</phase>
            <configuration>
                <target>
                    <chmod file="target/test-classes/chromedriver" perm="755"/>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

ChromeDriver

To reduce setup effort for other project members, we'll add ChromeDriver as test resource to the project.

Download latest chromedriver from here: https://sites.google.com/a/chromium.org/chromedriver/

Copy it to src/test/resources

Add a test

import java.io.IOException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class FrontendTest {

	private static final String DRIVER_NAME = "chromedriver";

	@Value("${local.server.port}")
	private int port;

	private WebDriver webDriver;

	@Before
	public void initWebDriver() throws IOException {
		String driverPath = this.getClass().getClassLoader().getResource(DRIVER_NAME).getPath();
		System.setProperty("webdriver.chrome.driver", driverPath);
		webDriver = new ChromeDriver();
	}

	@After
	public void quitDriver() {
		if (webDriver == null) {
			return;
		}
		webDriver.quit();
	}

	@Test
	public void pageTitle() {
		String url = String.format("localhost:%s", port);
		webDriver.navigate().to(url);
		System.out.println(webDriver.getTitle());
	}
}

Running on Jenkins

To run this test on a jenkins, just install chromium-browser on the build server. Othewise following exception will occur:

org.openqa.selenium.WebDriverException: unknown error: cannot find Chrome binary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment