title: How to write test result in CSV file using selenium
body: <p>I have to add result at the last column of each row. I have to test user successfully login with correct email and password the "PASS" is append to last else "FAIL" and go with the second row and check the result of each row.</p>
  
  <pre><code>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&lt;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();
          }
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/YJLie.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YJLie.png" alt="enter image description here"></a></p>