Wednesday, August 8, 2018

How to pass parameters from testng.xml?

We can pass test data as parameters from testng.xml to the tests directly using TestNG Parameters feature.

To pass any parameter from testng.xml we can define "parameter" tag at suite, test and class level in testng.xml. Syntax is <parameter name="name_of_parameter", value="value_of_parameter"/>. We can add any number of parameters for a test. Please see the below screenshot.

To use the parameters in the test methods, we have to use @Parameters annotation provided by TestNG. And @Parameters annotation can be used with the methods that has @Test, @BeforeXXXX, @AfterXXXX or @Factory annotations. Inside @Parameters annotation we have to give the name of the parameters and these names must be same as what we have given in the testng.xml as shown in the below screenshot.

This how the testng.xml looks like after adding the parameters.

If we run the test method from the class, it will throw an exception. Because the test method is associated with parameters that are defined in the testng.xml file. The test method will not be able to understand what are the parameters.

So we should run the below test using testng.xml and see the result, we have not written selenium code in the test case. Here our main focus is to check how to parameterize the test using testng.xml, that's why are just printing the parameters.

Check the below result, the test case has received the values from the testng.xml and printed in console.

This is how we parameterize the test using testng.xml, generally we pass data like browser type, application url, user credentials and etc from the testng.xml file. We do not prefer to send large in this approach.

Practically this approach is less used, testers use configuration files to maintain such data.

Thanks for reading. Please comment your queries or suggestions on this post. #HappyLearning #HappyTesting #qababu

1 comment:

DevOps 01: What is DevOps and How it benefits organizations?

DevOps is a culture in an organization, where the development team and operations team help each other by sharing information, process and t...