Java 如何阅读&;将测试用例的结果写在同一张Excel表格中
我已经创建了一个用于参数化的示例脚本,我正在寻找类似我的脚本的东西,它应该从excel工作表中读取数据,然后在同一excel工作表的下一列中更新每个测试用例的结果。我已经生成了一个脚本,但不幸的是它不起作用。excel工作表是在前面提到的路径上生成的,但是当尝试打开它时,它会显示为已锁定,如果我们检查此excel工作表的总体属性,则它会显示0字节的大小。我对selenium是新手,所以我对自己遗漏了什么或做错了什么感到困惑 以下是我随附的一些示例代码:Java 如何阅读&;将测试用例的结果写在同一张Excel表格中,java,selenium-webdriver,Java,Selenium Webdriver,我已经创建了一个用于参数化的示例脚本,我正在寻找类似我的脚本的东西,它应该从excel工作表中读取数据,然后在同一excel工作表的下一列中更新每个测试用例的结果。我已经生成了一个脚本,但不幸的是它不起作用。excel工作表是在前面提到的路径上生成的,但是当尝试打开它时,它会显示为已锁定,如果我们检查此excel工作表的总体属性,则它会显示0字节的大小。我对selenium是新手,所以我对自己遗漏了什么或做错了什么感到困惑 以下是我随附的一些示例代码: package Pn1; import
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
}
}
包Pn1;
导入java.io.File;
导入java.io.IOException;
导入java.util.concurrent.TimeUnit;
导入jxl.工作簿;
导入jxl.read.biff.BiffException;
导入jxl.write.Label;
导入jxl.write.WritableSheet;
导入jxl.write.WritableWorkbook;
导入org.apache.poi.hslf.model.Sheet;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
导入org.openqa.selenium.support.ui.ExpectedConditions;
导入org.openqa.selenium.support.ui.WebDriverWait;
公共类参数化{
公共静态void main(字符串[]args)抛出biffeException、IOException、Exception{
WebDriver=newfirefoxdriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
//工作簿位置
工作簿wBook=Workbook.getWorkbook(新文件(“C:\\Users\\Documents\\TestData\\SampleData.xls”);
文件fExcel=新文件(“C:\\Users\\Documents\\TestSmokeTestResult2.xls”);
//拿床单
jxl.Sheet Sheet=wBook.getSheet(0);
//环路
对于(int i=1;i在关闭驱动程序之前添加两条语句
writableBook.write();
writableBook.close();
jxl api不提供更新excel工作表的方法,您应该将代码切换到apache api以更新excel。