[英]Write in Excel Apache POI
我創建了從excel獲取元素的腳本。
這是我的劇本
public void readExcel() throws BiffException, IOException {
String script = "return rlSerial;";
WebDriver driver;
String baseUrl;
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");
driver = new FirefoxDriver();
baseUrl = "http://website.com/";
String SiteWindow = driver.getWindowHandle();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String FilePath = "D:\\TestData.xls";
FileInputStream fs = new FileInputStream(FilePath);
Workbook wb = Workbook.getWorkbook(fs);
// TO get the access to the sheet
Sheet sh = wb.getSheet(0);
// To get the number of rows present in sheet
int totalNoOfRows = sh.getRows();
int totalNoOfCol=sh.getColumns();
sh.getColumns();
for (int row =1; row < totalNoOfRows; row++)
{
for (int col = 0; col < totalNoOfCol; col++){
if (col == 0)
{
System.out.println("Check for Elink "+sh.getCell(col,row).getContents());
}
if (col == 1) {
driver.get(baseUrl +sh.getCell(col,row).getContents());
}
if (col ==2 ) {
driver.findElement(By.xpath(sh.getCell(col,row).getContents())).click();
for (String PromoWindow : driver.getWindowHandles()) {
driver.switchTo().window(PromoWindow); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
}
}
if (col ==3 ) {
String exSerial = (String) ((JavascriptExecutor)driver).executeScript(script);
System.out.println("Actual rlSerial = "+ exSerial + "\t" +"Excpected rlSerial = "+sh.getCell(col,row).getContents());
Assert.assertEquals(exSerial ,sh.getCell(col,row).getContents());
System.out.println("Pass");
driver.close();
driver.switchTo().window(SiteWindow);
}
}
}
}
public static void main(String args[]) throws BiffException, IOException {
runTest DT = new runTest();
DT.readExcel();
}
}
如果我的測試用例通過,我想在下一列上寫“通過”,如果失敗,則“失敗”。
如何做到這一點,需要做什么!
為此,您必須在給定的行中創建一個新單元格,然后將該單元格的值設置為“通過”和“失敗”。 使用以下代碼:
sheet.getRow(rowNumber).createCell(columnNumber).setCellValue("pass");
編輯:在您的代碼中,您正在使用與TestNg批注一起使用的Assert.assertEquals(actual, expected)
函數,但是您在此處未使用TestNG批注,因此更好的方法是使用equals()
或簡單比較實際字符串和期望字符串equalsIgnoreCase()
方法並equalsIgnoreCase()
設置您的列通過或失敗,這是您想要的解決方案:
if (col ==3 ) {
String exSerial = (String) ((JavascriptExecutor)driver).executeScript(script);
System.out.println("Actual rlSerial = "+ exSerial + "\t" +"Excpected rlSerial = "+sh.getCell(col,row).getContents());
//Assert.assertEquals(exSerial ,sh.getCell(col,row).getContents());
if(exSerial.equals(sh.getCell(col,row).getContents())){
sh.getRow(row).createCell(totalNoOfCol).setCellValue("Pass");
System.out.println("Pass");
}
else{
sh.getRow(row).createCell(totalNoOfCol).setCellValue("Fail");
System.out.println("Fail");
}
driver.close();
driver.switchTo().window(SiteWindow);
}
並在for循環結束后保存工作表,如下所示:
FileOutputStream outFile =new FileOutputStream(new File(FilePath ));
wb.write(outFile);
outFile.close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.