How to run scripts in a specific browser with Maven

mvn test is the Maven command for running test scripts in command prompt (or Jenkins).

It runs all test scripts from the test class in the browser defined in the project.

But what if you would like to specify the browser name as a parameter of the command?

Such as

mvn test -Dbrowser=chrome


mvn test -Dbrowser=firefox

How can this be done?

There are 3 parts to it.


Create a custom driver class

The custom driver class should implement the WebDriver interface and all its methods.

Its constructor has browser name as parameter and it creates the appropriate web driver.


import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Driver implements WebDriver {

WebDriver driver;
String browserName;

public Driver(String browserName) {
  this.browserName = browserName;

  if (browserName.equalsIgnoreCase("chrome"))
   this.driver = new ChromeDriver();

  if (browserName.equalsIgnoreCase("firefox"))
   this.driver = new FirefoxDriver();

  if (browserName.equalsIgnoreCase("ie"))
   this.driver = new InternetExplorerDriver();


public void close() {


public WebElement findElement(By arg0) {
  return this.driver.findElement(arg0);

public List<WebElement> findElements(By arg0) {
  return this.driver.findElements(arg0);

public void get(String arg0) {


public String getCurrentUrl() {
  return this.driver.getCurrentUrl();

public String getPageSource() {
  return this.driver.getPageSource();

public String getTitle() {
  return this.driver.getTitle();

public String getWindowHandle() {
  return this.driver.getWindowHandle();

public Set<String> getWindowHandles() {
  return this.driver.getWindowHandles();

public Options manage() {
  return this.driver.manage();

public Navigation navigate() {
  return this.driver.navigate();

public void quit() {

public TargetLocator switchTo() {
  return this.driver.switchTo();


Read the browser property from Maven command

The maven command looks like

mvn test -Dbrowser=chrome

Browser is a system property that can be read in the setUp() method of the test class:

String browserName = getParameter("browser");


Create the driver based on the browser name

This is done as well in the setUp() method of the test class:


import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestsMaven {

 Driver driver;

 public void setUp() {
  String browserName = getParameter("browser");
  driver = new Driver(browserName);

 public void tearDown() {

 private String getParameter(String name) {
  String value = System.getProperty(name);
  if (value == null)
     throw new RuntimeException(name + " is not a parameter!");

  if (value.isEmpty())
    throw new RuntimeException(name + " is empty!");

  return value;

 public void test1() {




You can now execute the maven command in command prompt so that the tests run in a specific browser.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s