I found out that there are 2 ways to read parameters in testng xml configuration file:
use @Parameter
annotation and define parameter names after the annotation
use ITestContext
as an argument for the test class, DataProvider or Factory and use getCurrentXmlTest()
and getParameter()
to read the parameter.
Is any of these ways preferred for any reason on the other one? and is there any other way to do it?
I think the difference is the scope. If I would need the same parameter across all my suite of tests for eg. env variables, I would use it in a listener implementing ITestListener or ISuiteListener (for suite scope) and then use the ITestContext/Suitecontext to apply the value across all my tests under <test>
or under <suite>
However, if I need very specific params for specific testcases, then I would go with @Parameter annotation on those particular testcases.
Also, the @Parameter can only take simple values, so there may be usecases where your tests need different objects (rather than strings) based on some param value. In which case I would prefer a dataprovider, reading the param value( your second option, though argument for testclass i think is incorrect), creating objects and then feeding it to the tests. (for eg. probably name of a properties file is a param value, which is read and objects created based on data in the properties file and fed as different testdata to tests).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With