Data driven framework in selenium webdriver using excel print the results in excel sheet

package SeleniumLearn;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.concurrent.TimeUnit;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class DataDrivenFramework {
    public WebDriver driver;
    public String str;
    
        
@Test
public void PrintTestResultsinSheet() throws Exception{
        driver=new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://www.seleniumlearn.com/user");
        driver.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
        //Reading the data from the input file      
        FileInputStream fi=new FileInputStream("C:\\lib\\TestData\\LoginRetesting.xls");
        Workbook w=Workbook.getWorkbook(fi);
        Sheet s=w.getSheet("Sheet1");
        // Creating the result file under the result folder
        FileOutputStream fo=new FileOutputStream("C:\\lib\\TestData\\Login1.xls");
        WritableWorkbook wb=Workbook.createWorkbook(fo);
        WritableSheet ws=wb.createSheet("Results", 0);

        for (int i = 1; i < s.getRows(); i++) {
            //Take the username, password and click on signin by taking the testdata from excel file    
            
            driver.findElement(By.id("edit-name")).clear();
            //driver.findElement(By.id("edit-name")).sendKeys(Keys.CONTROL, "a");
            Thread.sleep(2000);
            driver.findElement(By.id("edit-name")).sendKeys(s.getCell(0, i).getContents());
                        
            driver.findElement(By.id("edit-pass")).sendKeys(s.getCell(1, i).getContents());
            driver.findElement(By.id("edit-submit")).click();
            Thread.sleep(5000);
        
            //Validate logout, if available assign pass to str, else assign fail to str
            try{
                driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
                driver.findElement(By.linkText("Log out")).click();
                str="Pass";
            }catch(Exception e){str="Fail";}
            //Add the str value under the Result column    
            Label result=new Label(2, i, str);
            ws.addCell(result);
            //Add the input data to the Result file
            for(int j=0; j<s.getColumns(); j++){
                System.out.println(s.getCell(j, i).getContents());
                Label l=new Label(j, i, s.getCell(j, i).getContents());
                ws.addCell(l);
            }
        }
        //Add the Heading
        Label un=new Label(0,0,"Username");
        Label pw=new Label(1,0,"Password");
        Label rs=new Label(2,0,"Result");
            ws.addCell(un);
            ws.addCell(pw);
            ws.addCell(rs);
            //Write and close the Result file
                wb.write();
                wb.close();
          }

 

 @BeforeTest
      public void beforeTest() {
          driver=new FirefoxDriver();
        /*System.setProperty("webdriver.chrome.driver","C:\\lib\\chromedriver.exe"); //--->chrome browser path
        driver=new ChromeDriver();*/
        
      }

  @AfterTest
  public void afterTest() {
     
  }

}

 

Comments