jagdpanzer
jagdpanzer

Reputation: 713

Writing Test Results to Excel using Selenium

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.

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.

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.

I'll show you my current @BeforeClass, @AfterClass, and two @Test blocks.

@BeforeClass:

@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 < String, Object[] > ();
        //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"
        });

    }

@AfterClass:

@AfterClass
      public void setupAfterSuite(ITestContext context) {
        //write excel file and file name is TestResult.xls 
        Set<String> 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();
        }

@Test blocks:

@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();
                }

            }
        }

    }

The second test, invalidCredentialsOne, never writes to the excel, whether I make it pass or fail.

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.

Upvotes: 2

Views: 8186

Answers (1)

Dmitry Malinovsky
Dmitry Malinovsky

Reputation: 301

Here is the structure as i see it:

1) A part where you have a DriverFactory defined.

public class BrowserFactory {

public static WebDriver localDriver(Capabilities capabilities) {
String browserType = capabilities.getBrowserName();
if (browserType.equals("firefox"))
  return new FirefoxDriver(capabilities);
if (browserType.startsWith("internet explorer"))
  return new InternetExplorerDriver(capabilities);
if (browserType.equals("chrome"))
  return new ChromeDriver(capabilities);
throw new Error("Unrecognized browser type: " + browserType);
}

Then you can simply initialize it anytime you need it : Example:

driver = BrowserFactory.localDriver(DesiredCapabilities.firefox());

2) Your test classes, where you use this factory. Then there will be no need in @BeforeClass annotations. You write your tests in those classes. And at the end of every test, you make an assert (if test result failed or not). To check if the test passes, use the Assert.true(); Example: I i use the wrokg credentials on login, the allert: Wrong Password will appear.

Solution: You make an Assert.true(errorMessagePresent)

3) Your output writer class - to make it accessible for your tests

3) In case the test passes - you add the string you want to the output, using the buffer reader, else you throw an exception

Upvotes: 2

Related Questions