/selenium-jupiter

JUnit 5 extension for Selenium

Primary LanguageJavaApache License 2.0Apache-2.0

Maven Central Build Status Quality Gate codecov badge-jdk License badge Support badge Twitter

Selenium-Jupiter

Selenium-Jupiter is a JUnit 5 extension aimed to ease the use of Selenium (WebDriver and Grid) in JUnit 5 tests. This library is open source, released under the terms of Apache 2.0 License.

Basic usage

In order to include Selenium-Jupiter in a Maven project, first add the following dependency to your pom.xml (Java 8 required):

<dependency>
	<groupId>io.github.bonigarcia</groupId>
	<artifactId>selenium-jupiter</artifactId>
	<version>2.1.1</version>
</dependency>

Selenium-Jupiter is typically used by tests. In that case, the scope of the dependency should be test (<scope>test</scope>).

Once we have included this dependency, Selenium-Jupiter manages the WebDriver instances and inject them as parameters in your JUnit 5 tests:

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.SeleniumExtension;

@ExtendWith(SeleniumExtension.class)
public class SeleniumJupiterTest {

    @Test
    public void testChrome(ChromeDriver driver) {
    	// use chrome in this test
    }

    @Test
    public void testFirefox(FirefoxDriver driver) {
    	// use firefox in this test
    }

}

Internally, Selenium-Jupiter uses WebDriverManager to manage the WebDriver binaries (i.e. chromedriver, geckodriver, operadriver, and so on) required to use local browsers.

Docker browsers

As of version 2, Selenium-Jupiter allows to use browsers in Docker containers. The only requirement is to get installed Docker Engine in the machine running the tests. A simple example using this feature is the following:

import static io.github.bonigarcia.BrowserType.CHROME;
import static io.github.bonigarcia.BrowserType.FIREFOX;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openqa.selenium.remote.RemoteWebDriver;

import io.github.bonigarcia.DockerBrowser;
import io.github.bonigarcia.SeleniumExtension;

@ExtendWith(SeleniumExtension.class)
public class SeleniumJupiterDockerTest {

    @Test
    public void testChrome(
            @DockerBrowser(type = CHROME, version = "latest") RemoteWebDriver driver) {
        // use chrome (latest version) in this test
    }

    @Test
    public void testFirefox(
            @DockerBrowser(type = FIREFOX, version = "57.0") RemoteWebDriver driver) {
        // use firefox (version 57.0) in this test
    }

}

Documentation

You can find more details and examples on the Selenium-Jupiter user guide.

About

Selenium-Jupiter (Copyright © 2017-2018) is a project by Boni Garcia licensed under Apache 2.0 License. Comments, questions and suggestions are always very welcome!