Sameera De Silva
3 min readJul 27, 2020

--

Parametrize and run a webdriver Test suite .xml using maven-surefire-plugin

Official documentation of Maven surefire plugin
https://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html

In this article , I’ll demonstrate how to run a webdriver test suite xml using maven-surefire-plugin which already has test plan level parameters like credentials

Preconditions-

JDK must be installed and JAVA_HOME=C:\Program Files\Java\jdk1.8.0_171
Also if you use eclipse IDE , JRE system library should set to JDK location.

I have created a maven project in my eclipse IDE and added maven-surefire-plugin . make sure to add it as a plugin , not as a dependency .

You can get it from https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin/3.0.0

Example-

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<systemPropertyVariables>
<year>2020</year>
</systemPropertyVariables>
<suiteXmlFiles>
<suiteXmlFile>TestPlanLoc/FirstTestPlan.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>

Here I have a system property variable as year=2020 inside
<systemPropertyVariables> tag.

<suiteXmlFiles>
<suiteXmlFile>TestPlanLoc/FirstTestPlan.xml</suiteXmlFile>
</suiteXmlFiles>

Also I mentioned the the test plan location .

In my Java code I refer the year parameter .

@Parameters(“year”)//Parameter name
@Test(priority = 1, description = “get parameter from Test suite Surefire”, groups = { “smoke” })
public void getYear(String year) {
System.out.println(“get parameter from Test suite Surefire-” + year);
}

In this example I did not pass groups but just mentioned how to mention group in the Java code.

If you use group , so only the mentioned group will get executed.
If you haven’t mention any group ,in a test method of your java code it will be skipped
in the execution.
<configuration>
<groups>smoke,uat</groups>
</configuration>

To exclude groups use the parameter excludedGroups.

To run the pom.xml right click on it run as Maven Install.
To run via command line go to the source code folder and open a
command line and type mvn clean install

Output-

The title of the Site is https://www.google.com/
Username from Test plan:- sam
Password from Test plans :- XOXO
get parameter from Test suite Surefire-2020
***************End of testing shutdown happend **************************
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 12.088 s — in TestSuite

Testplan.xml-

<?xml version=”1.0" encoding=”UTF-8"?>
<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd">
<suite name=”FirstTestPlan” parallel=”tests”
thread-count=”1">
<test name=”ChromeTest”>
<parameter name = “username” value=”sam”/>
<parameter name = “password” value=”XOXO”/>
<classes>
<class name=”com.sam.scripts.FirstWebdriverScript” />
</classes>
</test> <! — Test →
</suite> <! — Suite →

Java code-

package com.sam.scripts;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class FirstWebdriverScript {

WebDriver driver;

@Test(groups = { “UAT” })
public void downloadMultipleFiles() {
// Navigate to the URL
driver.get(“https://www.google.com/");
System.out.println(“The title of the Site is “ + driver.getCurrentUrl());
}

@Parameters(“year”)//Parameter name
@Test(priority = 1, description = “get parameter from Test suite Surefire”, groups = { “smoke” })
public void getYear(String year) {
System.out.println(“get parameter from Test suite Surefire-” + year);
}

@Parameters({ “username”, “password” })
@Test(description = “Get credentials from test plan”)
public void testCaseTwo(String username, String password) {
System.out.println(“Username from Test plan:- “ + username);
System.out.println(“Password from Test plans :- “ + password);
}

@BeforeClass()
public void setUp() throws IOException {
System.setProperty(“webdriver.chrome.driver”, System.getProperty(“user.dir”) + “\\Jar_files\\chromedriver.exe”);
ChromeOptions options = new ChromeOptions();
// To start Chrome in Maximized browser window
options.addArguments(“start-maximized”);
// To remove Chrome is being controlled by automated test software
options.addArguments(“disable-infobars”);
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

driver.manage().window().maximize();
}

@AfterSuite()
public void shutDown() {
driver.close();
driver.quit();
try {
Runtime.getRuntime().exec(“taskkill /F /IM chromedriver.exe”);
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println(“***************End of testing shutdown happend **************************”);
}

}

pom.xml-

<project xmlns=”http://maven.apache.org/POM/4.0.0" xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.sam</groupId>
<artifactId>CiCdWebDriverJenkinsGitDocker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>CiCdWebDriverJenkinsGitDocker</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<! — https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<! — https://mvnrepository.com/artifact/org.testng/testng
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>
<! — https://mvnrepository.com/artifact/net.rcarz/jira-client
<dependency>
<groupId>net.rcarz</groupId>
<artifactId>jira-client</artifactId>
<version>0.5</version>
<scope>test</scope>
</dependency>
<! — https://mvnrepository.com/artifact/org.sonarsource.scanner.maven/sonar-maven-plugin
<dependency>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.7.0.1746</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<systemPropertyVariables>
<year>2020</year>
</systemPropertyVariables>
<suiteXmlFiles>
<suiteXmlFile>TestPlanLoc/FirstTestPlan.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>

--

--