In this tutorial, we are going to cover below points:
Let’s begin:
1. How to Setup OpenCSV library in a Project
In order to start the implementation of Data driven testing using CSV as your data source, we need to set up the environment first. For this reason, this tutorial will explain and help to set up the prerequisite and required dependencies in Eclipse.
Maven based Project:
If you are using Maven as the build tool for you project, use below mentioned dependency and add it into the POM file:
1 2 3 4 5 | <dependency> <groupId>com.opencsv</groupId> <artifactId>opencsv</artifactId> <version>5.1</version> </dependency> |
OR
If you are using standalone project then download the OpenCSV jar from this location and add it into your project’s class-path.
2. Prepare Test Data CSV
Create a CSV format Test data file that you are going to use in your test. Download TestDataCSV as this we are using in this example.
3. How to implement Data Driven testing using CSV
Now, as you’ve added the dependency into the project and Test data CSV file is ready too, now, use the below code to perform data read through CSV.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | import java.io.FileReader; import java.io.IOException; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import com.opencsv.CSVReader; import com.opencsv.exceptions.CsvValidationException; import io.github.bonigarcia.wdm.WebDriverManager; public class DataDrivenTestingUsingCSVInSelenium { //Provide test data CSV file path. As below path based on Mac machine. So, lets say you are using windows machine then write the below path accordingly. String CSV_PATH = "/Users/d33p4k/Documents/Techndeck/JavaPractice/TestData.csv"; WebDriver driver; private CSVReader csvReader; String[] csvCell; @BeforeTest public void setup() throws Exception { //You can specify the hardcoded value of a chrome driver or driver based on your browser like below line //System.setProperty("webdriver.chrome.driver", "/Users/d33p4k/driver/chromedriver"); //OR //Use below line to manage driver by WebdriverManager for chrome browser in our case (you can use any other driver of your choice) WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://amazon.com"); } @Test public void dataRead_CSV() throws IOException, CsvValidationException { //Create an object of CSVReader csvReader = new CSVReader(new FileReader(CSV_PATH)); driver.findElement(By.id("nav-link-accountList")).click(); driver.findElement(By.id("createAccountSubmit")).click(); //You can use while loop like below, It will be executed until the last line in CSV used. while ((csvCell = csvReader.readNext()) != null) { String CustomerName = csvCell[0]; String CustomerEmail = csvCell[1]; String CustomerPassword = csvCell[2]; String CustomerConfirmPassword = csvCell[3]; driver.findElement(By.id("ap_customer_name")).clear(); driver.findElement(By.id("ap_customer_name")).sendKeys(CustomerName); driver.findElement(By.id("ap_email")).clear(); driver.findElement(By.id("ap_email")).sendKeys(CustomerEmail); driver.findElement(By.id("ap_password")).clear(); driver.findElement(By.id("ap_password")).sendKeys(CustomerPassword); driver.findElement(By.id("ap_password_check")).clear(); driver.findElement(By.id("ap_password_check")).sendKeys(CustomerConfirmPassword); driver.findElement(By.id("continue")).click(); } } @AfterTest public void exit() { driver.close(); driver.quit(); } } |
Check below video to see the execution in progress:
That’s it. So easy to implement it.
Happy Learning!!!
4. Resources
Data Driven Approach – Elemental Selenium
Read Data From CSV In Selenium Webdriver...!!! Share on XCheckout other useful tutorials, take a look: