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

我是研究僧i 提交于 2021-02-19 02:03:44

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!