Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass property file value to testng.xml as parameter value?

Tags:

maven

testng

Is it possible to pass property file value to testng.xml as parameter value?

Example:

test.properties:

browser = FIREFOX

testng.xml:

parameter name = "browser" value = "${test.browser}"
like image 583
Rey Guballo Avatar asked Oct 18 '22 04:10

Rey Guballo


2 Answers

Yes, this is possible by filtering the testng.xml test resource.

Inside your POM, you need to declare a filter poiting to your properties files and override the <testResources> to filter the testng.xml resource.

<build>
  <filters>
    <filter>path/to/test.properties</filter> <!-- relative to location of POM -->
  </filters>
  <testResources>
    <testResource>
      <directory>src/test/resources</directory>
      <includes>
        <include>testng.xml</include>
      </includes>
      <filtering>true</filtering>
    </testResource>
    <testResource>
      <directory>src/test/resources</directory>
      <excludes>
        <exclude>testng.xml</exclude>
      </excludes>
    </testResource>
  </testResources>
  <!-- rest of build section -->
</build>

If you properties file contains a key test.browser, this will correctly replace the plaholder ${test.browser} by the value of that key. All of this will happen before the tests are launched so TestNG will see the filtered file with the values replaced.

Note that the above configuration only filters testng.xml to avoid filtering too much. You could extend that to filter multiple files.

like image 149
Tunaki Avatar answered Oct 21 '22 06:10

Tunaki


It is not possible out of the box, but you should be able to something with a IAlterSuiteListener where you'll replace values by what you want.

like image 39
juherr Avatar answered Oct 21 '22 06:10

juherr