问题
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}"
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/38634335/is-it-possible-to-pass-property-file-value-to-testng-xml-as-parameter-value