Using both @DataProvider and @Parameters
I would like to know if there is a way to use both @DataProvider
and Paramaters
to pass parameters.
I tried two options, but both failed:
@Parameters("Brand")
@Test(dataProvider="dpCGA", groups={"CGA"})
public void createAccount(String brand) {
setBrand(brand);
}
In the above example, the data provider is overwritten with a variable.
@Parameters("Brand")
@Test(dataProvider="dpCGA", groups={"CGA"})
public void createAccount(String brand, String email) {
setBrand(brand);
createAccount(email);
}
The test drive didn't even start.
I am using TestNG to run my test cases and want to grab a brand parameter from an XML file.
Also, I have an Excel file that I am using to store my email and I want to get these values using @DataProvider
.
Can both of these tags be used together? If not, is there any other way to grab the brand parameter from the XML file?
Thank you in advance
source to share
@DataProvider is one way to pass parameters to a method. You cannot use both methods for the same method.
Looking at your question, you can simply add the brand to the DataProvider method, for example
@DataProvider(name="dpCGA")
public Object[][] data() {
return new Object[][] {
{"brand", "email1"},
{"brand", "email2"}
};
}
and pass it to the method,
@Test(dataProvider="dpCGA", groups={"CGA"})
public void createAccount(String brand, String email) {
setBrand(brand);
createAccount(email);
}
source to share
As already noted, DataProvider
it is not possible to get results together with the set parameters. This answer confirms this by stepping through the code:
-
Invoker.invokeTestMethods()
will causeParameters.handleParameters()
. - There he will try to find
DataProvider
- If found, it will be called. And through it after that the set parameters are not added, it passes the context to
DataProvider
- If not found, it resorts to
xmlParameters
- If found, it will be called. And through it after that the set parameters are not added, it passes the context to
While it DataProvider
can get the context using ITestContext
, it only makes the parameters available in the set (XML) not set using system properties.
source to share