Selenium | Data Driven Framework to read csv data

|
| By Webner

During testing, I had a test case to add multiple employees automatically with selenium script so I have implemented Data Driven Framework. Using this we have to read the data from the CSV and register all the users in the system.

Note:  We need to add a jar file into our project, you can download that jar file from here:

https://sourceforge.net/projects/opencsv/

This is registration form:
21
This is sample csv with data:
22
Here is the code for reading csv:

public static void main(String[] args) throws Exception {
WebDriver driver = new FirefoxDriver();
driver.manage().window().setSize(new Dimension(1920,1080));
String appurl = "http://payroll.webnerserver.com/View/Login.php";
driver.get(appurl);
Thread.sleep(5000);
driver.findElement(By.xpath("/html/body/div[2]/div/div/form/div/input[1]")).sendKeys("sumit5@webners.com");	 driver.findElement(By.xpath("/html/body/div[2]/div/div/form/div/input[2]")).sendKeys("Sumit0!!"); 
driver.findElement(By.xpath("/html/body/div[2]/div/div/form/div/input[3]")).click(); Thread.sleep(3000);	 driver.findElement(By.xpath("id('page-wrapper')/div[2]/div[2]/div/div[2]/a[1]/span[1]")).click();    
CSVReader reader = new CSVReader(new FileReader("/home/sumit/Desktop/Payroll/Add_an_employeeCsv.csv"));
// this will load content into list
List<String[]> li=reader.readAll();
System.out.println("Total rows which we have is "+li.size());
//create Iterator reference
Iterator<String[]>i1= li.iterator();
// Iterate all values
While (i1.hasNext()){
String[] str=i1.next();
driver.findElement(By.id("empname")).sendKeys(str[0]);
driver.findElement(By.id("empnumber")).sendKeys(str[1]);
Select select = new Select(driver.findElement(By.id("dept")));
select.selectByVisibleText(str[2]);
driver.findElement(By.id("address")).sendKeys(str[3]);
driver.findElement(By.id("email")).sendKeys(str[4]);
driver.findElement(By.id("pass")).sendKeys(str[5]);
driver.findElement(By.id("addEmployee")).click();
Thread.sleep(2000);
driver.navigate().refresh();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *