【问题标题】:I want to read data from excel sheet and append(write PASS or FAIL )test result to the same excel sheet?我想从 excel 表中读取数据并将(写入 PASS 或 FAIL )测试结果附加到同一个 excel 表中?
【发布时间】:2017-04-12 03:57:42
【问题描述】:

@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("//a[@href='http://test.com]"))).click();

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表格中

【问题讨论】:

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


【解决方案1】:

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 2019-09-22
    • 2012-04-12
    • 2020-09-12
    • 1970-01-01
    相关资源
    最近更新 更多