简体   繁体   中英

TestNG - How to print run time testng parameters in testng custom emailable report?

I found there is an option to set the parameters to testng xml through surefire plugin, by then the parameter can be sent from command line.

<plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M3</version>
        <configuration>
          <systemPropertyVariables>
            <browser>firefox</browser>
          </systemPropertyVariables>
        </configuration>
      </plugin>
    [...]
</plugins>

Ref: https://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html https://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html

There is a requirement to print the parameters in testng custom emailable report. Able to print the testng parameters specified in testng xml using the following code. But, not able to print the parameters specified in surefire plugin.

Note: System.getProperty("browser") works here. But, I want to print them without having to specifying the parameter name (say "browser") as below. But below one doesn't work.

Map<String, String> allParameters = context.getCurrentXmlTest().getAllParameters();
for(String parameter: allParameters.keySet()) {
    System.out.println(parameter + " : " + allParameters.get(parameter));
}

Example:

import java.util.Map;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class TestNGTest {
    ITestContext context;

    @BeforeTest
    public void beforeTest(ITestContext context) {
        this.context = context;
    }

    @Parameters({"browser"})
    @Test
    public void method(String browser) {
        System.out.println(browser);
        Map<String, String> allParameters = context.getCurrentXmlTest().getAllParameters();
        for(String parameter: allParameters.keySet()) {
            System.out.println(parameter + " : " + allParameters.get(parameter));
        }
    }
}

Actual Output:

[RemoteTestNG] detected TestNG version 7.0.0
chrome
key : value

===============================================
Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

Expected Output:

[RemoteTestNG] detected TestNG version 7.0.0
chrome
key : value
browser : chrome

===============================================
Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

Testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="classes" thread-count="4">
    <test name="Front-End" group-by-instances="true">
    <parameter name="key" value="value"></parameter>
        <classes>
            <class name="com.ftd.automation.framework.tests.TestNGTest" />
        </classes>
    </test>
</suite>

Please help me on how to print the testng parameters specified in surefire plugin or passed in command line.

Assuming you are running with command line arguments like,

mvn test -Dbrowser=firefox

then get the parameter by,

import org.testng.annotations.Parameters;

@Parameters("browser")
@Test
public void myTestMethod(String browser){
    System.out.println(browser); 
}

//or as Test parameter
@Test(parameters = {"browser"})
public void myTestMethod(String browser){
    System.out.println(browser);
}

//or System.getProperty() way
@Test
public void myTestMethod(){
    System.out.println(System.getProperty("browser"));
}

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>3.0.0-M3</version>
    <configuration>
      <systemPropertyVariables>
          <browser>firefox</browser>
      </systemPropertyVariables>
      <suiteXmlFiles> 
          <suiteXmlFile>testng.xml</suiteXmlFile>
      </suiteXmlFiles>
     </configuration>
</plugin>

EDIT:

FYI, System.grtProperties() lists all properties, and those set from command line will be there, but there's no way to distinguish those from the other properties added by the system

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM