1

I have a maven project. Is there a way to read attributes of pom file from the TestNg Xml file for example I want to read the version of the app from the pom file and then pass it down to my test from the TestNG xml file using @Parameter annotation.

So far I have tried passing the pom attribute directly as value in the TestNG xml file but it does not fetch the value from the pom. Instead, it prints the pom attribute.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name ="Implementing Parametrization">
<test name ="Testing Functionality">
<parameter name = "browser" value = "${project.version}" />
<parameter name = "username" value = "test@gmail.com" />
<parameter name = "password" value = "abc@xyz123" />
<classes>
    <class 
name="it.org.seleniumtests.Parametrization.GenericHomePage"/>
</classes>
</test>
</suite>

After printing the values in the test: Expected result: 1.2.0 and Actual result: ${project.version}

I know I can do it at as JVM arguments as I explained here: https://rationaleemotions.wordpress.com/2017/09/29/dynamic-parameterization-in-testng/ but this is not what I want to achieve. I already have the value I need in the pom file. I want to fetch it in my TestNG xml file so I can pass it down to my tests as a parameter.

1 Answers1

0

Note: Leveraging this approach will take away your ability to run your testng suite xml file directly from within the IDE (without invoking a maven related goal from within your IDE)

Yes, you can do this as shown below:

Read more here and also this stackoverflow answer.

You need to leverage the maven resources plugin and enable filtering on it so that Maven starts replacing variables with actual values.

Add the following to your <build> tag

<testResources>
  <testResource>
    <directory>src/test/resources</directory>
    <filtering>true</filtering>
  </testResource>
</testResources>

Here's how your surefire plugin entry would look like (Notice that we aren't referring to the testng suite xml file from src/test/resources because that would contain the suite file with placeholders, but we need the suite file that contains the actual values as replaced by maven, which is available in the folder as denoted by the value of ${project.build.testOutputDirectory}

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M1</version>
    <executions>
      <execution>
        <phase>test</phase>
      </execution>
    </executions>
    <configuration>
      <suiteXmlFiles>
        <suiteXmlFile>${project.build.testOutputDirectory}/testng.xml</suiteXmlFile>
      </suiteXmlFiles>
    </configuration>
  </plugin>

Here's the testng suite xml file

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Implementing Parametrization">
  <test name="Testing Functionality">
    <parameter name="version" value="${project.version}"/>
    <classes>
      <class
        name="com.rationaleemotions.IgetInputFromMavenResources"/>
    </classes>
  </test>
</suite>

Here's the test class

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

public class IgetInputFromMavenResources {

  @Test
  @Parameters("version")
  public void testMethod(String projectVersion) {
    System.err.println("Project version " + projectVersion);
  }

}

Here's the execution output

09:27 $ mvn clean resources:testResources test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.rationaleemotions:playground >------------------
[INFO] Building playground 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ playground ---
[INFO] Deleting /Users/krmahadevan/githome/PlayGround/playground/target
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-cli) @ playground ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ playground ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/krmahadevan/githome/PlayGround/playground/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ playground ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/krmahadevan/githome/PlayGround/playground/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ playground ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ playground ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 8 source files to /Users/krmahadevan/githome/PlayGround/playground/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M1:test (default-test) @ playground ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
Project version 1.0-SNAPSHOT
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.324 s - in TestSuite
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.441 s
[INFO] Finished at: 2019-08-02T09:28:15+05:30
[INFO] ------------------------------------------------------------------------

PS: The blog that you have called out is mine :)

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
  • Yes, I know very well that its your blog :) I am glad there are people like you who are helping out the community on a good will. We need more people like you. And thank you for your solution. It does actually work but I don't want to take away my ability to run my testng suite xml file directly from within the IDE. I actually found a workaround for it. – Gagandeep Singh Aug 02 '19 at 04:25
  • Sorry splitting in two part as the character limit reached :D I created my own ITestListener which reads the value from the right pom file(as I have interlinked projects with shared tests) which creates a dynamic parameter and assign the value to the test, all in the run time. The bit I was not able to achieve was after fetching the value from the pom inside the listener, I was not able to set/override the value of the parameter(If you can share this, that would be great). – Gagandeep Singh Aug 02 '19 at 04:26
  • `` will work? Expecting test name dynamically changes every runs. – deokyong song Mar 07 '22 at 02:28
  • @deokyongsong - Maybe you can use resource filtering capabilities of your build tool (Maven has such capability) and see if that helps? – Krishnan Mahadevan Mar 08 '22 at 11:19