Java Selenium – CSV UTF-8格式文件创建在Salesforce中无法正常工作

huangapple go评论80阅读模式
英文:

Java Selenium - CSV UTF-8 format file creation is not working for Salesforce

问题

我面临着一个独特的挑战。我的应用程序基于SalesForce,我需要创建一个列表(名字,姓氏,电子邮件),然后上传到应用程序中。我正在使用OpenCSV生成文件并上传,但应用程序无法识别。

手动操作步骤:

  1. 下载开发人员提供的模板,格式为.xls Unicode格式。
  2. 用Microsoft Excel打开。
  3. 填充数据并保存为CSV UTF-8(逗号分隔)(*.csv)。

在手动修改当前模板时,它能够正常工作。

自动化方法:

  1. 使用Java和OpenCSV的帮助,复制文件以使用相同的模板。
  2. 填充数据。
  3. 刷新文件。
  4. 复制文件以处于CSV格式。

但当我上传文件时,应用程序不接受。也没有收到任何错误消息。

请查看下面是我当前拥有的代码。任何帮助将不胜感激。

public void prepareCSVfile(int numberOfRecords) throws IOException {

    String filePath = Constants.CONFIGURATION_PATH + "csv\\" + Constants.RUNNAME + ".xls";
    String fileName = Constants.CONFIGURATION_PATH + "csv\\" + Constants.RUNNAME + ".csv";
    File source=new File(Constants.CONFIGURATION_PATH + "csv\\" +"Template.xls");
    File destination=new File(filePath);

    copyFile(source,destination);
    
    CSVWriter writer = new CSVWriter(new OutputStreamWriter(new FileOutputStream(filePath, true), "UTF-8"), CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER,CSVWriter.DEFAULT_LINE_END);

    for (int i = 0; i < numberOfRecords; i++) {
        String firstName = Commons.getRandomAlphabet(4).toUpperCase();
        String lastName = Commons.getRandomAlphabet(4).toUpperCase();
        String emailAddress = Commons.getRandomAlphabet(8).toUpperCase() + "@email.com";

        String lines[] = { firstName, lastName, emailAddress };
        writer.writeNext(lines);
        System.out.println(lines);
    }

    // Flushing data from writer to file
    writer.flush();
    writer.close();
    copyFile(new File(filePath),new File(fileName));
    System.out.println("Data entered");
}
英文:

I am facing a unique challenge. My application is based on SalesForce, where I have to create a list (First Name, Last Name, Email) and upload in the application. I am using OpenCSV to generate the file and uploading but application is not recognizing.

Manually How it works:

  1. Download template provided by developers which is .xls unicode format
  2. Open with Microsoft Excel
  3. Populate data and save as CSV UTF-8 (Comma delimited)(*.csv)

It works perfectly fine while modifying current template manually.

Automation Approach:

  1. With help of Java and OpenCSV copying the file to use the same template
  2. Populate data
  3. Flush file
  4. Copy file to be in CSV format

But when I upload the file, application is not accepting. Not getting any error message as well.

Please find below code that I have currently. Any help will be appreciated.

	public void prepareCSVfile(int numberOfRecords) throws IOException {

	String filePath = Constants.CONFIGURATION_PATH + &quot;csv\\&quot; + Constants.RUNNAME + &quot;.xls&quot;;
	String fileName = Constants.CONFIGURATION_PATH + &quot;csv\\&quot; + Constants.RUNNAME + &quot;.csv&quot;;
	File source=new File(Constants.CONFIGURATION_PATH + &quot;csv\\&quot; +&quot;Template.xls&quot;);
	File destination=new File(filePath);

	copyFile(source,destination);
	
	CSVWriter writer = new CSVWriter(new OutputStreamWriter(new FileOutputStream(filePath, true), &quot;UTF-8&quot;), CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER,CSVWriter.DEFAULT_LINE_END);

	for (int i = 0; i &lt; numberOfRecords; i++) {
		String firstName = Commons.getRandomAlphabet(4).toUpperCase();
		String lastName = Commons.getRandomAlphabet(4).toUpperCase();
		String emailAddress = Commons.getRandomAlphabet(8).toUpperCase() + &quot;@email.com&quot;;

		String lines[] = { firstName, lastName, emailAddress };
		writer.writeNext(lines);
		System.out.println(lines);
	}

	// Flushing data from writer to file
	writer.flush();
	writer.close();
	copyFile(new File(filePath),new File(fileName));
	System.out.println(&quot;Data entered&quot;);
}

答案1

得分: 0

I have found a solution. While writing the file openCSV is not a good option. I am using Apache commons csv and it's working fine.

Here is updated working code.

String filePath = Constants.CONFIGURATION_PATH + "csv\\" + Constants.RUNNAME + ".csv";
File source=new File(Constants.CONFIGURATION_PATH + "csv\\" +"MyTemplate.csv");

CSVPrinter writer = new CSVPrinter(new FileWriter(filePath), CSVFormat.EXCEL);
writer.printRecord("First Name","Last Name","Email");
for (int i = 0; i < numberOfRecords; i++) {
    
    List<String> nextLine = new ArrayList<>();
    nextLine.add(Commons.getRandomAlphabet(6).toUpperCase());
    nextLine.add(Commons.getRandomAlphabet(6).toUpperCase());
    nextLine.add(Commons.getRandomAlphanumeric(10).toUpperCase() + "@email.com");
    
    writer.printRecord(nextLine);
}
    
writer.flush();
writer.close();
英文:

Just wanted to let the community know that I have found a solution. While writing the file openCSV is not a good option. I am using Apache commons csv and it's working fine.

Here is updated working code.

String filePath = Constants.CONFIGURATION_PATH + &quot;csv\\&quot; + Constants.RUNNAME + &quot;.csv&quot;;
	File source=new File(Constants.CONFIGURATION_PATH + &quot;csv\\&quot; +&quot;MyTemplate.csv&quot;);
	
	CSVPrinter writer = new CSVPrinter(new FileWriter(filePath), CSVFormat.EXCEL);
	writer.printRecord(&quot;First Name&quot;,&quot;Last Name&quot;,&quot;Email&quot;);
	for (int i = 0; i &lt; numberOfRecords; i++) {
		
		List&lt;String&gt; nextLine = new ArrayList&lt;&gt;();
	      nextLine.add(Commons.getRandomAlphabet(6).toUpperCase());
	      nextLine.add(Commons.getRandomAlphabet(6).toUpperCase());
	      nextLine.add(Commons.getRandomAlphanumeric(10).toUpperCase() + &quot;@email.com&quot;);
		
	      writer.printRecord(nextLine);
		//data.add(new String[]{ firstName, lastName, emailAddress });
	}
	
	writer.flush();
	writer.close();

huangapple
  • 本文由 发表于 2020年7月31日 05:40:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63181759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定