Blogs

Data Driven Selenium Automation Testing – Part 2 (CSV Data with Junit 5)

In the previous article “Data Driven Selenium Automation Testing – Part 1 (with Junit 5)” , we have learnt how we can perform data driven testing with Junit5. The data driven feature was provided by data provider but data was maintained in the Selenium Java Code itself. In this article, we would be extending previous article’s problem statement by trying to decouple data from the code by maintaining data in CSV file format.

Data Driven Testing is the need of the hour, and the most convenient way to get data is through data files. Data Files may be in various formats but most commonly used formats are:

  • CSV format (*.csv files)
  • MS Excel Workbook (*.xls or *.xlsx files)
  • XML Format (*.xml files)
  • JSON format(*.json files)

 The addendum to the Problem statement is:

“Given with a data set comprising of URLs to visit and verify the corresponding Page Titles using Data Driven features of Junit 5”

pom.xml

In order to read csv data file, we’d be adding a maven dependency of opencsv in pom.xml

We have updated the static data provider method dp(). It takes help of a static method

public static String[ ][ ] getCSVData(String fileName, char sep)

from a utility class CSVDataReader as shown below:

The csv data file (urls_titles_data.csv) contains sample data as follows

The getCSVData() method takes CSV Data file path and a separator as arguments. It reads line by line till the end of file and stores each line into arraylist and thus a two dimensional String array is populated, which is returned back to the caller method.

Rest of the Implementation is same as done in the previous article. And we can run this as a Junit 5 test and it is something like below

And the JUnit summary panel displays the test result summary for passed / failed tests and their execution time as well

Conclusion

We could decouple Data in CSV File format from the Java Code and making a true Data Driven approach. The code is available at following

In the next article, we would try to leverage Data in XL format.

*****

*******

Leave a Comment