我已经创建了从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();
}
}
如果我的测试用例通过,我想在下一列中写"通过",如果失败,则"失败"。
如何实现这一点,需要做什么!!
要实现这一点,首先您必须在给定行中创建一个新单元格,然后将值设置为"通过"one_answers"失败"到该单元格。使用以下代码:
sheet.getRow(rowNumber).createCell(columnNumber).setCellValue("pass");
编辑:在你的代码中,你使用的是与TestNg注释一起使用的Assert.assertEquals(actual, expected)
函数,但你没有在这里使用TestNg注释,所以更好的方法是简单地通过使用equals()
或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();