我已经编写了从 CSV 文件读取内容(用户名和密码)的代码。现在,我需要将这些内容写入另一个 CSV 文件。请帮我提供一个好的建议。
public class NewCsvClass {
// private static final String FILE_HEADER = "Username,Password,Result";
// private static final String NEW_LINE_SEPARATOR = "\n";
public static void main( String[] args) throws Exception {
// csv reader aswathy -start
CSVReader reader = new CSVReader(new FileReader("/home/user/Documents/UrmilaDocs/CSV PAck/testCSV.csv"));
String[] nextLine;
WebDriver driver = new FirefoxDriver();
String appUrl = "https://accounts.google.com";
driver.get(appUrl);
driver.manage().window().maximize();
while ((nextLine = reader.readNext()) != null) {
String user_name = nextLine[0];
String pass_word = nextLine[1];
System.out.println("Username: " + user_name);
System.out.println("Password: " + pass_word);
// //stackoverflow
// WebDriver driver = new FirefoxDriver();
// String appUrl = "https://accounts.google.com";
// driver.get(appUrl);
// driver.manage().window().maximize();
WebElement username = driver.findElement(By.xpath(".//*[@id='Email']"));
username.clear();
username.sendKeys(user_name);
driver.findElement(By.xpath(".//*[@id='next']")).click();
Thread.sleep(5000);
// try
try {
WebElement password = driver.findElement(By.xpath(".//*[@id='Passwd']"));
password.clear();
password.sendKeys(pass_word);
driver.findElement(By.xpath(".//*[@id='signIn']")).click();
Thread.sleep(8000);
// click on 'Google Apps' icon
driver.findElement(By.xpath(".//*[@id='gbwa']/div[1]/a")).click();
Thread.sleep(10000);
// Click on 'Gmail' icon to navigate to inbox page
driver.findElement(By.xpath(".//*[@id='gb23']/span[1]")).click();
Thread.sleep(10000);
System.out.println("Login Success");
// Click on user name first letter circle icon
driver.findElement(By.xpath(".//*[@id='gb']/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")).click();
Thread.sleep(5000);
// click on 'Signout' button
driver.findElement(By.xpath(".//*[@id='gb_71']")).click();
Thread.sleep(10000);
System.out.println("Logout Success");
/*
* click on 'Signin with a different account ' option (since, after signing out from // the first user,
* the page is navigated to password entry page, which is supposed to navigate to // username/mailid
* entry page
*/
try {
driver.findElement(By.xpath(".//*[@id='account-chooser-link']")).click();
// driver.findElement(By.xpath(".//*[@id='link-signin-different']/a")).click();
Thread.sleep(10000);
} catch (Exception e) {
}
// /* In 'Choose an Account page', Click on 'Add Account' button */
driver.findElement(By.xpath(".//*[@id='account-chooser-add-account']")).click();
Thread.sleep(10000);
//
} catch (Exception e) {
System.out.println("Login failed!");
driver.findElement(By.xpath(".//*[@id='link-signin-different']/a")).click();
Thread.sleep(10000);
driver.findElement(By.xpath(".//*[@id='account-chooser-add-account']")).click();
Thread.sleep(10000);
} // catch closed
// //closing driver & firefox
// driver.close();
// System.exit(0);
// end
} // while end
// //closing driver & firefox
// driver.close();
// //csv reader aswathy -end
System.exit(0); // closing firefox
// Writing the CSV file to another CSV file
String csv = "/home/user/Documents/UrmilaDocs/CSV PAck/testCSV.csv";
CSVWriter writer = new CSVWriter(new FileWriter(csv));
String[] username = "testuser01.zyx#testuser07#testuser2#testuser01.zyx#tester2.zyx#testernew779".split("#");
String[] password = "testuser0123#user0123#user0123#user0123#user0123#user0123".split("#");
writer.writeNext(username);
writer.writeNext(password);
writer.close();
}
}
我没有看到导入语句,但您使用的是 com.csvreader.CsvWriter,即 JavaCSV API?
如果是这样,请尝试插入
writer.endRecord();
之前
writer.close();
数据对于执行测试用例起着非常重要的作用,利用它我们可以使用不同的测试数据进行数据质量测试。 为了执行这些操作,我们需要 Excel 或 csv 来存储数据。
OpenCSV 是一个用于读取数据并将数据写入 Excel 的库。
下面是向excel写入数据的代码:
文件 file = new File(filePath);
try {
// create FileWriter object with file as parameter
FileWriter outputfile = new FileWriter(file);
// create CSVWriter object filewriter object as parameter
CSVWriter writer = new CSVWriter(outputfile);
// Add header to csv
String[] header = { "StudentName", "Class", "ID" };
writer.writeNext(header);
// Add data to csv
String[] data1 = { "Arman", "10", "1" };
writer.writeNext(data1);
String[] data2 = { "Sanjay", "10", "2" };
writer.writeNext(data2);
// closing writer connection
writer.close();
}