Monday, April 20, 2020

TestNG Tutorial Advanced Topics - Part 4

TestNG Parameters are the arguments that we pass to the test methods. 

Parameterisation is always helpful in any application to store the key values into the file and call it whenever required across the application, there are two ways through which we can pass the parameters to the test methods:


  • TestNG Parameters
  • TestNG DataProviders


In this topic, we will learn about the TestNG Parameters. We will learn about the parameterisation in the xml file.

Suppose we want to set the global variables such url settings, username, password or API Keys, there are some values which are constant in all the test cases, in such case we use the TestNG Parameters.

1> TestNG @Parameters – test parameters with testng.xml

You already know about testng.xml File. If not then Click Here.

Now, we need to store username and password in testng.xml file and need to pass it to class called 'Parameterized_Keyword'.

So we have added two parameter tags here in testng.xml.




Now, Below way we can call these two parameters using @Parameters annotation in Parameterized_Keyword.Java class.





Note: testng.xml parameter name should match with name we are passing into class file under @Parameter annotation to call it.

😀 TIME TO IMPLEMENT the Same IN YOUR ECLIPSE NOW 😀

2> TestNG @DataProvider – dataProvider in the class itself

A test method that uses DataProvider will be executed a multiple number of times based on the data provided by the DataProvider

The test method will be executed using the same instance of the test class to which the test method belongs.

Marks a method as supplying data for a test method. The annotated method must return an Object[ ][ ], where each Object[ ] can be assigned the parameter list of the test method. 

The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.

Lets understand this by example practically.

We have one TestNG Class, Which contains below @DataProvider having name=SearchProvider.



It contains two dimensional Object array to pass the element values . Hence return values show the same.

This will pass the value to @Test method one by one.

Now, we have one @Test method in the same TestNG class. We pass the dataProvider using the same name as we have given in @DataProvider to pass the values one by one.



Below is the result when we run this code. Console & TestNG Result.




😀 TIME TO IMPLEMENT the Same IN YOUR ECLIPSE NOW 😀


~~~~ Other TestNG Related Tutorial Links~~~~

TestNG Tutorial - Introduction Part

TestNG Tutorial Advanced Topics - Part 1 (Configuration of TestNG)

TestNG Tutorial Advanced Topics - Part 2 (TestNG.XML File)

TestNG Tutorial Advanced Topics - Part 3 (Priority, Groups, Parallel Mode, invocationCount, ThreadPool)

TestNG Tutorial Advanced Topics - Part 4 (Parameterization, Data Provider)

TestNG Tutorial Advanced Topics - Part 5 (Exclude,Listeners,DependsOnMethod)



No comments:

Post a Comment