Skip to content

Instantly share code, notes, and snippets.

@pragmatictesters
Created December 31, 2018 19:11
Show Gist options
  • Save pragmatictesters/66d8e3e14aebf0b95a46e0f9ef303405 to your computer and use it in GitHub Desktop.
Save pragmatictesters/66d8e3e14aebf0b95a46e0f9ef303405 to your computer and use it in GitHub Desktop.
Selenium WebDriver Examples : Reading test data from a CSV file and use them in a test method
package com.pragmatic.selenium.examples;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.*;
import java.io.FileReader;
import java.io.Reader;
/**
* This class demonstrates reading test data from a CSV file
**/
public class ReadDataFromCSV {
private WebDriver driver;
@BeforeClass
public void beforeClass() {
WebDriverManager.chromedriver().setup();
}
@BeforeMethod
public void beforeMethod() {
driver = new ChromeDriver();
driver.get("http://hrm.pragmatictestlabs.com");
driver.manage().window().maximize();
}
@AfterMethod
public void afterMethod() {
driver.close();
}
/**
* Demonstrate use of test data from a CSV file in a test method
*
* @param loginData
*/
@Test(dataProvider = "login-data-csv")
public void testLoginData(String[] loginData) {
String username = loginData[0];
String password = loginData[1];
String expected = loginData[2];
String msgError;
driver.findElement(By.id("txtUsername")).sendKeys(username);
driver.findElement(By.id("txtPassword")).sendKeys(password);
driver.findElement(By.id("btnLogin")).click();
msgError = driver.findElement(By.id("spanMessage")).getText();
Assert.assertEquals(msgError, expected);
}
/**
* Reading test data from a CSV file
*
* @return
* @throws Exception
*/
@DataProvider(name = "login-data-csv")
public Object[][] readCSVData() throws Exception {
String[][] testData;
//Get the workbook
Reader fileInputStream = new FileReader("testData/login-data.csv");
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(fileInputStream);
int numberOfRecords = 0;
int numberOfColumns = 0;
for (CSVRecord record : records
) {
System.out.println("Reading record line #" + ++numberOfRecords);
numberOfColumns = record.size();
}
testData = new String[numberOfRecords - 1][numberOfColumns];
System.out.println("numberOfRecords = " + numberOfRecords);
System.out.println("numberOfColumns = " + numberOfColumns);
int currentRecord = 0;
fileInputStream = new FileReader("testData/login-data.csv");
records = CSVFormat.EXCEL.parse(fileInputStream);
for (CSVRecord record : records
) {
System.out.println("Reading test data ");
if (record.getRecordNumber() == 1) {
System.out.println("record = " + record);
continue;
}
for (int i = 0; i < record.size(); i++) {
testData[currentRecord][i] = record.get(i);
}
currentRecord++;
}
return testData;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment