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.

Run Test Automation Scripts in Command Prompt With MAVEN

While developing the Selenium WebDriver test automation scripts, the scripts are executed within Eclipse.

But as soon as the scripts work as expected, you may want to make the scripts available to other team members.

Like other manual testers.

Or developers.

You may also want to run the test scripts automatically as scheduled jobs.

Or use them in a continuous integration process.

All these require the ability of running the test scripts in the Command Prompt.

Let`s see how you can do this.

We will use an example project that automates the following test case:

  1. opens the home page of the Vancouver Public Library site
  2. checks if the home page title is correct

The project uses 2 classes: test class and home page class.


import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestClass {

  WebDriver driver;

  public void setUp() {
    driver = new FirefoxDriver();

  public void tearDown() {

  public void testScript() {
    HomePage homePage = new HomePage(driver);;



import org.openqa.selenium.WebDriver;
public class HomePage {
  WebDriver driver;
  String url = "";
  String title = "Vancouver Public Library - Home";

  public HomePage(WebDriver driver) {
    this.driver = driver;

  public void open()  {

  public Boolean isTitleCorrect() {
    return driver.getTitle().equals(title);


The following phases are needed for running this code from command prompt.

Everything that follows assumes that Maven is installed on your computer.

If you dont have it installed, use this link for instructions.

If you want to learn more about Maven, this is a very good tutorial.

  1. Create the Maven Project

This can be done manually without Eclipse.

Or you can use Eclipse for generating the project:

  • Open Eclipse
  • Click File, New, Project
  • In the New Project window, expand the Maven category
  • Select Maven project and click Next

1. select maven project

  • Check the Create A Simple Project checkbox
  • Click Next

2. maven - create a simple project

  • Configure the Maven project by providing the Group ID, Artifact ID and Name

3. configure maven project

  • Click Finish

The project is created and it shows in the Navigator panel of Eclipse.

You can see the project structure by expanding the project node:

4. maven project structure

The project folder includes

  • SRC folder; includes source code for the test and page object classes.
  • TARGET folder; includes the resources generated by compiling the source code.
  • POM.XML file; it defines the project dependencies

Under the SRC folder, you will find 2 folders:

  • main; the page objects go here
  • test; the test classes go here

When the project is created, the POM.XML file has just a few lines:

5. initial pom file

The project does not have any dependencies added yet.

6. no maven dependencies

2. Add dependencies to the POM.XML file

2 dependencies are needed for our project:

  1. JUNIT

They are added to POM.XML by editing the file:

8. add junit and selenium dependencies to pom file

The project has now 2 dependencies added to it:

9. junit and selenium are added as dependencies

3. Add the test and page object classes to the project folders

Next, we need to add the test class to the SRC – TEST – JAVA folder.

The page object class is added to the SRC – MAIN – JAVA folder.

10. add class files

4. Execute the Maven Project in Eclipse

The Maven project can be executed by

  • right clicking on the project name
  • clicking Run As
  • clicking Maven Test

11. run maven test

The test script is being executed and the results are displayed in the Console panel.

12. build success from eclipse

After the project execution, you can see the .class files in the TARGET folder of the project:

13. class files are created

5. Run the Maven Project in Command Prompt

First, we need to get the project folder path.

This can be done by

  • right clicking on the project name
  • clicking Properties
  • getting the value of the Location field

14. get project path

The last steps of the process are:

  • close Eclipse
  • open Command Prompt
  • go to the project folder path
  • execute the project using the mvn test command

15. test runs in command prompt

Try to create your own project using this short tutorial.

If you have any questions, please leave them in the Comments section.