我想从excel表中读取数据并将(将PASS或FAIL)测试结果附加到同一个excel表中?

时间:2017-04-12 03:57:42

标签: selenium selenium-webdriver data-driven-tests

@Test(优先级= 1)   public void AddUsers1()抛出异常{

  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工作表

1 个答案:

答案 0 :(得分:0)

导入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();
    }

}