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

10 comments:

  1. I am really happy with your blog because your article is very unique and powerful for new reader.
    Selenium Training in Chennai | Selenium Training in Bangalore | Selenium Training in Pune

    ReplyDelete
  2. Your articles really impressed for me,because of all information so nice.servicenow training in bangalore

    ReplyDelete
  3. These provided information was really so nice,thanks for giving that post and the more skills to develop after refer that post.opennebula training in bangalore

    ReplyDelete
  4. I gathered a lot of information through this article.Every example is easy to undestandable and explaining the logic easily.openstack training in bangalore

    ReplyDelete
  5. Very useful and information content has been shared out here, Thanks for sharing it.salesforce developer training in bangalore

    ReplyDelete
  6. This is really an awesome post, thanks for it. Keep adding more information to this.vmware training in bangalore

    ReplyDelete
  7. thank you so much for this nice information Article, Digitahanks for sharing your post with us.Real Time Experts training center bangalore

    ReplyDelete
  8. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful . student review for Realtime Experts marathahalli bangalore

    ReplyDelete
  9. I have read your blog its very attractive and impressive. I like it your blog.Real Time Experts Training in Bangalore center address bangalore

    ReplyDelete

Selenium4: Working with two parallel windows in Selenium Automation

A new method newWindow() is introduced in Selenium 4 (Alpha). Using this method we can open a new tab or new window from the current window...