簡體   English   中英

如何在同一Excel工作表中讀取和寫入測試用例的結果

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

我已經創建了一個用於參數化的示例腳本,並且正在尋找類似腳本的內容,這些腳本應該從excel工作表中讀取數據,然后在同一excel工作表的下一列中更新每個測試用例的結果。 我已經生成了一個腳本,但是不幸的是它沒有用。 excel工作表是在上述路徑中生成的,但是當嘗試打開它時,它顯示為鎖定狀態,如果我們檢查此excel工作表的整體屬性,那么它的大小為0字節。 我是硒的新手,所以我對丟失或做錯的事感到困惑。

這是我附帶的一些示例代碼:

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

    }

}

在關閉驅動程序之前添加兩個語句

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM