title: Writing Test Results to Excel using Selenium
body: <p>I've done plenty of research on this question and have tried many different methods, but none of them do what I'd like, or the explanations to implement them into my own code are really vague.</p>
  
  <p>I need to export the test results (TestID, Expected Result, Pass or Fail) into an excel sheet. I am currently using TestNG and Apache POI.</p>
  
  <p>I know how to write to an excel sheet, but I am absolutely lost on how to write whether or not something passed or failed. I am currently using some code that doesn't exactly work - sometimes it will write it, sometimes it won't. I need the most simple, easy way to do this, with a good explanation.</p>
  
  <p>I'll show you my current <code>@BeforeClass</code>, <code>@AfterClass</code>, and two <code>@Test</code> blocks.</p>
  
  <p><code>@BeforeClass</code>:</p>
  
  <pre><code>@BeforeClass(alwaysRun = true)
      public void setupBeforeSuite(ITestContext context) throws IOException {
          //create a new work book
          workbook = new HSSFWorkbook();
          //create a new work sheet
          sheet = workbook.createSheet("Test Result");
          testresultdata = new LinkedHashMap &lt; String, Object[] &gt; ();
          //add test result excel file column header
          //write the header in the first row
          testresultdata.put("1", new Object[] {
              "Test Step Id", "Action", "Expected Result", "Actual Result"
          });
  
      }
  </code></pre>
  
  <p><code>@AfterClass</code>:</p>
  
  <pre><code>@AfterClass
        public void setupAfterSuite(ITestContext context) {
          //write excel file and file name is TestResult.xls 
          Set&lt;String&gt; keyset = testresultdata.keySet();
          int rownum = 0;
          for (String key : keyset) {
              Row row = sheet.createRow(rownum++);
              Object [] objArr = testresultdata.get(key);
              int cellnum = 0;
              for (Object obj : objArr) {
                  Cell cell = row.createCell(cellnum++);
                  if(obj instanceof Date) 
                      cell.setCellValue((Date)obj);
                  else if(obj instanceof Boolean)
                      cell.setCellValue((Boolean)obj);
                  else if(obj instanceof String)
                      cell.setCellValue((String)obj);
                  else if(obj instanceof Double)
                      cell.setCellValue((Double)obj);
              }
          }
          try {
              FileOutputStream out =new FileOutputStream(new File("C:/Users/PathToFile/LoginCombinations.xls"));
              workbook.write(out);
              out.close();
              System.out.println("Excel written successfully..");
  
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  </code></pre>
  
  <p><code>@Test</code> blocks:</p>
  
  <pre><code>@Test(priority=0)
      public void successfulLogin() throws InterruptedException {
  
          Properties prop = new Properties();
          InputStream config = null;
          InputStream signinpage;
  
          try {
              // First we iterate over and read the config file
              config = new FileInputStream("C:/Users/PathToFile/src/ConfigFiles/config");
              prop.load(config);
              signinpage = new FileInputStream("C:/Users/PathToFile/src/ObjectRepositories/signinpage");
              prop.load(signinpage);
  
              // Next we initiate the driver, and navigate to the Web Application
              driver = new FirefoxDriver();
              driver.get(prop.getProperty("url"));
              driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  
              // Now we run the first step, "enterValidCredentials"
              // In this test, this is actually the only step.
              LoginPage.enterValidCredentials.run(driver);
  
              // Assert that we landed on the Product Select page.
              // assertEquals(driver.findElement(By.xpath(prop.getProperty("tempproductselect"))).getText(), "SELECT PRODUCT");
              try{
                  assertEquals(driver.findElement(By.xpath(prop.getProperty("tempproductselect"))).getText(), "SELECT PRODUCT");
                    //add pass entry to the excel sheet
                    testresultdata.put("2", new Object[] {1d, "User can login with a valid username and password", "Login successful","Pass"});
                    }
  
                    catch(Exception e)
                    {
                      //add fail entry to the excel sheet
                      testresultdata.put("2", new Object[] {1d, "User can login with a valid username and password", "Login successful","Fail"});
                    }
  
              // Write the test result to the sheet.
  
              driver.close();
              Alert alert = driver.switchTo().alert();
              alert.accept();
  
          } catch (IOException ex) {
              ex.printStackTrace();
          } finally {
              if (config != null) {
                  try {
                      config.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
  
              }
          }
  
      }
  
  @Test(priority=1)
      public void invalidCredentialsOne() throws InterruptedException {
  
          Properties prop = new Properties();
          InputStream config = null;
          InputStream signinpage;
  
          try {
              // First we iterate over and read the config file
              config = new FileInputStream("C:/Users/PathToFile/src/ConfigFiles/config");
              prop.load(config);
              signinpage = new FileInputStream("C:/Users/PathToFile/src/ObjectRepositories/signinpage");
              prop.load(signinpage);
  
              // Next we initiate the driver, and navigate to the Web Application
              WebDriver driver;
              driver = new FirefoxDriver();
              driver.get(prop.getProperty("url"));
              driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  
              // Now we run the first step, "invalidCredentialsOne"
              // In this test, this is actually the only step.
              LoginPage.invalidCredentialsOne.run(driver);
              Thread.sleep(5000);
  
              try{
                  assertEquals(driver.findElement(By.xpath(prop.getProperty("failedlogin"))).getText(), "LOG IN");
                //add pass entry to the excel sheet
                  testresultdata.put("3", new Object[] {2d, "User should not be able to login with an invalid password", "Login failed","Pass"});
                }
  
                catch(Exception e)
                {
                  //add fail entry to the excel sheet
                  testresultdata.put("3", new Object[] {2d, "User should not be able to login with an invalid password", "Login failed","Fail"});
                }           
  
              // Write the test result to the sheet.
  
              // After the test, we close the driver.
              driver.close();
              Alert alert = driver.switchTo().alert();
              alert.accept();
  
          } catch (IOException ex) {
              ex.printStackTrace();
          } finally {
              if (config != null) {
                  try {
                      config.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
  
              }
          }
  
      }
  </code></pre>
  
  <p>The second test, invalidCredentialsOne, never writes to the excel, whether I make it pass or fail.</p>
  
  <p>Java is also new to me, so forgive any formatting/lingo/whatever errors I have in there. I'm very open-minded to suggestion, I'm trying to improve.</p>