简体   繁体   中英

How to Read & write Result of Test Case in same Excel sheet

I have created a sample script for parameterization and I'm looking for something like my script that should read data from an excel sheet and should then update the result of each test case in the next column of the same excel sheet. I have generated one script but unfortunately it's not working. The excel sheet is getting generated at the path mentioned but when trying to open it it shows up as locked and if we check the overall properties of this excel sheet then it shows a size of 0 bytes. I'm new to selenium so I'm confused as to what I'm missing or doing wrong.

Here is some sample code i have attached:

package Pn1;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.poi.hslf.model.Sheet;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class parameterization {

    public static void main(String[] args) throws BiffException, IOException, Exception {

        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
        //Workbook location
        Workbook wBook = Workbook.getWorkbook(new File("C:\\Users\\Documents\\TestData\\SampleData.xls"));
        File fExcel = new File ("C:\\Users\\Documents\\TestSmokeTestResult2.xls");
        //get sheet
        jxl.Sheet Sheet = wBook.getSheet(0); 

        //loop
        for(int i=1; i<Sheet.getRows(); i++)
        {           
            driver.get("Enter Test Env");
            driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
            driver.findElement(By.xpath("//input[@id='UserName']")).sendKeys(Sheet.getCell(0, i).getContents());
            WritableWorkbook writableBook = Workbook.createWorkbook(fExcel);
            writableBook.createSheet("Data", 1);
            WritableSheet writableSheet =writableBook.getSheet(0);
            Label data1 = new Label(0, 0, "Login as user");
            writableSheet.addCell(data1);
            Label data2 = new Label(1, 0, "Pass");
            writableSheet.addCell(data2);
            driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
            driver.findElement(By.xpath("//input[@id='Password']")).sendKeys(Sheet.getCell(1, i).getContents());
            driver.findElement(By.xpath("//input[@id='Password']")).sendKeys(Sheet.getCell(1, i).getContents());
            //Need to use twice time otherwise it will not take password.
            Thread.sleep(40);
            driver.findElement(By.xpath("//input[@name='Login']")).click();
            new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='topnav']/a/span")));
            driver.findElement(By.xpath("//div[@id='topnav']/a/span")).click();
            driver.findElement(By.xpath("//a[contains(text(),'Logout')]")).click();
        }
        driver.close();

        // TODO Auto-generated method stub

    }

}

Add two statements before closing of driver

writableBook.write();
writableBook.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM