gpt4 book ai didi

csv - 如何使用 Selenium 将测试结果写入CSV文件

转载 作者:行者123 更新时间:2023-12-04 20:27:20 25 4
gpt4 key购买 nike

我必须在每一行的最后一列添加结果。我必须测试用户使用正确的电子邮件和密码成功登录,“PASS”附加到最后一个“FAIL”,然后转到第二行并检查每一行的结果。

public static void main(String[] args) throws Exception {

System.setProperty("webdriver.chrome.driver", "D:\\Automation\\Selenium Drivers\\chromedriver.exe");
WebDriver driver=new ChromeDriver();

driver.get("http://www.facebook.com");

// This will load csv file
CSVReader reader = null;
try{
reader = new CSVReader(new FileReader("C:\\Users\\src\\com\\elements\\demo.csv"));
}catch (Exception e) {
e.printStackTrace();
}

String[] cell;

while ((cell=reader.readNext())!=null){
for(int i=0;i<1;i++){
String emailid=cell[i];
String password=cell[i+1];

driver.findElement(By.id("email")).sendKeys(emailid);
driver.findElement(By.id("pass")).sendKeys(password);
driver.findElement(By.id("loginbutton")).click();

String outputFile = "C:\\Users\\src\\com\\elements\\demo.csv";
try {
// use FileWriter constructor that specifies open for appending
CsvWriter csvOutput = new CsvWriter(new FileWriter(outputFile, true),',');
if(driver.getTitle().equals("Log1 in to Facebook | Facebook"))
{
csvOutput.write("Pass"); //Your selenium result.
//csvOutput.endRecord();
//csvOutput.close();
}
else if (driver.getTitle().equals("Log in to Facebook | Facebook"))
{
csvOutput.write("userName");
csvOutput.write("password");
csvOutput.write("Fail"); //Your selenium result.
csvOutput.endRecord();
csvOutput.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

enter image description here

最佳答案

试试这段代码。

String outputFile = "test.csv";

// before we open the file check to see if it already exists
boolean alreadyExists = new File(outputFile).exists();

try {
// use FileWriter constructor that specifies open for appending
CsvWriter csvOutput = new CsvWriter(new FileWriter(outputFile, true),',');

// if the file didn't already exist then we need to write out the header line
if (!alreadyExists){
csvOutput.write("result");
csvOutput.endRecord();
}
// else assume that the file already has the correct header line
// write out a few records
csvOutput.write("userName");
csvOutput.write("password");
csvOutput.write("Pass/Fail"); //Your selenium result.
csvOutput.endRecord();
csvOutput.close();

} catch (IOException e) {
e.printStackTrace();
}

或如果我们想使用以字符串数组为参数的 writeNext() 方法,那么

String csv = "D:\\test.csv";
CSVWriter writer = new CSVWriter(new FileWriter(csv));

List<String[]> data = new ArrayList<String[]>();
data.add(new String[] {"India", "New Delhi"});
data.add(new String[] {"United States", "Washington D.C"});
data.add(new String[] {"Germany", "Berlin"});

writer.writeAll(data);

writer.close();

尝试其他选项。

FileWriter writer = new FileWriter("D:/test.csv",false);

writer.append(" ");
writer.append(',');

writer.append("UserName");
writer.append(',');
writer.append("Password");
writer.append(',');
writer.append("Pass/Fail");
writer.append('\n');

//generate whatever data you want

writer.flush();
writer.close();

关于csv - 如何使用 Selenium 将测试结果写入CSV文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48577108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com