我想从Excel工作表中读取数据,并将(写入PASS或FAIL)测试结果追加到同一张Excel工作表中?

问题描述:

@Test(优先级= 1) 公共无效AddUsers1()抛出异常{我想从Excel工作表中读取数据,并将(写入PASS或FAIL)测试结果追加到同一张Excel工作表中?

String invalidusername=admin.getData(8, 1); 
    String validusername=admin.getData(9, 1); 
    String firstname=admin.getData(10, 1); 
    String lastname=admin.getData(11, 1); 
    String invalidpwd=admin.getData(12, 1); 
    String validpwd=admin.getData(13, 1); 
    String invalidemail=admin.getData(14, 1); 
    String validemail=admin.getData(15, 1); 
    String phone=admin.getData(16, 1); 



//driver.findElement(By.cssSelector("#expiretxt > button.btn.btn-default")).click(); 

Thread.sleep(1000);; 
driver.findElement(By.linkText("ADMIN")).click(); 

driver.findElement(By.linkText("Users")).click(); 

WebDriverWait wait = new WebDriverWait(driver, 50);// 1 minute 

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//一个[@ HREF =” http://test.com]“)) )。单击();

WebDriverWait wait1 = new WebDriverWait(driver, 40);// 1 minute 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))).click(); 


//Submitting without giving user name 
driver.findElement(By.id("submit-1")).click(); 


String expectedTitle = "Username is empty."; 
String actualTitle = ""; 

actualTitle = driver.switchTo().alert().getText(); 

if (actualTitle.contentEquals(expectedTitle)){ 
    System.out.println("1.Test Passed!-By submitting without user name alert displayed as [User name is empty]"); -----> 

这个结果应该在现有的Excel工作表

+0

的Apache POI应该给你你需要什么 https://poi.apache.org/ – Jsmith2800

进口的Apache POI的jar编写和创建这个类。要读/通过该方法传递参数实例权

package pom; 

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import org.apache.poi.EncryptedDocumentException; 
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.ss.usermodel.Sheet; 
import org.apache.poi.ss.usermodel.Workbook; 
import org.apache.poi.ss.usermodel.WorkbookFactory; 

public class ExcelOperations 
{ 

    //public int lastRow; 
    public String readExcel(String xlPath, String xlSheet, int rowNum, int cellNum) throws EncryptedDocumentException, InvalidFormatException, IOException 
    { 

     FileInputStream fis = new FileInputStream(xlPath); 
     Workbook wb = WorkbookFactory.create(fis); 
     Sheet sh = wb.getSheet(xlSheet); 
     Row row = sh.getRow(rowNum); 
     String cellVal = row.getCell(cellNum).getStringCellValue(); 
     //lastRow = sh.getLastRowNum(); 
     return cellVal; 
    } 

    public int lastRowNum(String xlPath, String xlSheet) throws EncryptedDocumentException, InvalidFormatException, IOException 
    { 
     FileInputStream fis = new FileInputStream(xlPath); 
     Workbook wb = WorkbookFactory.create(fis); 
     Sheet sh = wb.getSheet(xlSheet); 
     //Row row = sh.getRow(rowNum); 
     //String cellVal = row.getCell(cellNum).getStringCellValue(); 
     int lastRow = sh.getLastRowNum(); 
     return lastRow; 
    } 


    public void writeExcel(String xlPath, String xlSheet, int rowNum, int cellNum, String inputString) throws EncryptedDocumentException, InvalidFormatException, IOException 
    { 
     FileInputStream fis = new FileInputStream(xlPath); 
     Workbook wb = WorkbookFactory.create(fis); 
     Sheet sh = wb.getSheet(xlSheet); 
     Row row = sh.getRow(rowNum); 
     Cell cell=row.createCell(cellNum); 
     cell.setCellValue(inputString); 
     FileOutputStream fos=new FileOutputStream(xlPath); 
     wb.write(fos); 
     fos.close(); 
    } 

}