How to pass java code a parameter from maven for testing
Asked Answered
F

6

31

I need to pass on following values …

exeEvironment (Test environment) , testGroup (Group in testNG)

from Command-Line -> POM -> TestNG -> Test cases.

Based on these two posts ....

pass a java parameter from maven

How to pass parameters to guicified TestNG test from Surefire Maven plugin?

I did the following configuration ..

In surefire plugin, I tried following two options, none seem to work.

=====

(1)

  <execution>
<id>default-test</id>
    <goals>
        <goal>test</goal>
    </goals>
    <configuration>
        <properties>
            <exeEnvironment>${exeEnvironment}</exeEnvironment>
            <testGroup>${testGroup}</testGroup>
        </properties>
        <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
        </suiteXmlFiles>
    </configuration>
</execution>

(2)

<execution>
<id>default-test</id>
<goals>
    <goal>test</goal>
</goals>
<configuration>
    <systemPropertyVariables> <exeEnvironment>${exeEnvironment}</exeEnvironment> 
        <testGroup>${testGroup}</testGroup> </systemPropertyVariables> 
    <suiteXmlFiles>
        <suiteXmlFile>testng.xml</suiteXmlFile>
    </suiteXmlFiles>
</configuration>
</execution>

In testNG.xml , can I use the the variable testGroup like …

<test name="Web Build Acceptance">
    <groups>
        <run>
            <include name="${testGroup} />
        </run>
    </groups>
    <classes>
        <class name="com.abc.pqr" />
    </classes>
</test>

This doesn't seem to work as well, do I need to define a parameter.


In the test cases , I tried to get he variables in following two ways …. (1)

testEnv = testContext.getSuite().getParameter("exeEnvironment");
testGroup = testContext.getSuite().getParameter("testGroup");

(2)

testEnv = System.getProperty("exeEnvironment");
testGroup = System.getProperty("testGroup");

Ferromagnesian answered 6/11, 2012 at 6:14 Comment(0)
P
51

This is the exact thing I was looking for my automation test and I got it working.

Command Line argument

mvn clean test -Denv.USER=UAT -Dgroups=Sniff

My Pom Xml

<?xml version="1.0" encoding="UTF-8"?>
<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>TestNg</groupId>
    <artifactId>TestNg</artifactId>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <systemPropertyVariables>
                        <environment>${env.USER}</environment>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

TestNG test

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;


public class TestAuthentication {

    @Test (groups = { "Sniff", "Regression" })
    public void validAuthenticationTest(){
        System.out.println(" Sniff + Regression" + System.getProperty("environment"));
    }

    @Test (groups = { "Regression" },parameters = {"environment"})
    public void failedAuthenticationTest(String environment){
        System.out.println("Regression-"+environment);
    }

    @Parameters("environment")
    @Test (groups = { "Sniff"})
    public void newUserAuthenticationTest(String environment){
        System.out.println("Sniff-"+environment);
    }
}

The above works well. Additionally, if you need to use testng.xml, you can specify the suiteXmlFile like ...

      <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12.4</version>
            <configuration>
                <systemPropertyVariables>
                    <environment>${env.USER}</environment>
                </systemPropertyVariables>
                <suiteXmlFiles> 
                    <suiteXmlFile>testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>

Also, I prefer using @Parameters instead of parameters in @Test() as the later is deprecated.

Polyphonic answered 29/11, 2012 at 18:3 Comment(5)
Looks promising , let me try and get back. Thanks for sharing.Ferromagnesian
By the way, ate you using testng.xml here ?Ferromagnesian
I am not using testng.xml, By the way you don't need to add the parameter in the POM file. mvn clean test -Denvironment=QA -Dgroups=Regression if you use this 1. only Regression group will be executed (@Test(groups = { "Sniff", "Regression" }) 2. @Parameters("environment") - value "QA" will be passed directly to your testPolyphonic
Runs smooth, one thing btw, you need to execute with mvn clean test "-D..."Holcomb
In case you would like to pass several parameters, you can separate them with a coma, e.g. @Parameters({"param1","param2"})Shona
P
5

You need not define anything for groups in testng xml or the pom, the support comes inbuilt. You can simply specify the groups on the cmd line http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#groups

Hope it helps..

Edit 2:

Ok..so here's another option...Implement IMethodInterceptor

Define your custom property. Use -Dcustomproperty=groupthatneedstoberun in your command line call.

In the intercept call, scan through all methods ..something to the effect..

System.getProperty("customproperty");
for(IMethodInstance ins : methods) {
    if(ins.getMethod().getGroups()) contains group)
        Add to returnedVal;
    }
return returnedVal;

Add this to the listeners list in your xml.

Potman answered 6/11, 2012 at 6:31 Comment(1)
Thanks for the answer and you are correct , groups are inbuild in surefire pom plugin but it will be ignored if I specify suiteXMLFile, which I'm specifying. SO basically I want to provide the groups dynamically on the command line to the POM and it should be able to run those tests using testNG. Any other Ideas please ?Ferromagnesian
E
2

Passing parameter like browser and other can be done as below :

<properties>    
    <BrowserName></BrowserName>
    <TestRunID></TestRunID>
</properties>



        <!-- Below plug-in is used to execute tests -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>src/test/resources/${testXml}</suiteXmlFile>
                </suiteXmlFiles>
                <systemPropertyVariables>
                  <browserName>${BrowserName}</browserName>
                    <testRunID>${TestRunID}</testRunID> 
                </systemPropertyVariables>
            </configuration>
            <executions>
                <execution>
                    <id>surefire-it</id>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <skip>false</skip>
                        <testFailureIgnore>true</testFailureIgnore>
                    </configuration>
                </execution>
            </executions>
        </plugin>

and to handle this in java code use this :

 public static final String Browser_Jenkin=System.getProperty("BrowserName");
    public static final String TestRunID=System.getProperty("TestRunID");

  public static String browser_Setter()
    {
        String value=null;
        try {
            if(!Browser_Jenkin.isEmpty())
            {
                value = Browser_Jenkin;
            }
        } catch (Exception e) {
            value =propObj.getProperty("BROWSER");
        }
        return value;   
    }

    public static String testRunID_Setter()
    {
        String value=null;
        try {
            if(!TestRunID.isEmpty())
            {
                value = TestRunID;
            }
        } catch (Exception e) {
            value =propObj.getProperty("TEST_RUN_ID");
        }
        return value;   
    }
Erdmann answered 24/4, 2017 at 9:30 Comment(0)
S
1

Perfect.

The simplest way to pass the variable from POM.xml to ABC.java

POM.xml

<properties>
   <hostName>myhostname.com</hostName>
</properties>

And in the ABC.java we can call it from the system properties like this

System.getProperty("hostName")
Stockist answered 28/5, 2014 at 9:25 Comment(2)
16:20:15,676 DEBUG TestApp:21 - hostname: nullHarakiri
This also fails for me with a "null"Just
M
1

building on the accepted answer

if maven surefire and the <systemPropertyVariables> are declared in a maven profile, they are not available and will return null unless the profile is also invoked.

Command Line argument

mvn clean test -PmyTestProfile -Denv.USER=UAT -Dgroups=Sniff 

pom.xml

<profiles>
    <profile>
        <id>myTestProfile</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${surefire.version}</version>
                    <configuration>
                        <systemPropertyVariables>
                            <environment>${env.USER}</environment>
                        </systemPropertyVariables>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
Microsporangium answered 26/11, 2020 at 17:44 Comment(0)
S
0

You don't need to use environment variables or edit pom.xml to use them.

The goals and options for Invoke Maven 3 under Build section takes the parameter. Try this (assuming you parameterized the build):

Invoke Maven 3
  Goals and options = test -Denv=$PARAM_ENV -Dgroup=$PARAM_GROUP
Selfpropulsion answered 28/1, 2016 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.