How to get the value entered in the command line to Testng using maven ?
Add the following code to your pom.xml:
Add maven-surefire plugin in the dependency :
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<type>maven-plugin</type>
</dependency>
Add the build just like below and set the Systempropertyvariable just like you want.
For example: if you want to add an environment variable and set value to this environment variable at the run time.add the following tag inside the <systemPropertyVariables> tag.
<environement> ${environment} </envionment>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.21.0</version> <configuration> <suiteXmlFiles> <!-- <suiteXmlFile>Testsuites/test.xml</suiteXmlFile> <suiteXmlFile>Testsuites/Scantool.xml</suiteXmlFile> <suiteXmlFile>Testsuites/v2api.xml</suiteXmlFile> Note:- ===== mvn clean test -Dsurefire.suiteXmlFiles=testng.xml --> <suiteXmlFile>${suiteXmlFile}</suiteXmlFile> </suiteXmlFiles> <!-- Add the property value here and get it inside the program --> <systemPropertyVariables> <browser>${browsername}</browser> </systemPropertyVariables> </configuration> </plugin> </plugins> </build>
Testng file:File name: commandline.xml<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Commandline"> <test verbose="0" name="Commandline" group-by-instances="true" > <parameter name="value" value="${browser}" /> <classes> <class name="testcase.runtimecommandline" /> </classes> </test> </suite>Java class file
File name:runtimecommandline.java
You can access the value in two ways:
1. One is get the value directly by using System.getProperty("<tag name>")
2. Another one is set the value in testng xml file <parameter name="value" value="${browser}" />like this and you can access the value inside the parmeter.package testcase; import core.framework;import org.testng.annotations.Parameters;import org.testng.annotations.Test; public class runtimecommandline { @Test public void browser(){ //To use the command line feature use the following command to set the value :
mvn clean test -Dsurefire.suiteXmlFiles=Testsuites/commandline.xml -Dbrowser=firefox String commandlinevalue = System.getProperty("browser"); if(commandlinevalue.equals("")) commandlinevalue="Manually assign value to this variable since value is not passed in commandline";
System.out.println("Entered command line value : "+commandlinevalue); } @Parameters({"value"}) @Test public void paramter(String value){ System.out.println("parameter value: "+value); } }
In command line :Run the following command
mvn clean test -Dsurefire.suiteXmlFiles=commandline.xml -Dbrowser=firefoxOutput:
[INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running TestSuite ************************ Commandline ************************* ------------------------------ testcase.runtimecommandline:browser
-------------------------------------------- ->Entered command line value : firefox ------------------------------------------------------------------------------------------------ ------------------------------ testcase.runtimecommandline:paramter
-------------------------------------------- ->parameter value: firefox ------------------------------------------------------------------------------------------------ ********************** -E---N---D- ********************************* X X