Hello Folks,
As part of ongoing series on DataProvider, we will learn “How does Encapsulation ( in OOPS concept) help in a DataProvider method? ” in this post.
Encapsulation is an Object Oriented Programming concept which describes binding of related stuffs together under one roof. It makes easier to add , remove and modify members.
For example: A Student has its name, age, gender, subject , address ( data members) and they can perform activities like Study, Playing etc (Member functions) . All these can be clubbed and kept under a roof called Student class. You can have as many as student object you want and all object will carry same properties. If you need to add or remove a field or method, you can easily do it.
Encapsulation concept is very useful if you are trying to achieve data driven testing using DataProvider methods in TestNG. We will see this concept in detail here.
Suppose you want to register 10 students with three different details like FirstName, LastName and Email_ID. You created a DataProvider method and its calling Test method as below:
package DataProvider; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class DataProviderBasics3 { // You must need to mention data provider method name in Test method @Test(dataProvider="DataContainer") public void studentRegistration(String First_name, String Last_Name, String Email_ID) { System.out.println("Registered student with details: "+First_name+" "+Last_Name+" "+Email_ID); } // A data provider method with return type as 2D array @DataProvider(name="DataContainer") public Object[] myDataProvider() { return new Object[][] { {"Mukesh", "Otwani", "Motwani@gmail.com"}, {"Amod","Mahajan","Amahajan@hotmail.com"}, {"Animesh","Prashant","aprashant@gmail.com"}, {"Ankur","Singh","asingh@gmail.com"}, {"Amritansh", "Kumar","akumar@gmail.com"} }; } }
Now you got to know that, you need to pass an extra field “Gender” to register a student. To achieve this, you need to do modification in DataProvider method along with its caller Test method to match number of arguments. Just imagine if there are more number of fields to be added or removed. It will become worse if it happens frequently.
What is the best solution to handle this pain? Answer is Encapsulation.
We will create a class named “Student” and define properties in this. We can also create some methods which can be used to do some validation, data getter and setter during registration. A sample java code is below:
package DataProvider; public class Student { String name; int age; String gender; public Student(String name, int age, String gender) { this.name= name; this.age=age; this.gender=gender; } // A method to check if student is eligible for registration public boolean validateAge() { if(this.age>2) { System.out.println("You are eligible to do registration."); return true; } else return false; } }
A caller Test method and its DataProvider method are as below:
Below DataProvider method returns an Object. I am not specifying any list of parameters. This solves our problem as if we need to add or remove a new field, we do not need to modify anything to match number of arguments in caller Test method.
package DataProvider; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class StudentDataProvider { // A data provider method with return type as 2D array @DataProvider(name = "DataContainer") public static Student[] myDataProvider() { return new Student[] { new Student("Amod", 4, "Male"), new Student("Neha", 1, "Female") }; } // You must need to mention data provider method name in Test method @Test(dataProvider = "DataContainer") public void methodWithSingleAttribute(Student a) { if(a.validateAge()) { System.out.println("Student registered with details as Name = "+a.name + " , Age ="+a.age +" , Gender ="+a.gender); } else { System.out.println("Student not registered with details as Name = "+a.name + " , Age ="+a.age +" , Gender ="+a.gender); } } }
Output:
[RemoteTestNG] detected TestNG version 6.14.2 You are eligible to do registration. Student registered with details as Name = Amod , Age =4 , Gender =Male Student not registered with details as Name = Neha , Age =1 , Gender =Female PASSED: methodWithSingleAttribute(DataProvider.Student@544fe44c) PASSED: methodWithSingleAttribute(DataProvider.Student@18eed359) =============================================== Default test Tests run: 2, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 2, Failures: 0, Skips: 0 ===============================================
If you need to add a new field or remove a field, you will get less headache to do so as you don’t need to update all Test method and very less changes in DataProvider method. You can easily add new validation or methods and use it whatever you want.
The biggest problem with a DataProvider method is that you must need to match number of arguments returned by DataProvider method and its caller Test method. If it is not same, an exception is thrown. Using above concept number of argument dependency can be eliminated.
More about TestNG in upcoming posts. Stay tuned.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium
My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning
Perfect Number: A Perfect number is a positive integer that is equal to the sum of its proper divisors excluding the number itself.…
Problem Statement: Find occurrence of given char (Ignore case) in given string without iterating through string. Input - Make Selenium Easy…
Problem Statement: Write a program to print occurrence of each character in given string without using Collection. Example:- Please enter…
Hello Folks, In last post, we have learnt about new annotation of TestNG , named @Ignore which is used to…
Hello Folks, TestNG provides an attribute called "enabled" which can be used to ignore a test in a running suite.…
Hello Folks, We have already learnt about : Sending GET request in Postman. Sending POST request in Postman. If you…