Reading data from a CSV file

We saw a simple data-driven test TestNG. The test data was hardcoded in the test-script code. This could become difficult to maintain. It is recommended that we store the test data separately from the test scripts. Often, we use data from the production environment for testing. This data can be exported in the CSV format. We can read these CSV files in data-provider methods and pass the data to the test instead of hardcoded object arrays.

In this example, we will use the OpenCSV library to read a CSV file. OpenCSV is a simple Java library for reading CSV files in Java. You can find more details on OpenCSV at http://opencsv.sourceforge.net/.

Let's first create a CSV file, named data.csv, in the src/test/resources/data ...

Get Selenium WebDriver 3 Practical Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.