How to read and write data in csv

Hi,

I am exploring Appium since last 15 days.I am working on native app automation using Testng. Currently,I want to read data and write data from csv file but the script is not working

Here is my script

package com.peddle.seller;

import org.testng.annotations.Test;

import org.apache.poi.hslf.model.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;

import java.net.MalformedURLException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.net.URL;
import java.io.File;
import java.io.FileInputStream;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.touch.TouchActions;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.opencsv.CSVReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;

public class PeddleSellercsv {
WebDriver driver;

@BeforeClass
  
  
  public void setUp() throws Exception 
  {
	    DesiredCapabilities capabilities = new DesiredCapabilities();
	    capabilities.setCapability("BROWSER_NAME", "Android");
		capabilities.setCapability("VERSION", "5.0.2"); 
		capabilities.setCapability("deviceName","ZX1G4276JV");
		capabilities.setCapability("platformName","Android");
		capabilities.setCapability("newCommandTimeout","800");
		capabilities.setCapability("appium-version", "1.4.0");
		capabilities.setCapability("appPackage", "com.test.test");
		capabilities.setCapability("appactivity","com.peddle.seller/com.test.test.SplashActivity");
		capabilities.setCapability("appwaitactivity","com.peddle.seller/com.test.test.SellerHomeActivity");
		driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
		
		
		);
		 
  }

@Test
private void GenerateOffer() throws Exception {

 driver.manage().timeouts().implicitlyWait(95, TimeUnit.SECONDS);
 
 WebElement Getyouroffer=driver.findElement(By.name("Get your offer"));
 Getyouroffer.click();

}
public static void main(String[] args) {

	PeddleSellercsv obj = new PeddleSellercsv();
	obj.run();
 
  }

private void run() {
String csvFile = “D:\Detail.csv”;
BufferedReader br = null;
String line = “”;
String cvsSplitBy = “,”;
try {

		br = new BufferedReader(new FileReader(csvFile));
		while ((line = br.readLine()) != null) {
 
		        // use comma as separator
			String[] Year = line.split(cvsSplitBy);
 
			System.out.println("Year");
 
		}
 
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		if (br != null) {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	//System.out.println("Done");
  }

}

Here is my csv file
2012,Acura,MDX,Base

I am not able to read data (Year - 2012) from csv.If any one please help me to solve this.

Please use opencsv.jar which provides good api’s to read and write csv files. If you are using pom.xml and testng structure then add opencsv jar dependancies to pom.xml or you can add it externally as well.

Hi vighnesh,

It would be great, If you can share any example or link.

Thanks,
Gunjan

Hi vighnesh,

Tried with following script but still it is not working

package name;

import org.testng.annotations.Test;

import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;

import java.net.MalformedURLException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.net.URL;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.touch.TouchActions;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.FileNotFoundException;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class PeddleSellercsv {
static WebDriver driver;

@BeforeClass
  
  
  public void setUp() throws Exception 
  {
	    DesiredCapabilities capabilities = new DesiredCapabilities();
	    capabilities.setCapability("BROWSER_NAME", "Android");
		capabilities.setCapability("VERSION", "5.0.2"); 
		capabilities.setCapability("deviceName","ZX1G4276JV");
		capabilities.setCapability("platformName","Android");
		capabilities.setCapability("newCommandTimeout","800");
		capabilities.setCapability("appium-version", "1.4.0");
		capabilities.setCapability("appPackage", "com.peddle.seller");
		capabilities.setCapability("appactivity","com.peddle.seller/com.peddle.seller.SplashActivity");
		capabilities.setCapability("appwaitactivity","com.peddle.seller/com.peddle.seller.SellerHomeActivity");
		driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
		driver.manage().timeouts().implicitlyWait(95, TimeUnit.SECONDS);
	     
		WebElement Getyouroffer=driver.findElement(By.name("Get your offer"));
		 Getyouroffer.click();
	 
		
		 //DesiredCapabilities caps = DesiredCapabilities.android();
		 //caps.setCapability("appiumVersion", "1.4.0");
		 //caps.setCapability("deviceName","Samsung Galaxy S4 Emulator"); 
		 //caps.setCapability("deviceOrientation", "portrait");
		 //caps.setCapability("platformVersion","4.4");
		 //caps.setCapability("platformName", "Android");
		 //caps.setCapability("browserName", "");
		 //caps.setCapability("app","sauce-storage:PeddleSeller.zip");
		 //driver = new RemoteWebDriver(new URL("http://himanshupeddle:[email protected]:80/wd/hub"),caps);
		 
  }

@Test

public static class ReadCVS
{

 public static void main(String[] args) {

	 ReadCVS obj = new ReadCVS();
		obj.run();

	  }

	  public void run() {

		String csvFile = "D:\\Detail.csv";
		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = ",";

		try {

			br = new BufferedReader(new FileReader(csvFile));
			while ((line = br.readLine()) != null) {

			        // use comma as separator
				String[] vehicledetail = line.split(cvsSplitBy);

				//System.out.println("vehicledetail [code= " + country[4] 
	                                // + " , name=" + country[5] + "]");
				
				WebElement Year = driver.findElement(By.name(vehicledetail[0]));
			     Year.click();
			     
				 WebElement Make = driver.findElement(By.name(vehicledetail[1]));
			     Make.click();
			     
				 WebElement Model = driver.findElement(By.name(vehicledetail[2]));
			     Model.click();
			     
				 WebElement Trim = driver.findElement(By.name(vehicledetail[3]));
			     Trim.click();
			     
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		System.out.println("Done");
	  }

}
}

Here is my csv file.

“2012”,“Acura”,“MDX”,“Base”

If you can please look into this and help me how to solve this.

Thanks,
Gunjan.

What error it is giving you?
you can take reference of below link for sample example
http://opencsv.sourceforge.net/

Hi,
There is quite change in my requirement. Now based on latest update I have to read input data from excel sheet and write output into excel sheet.

Have you any idea!!