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

标签 csv selenium data-driven-tests

我必须在每一行的最后一列添加结果。我必须测试用户使用正确的电子邮件和密码成功登录,“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/

相关文章:

cucumber - Cucumber 中的数据驱动测试

java - 是否有类似 ITestResult(使用 testng)的等效类仅适用于 JUnit?

javascript - 如何从 D3.js 中的嵌套访问值?

performance - Powershell 脚本的速度。寻求优化

c# - 使用 OleDB 读取 CSV 数据的文件访问错误

javascript - 在 Protractor 中,我怎样才能等到弹出窗口出现而不依赖超时?

java - 如何使用Java中的Excel文件中的随机函数随机读取单元格值?

perl - 需要更多有关如何使用 Spreadsheet::ParseExcel 的示例

java - 如何检查页面上是否出现了一些文本数据

python selenium - 单击按钮并获取 page_source