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

or

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.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

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() {
  this.driver.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) {
  this.driver.get(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() {
  this.driver.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;

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

 @After
 public void tearDown() {
  driver.quit();
 }

 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;
 }

 @Test
 public void test1() {
  driver.get("http://www.vpl.ca");
 }

}

}

 

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

Advertisements

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s