selenium+junit实现关键字数据驱动

本文详细介绍了如何使用Selenium和POI集成到Eclipse中进行自动化测试,并通过读取Excel内容实现数据驱动测试流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备工作

selenium-2.28.0

poi-3.7

集成到eclipse中去

 

//测试代码

package banco;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import commen.DataExtractTool;
import commen.Logincomm;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.text.SimpleDateFormat;
import java.util.Date;

@RunWith(value = Parameterized.class)
public class Open extends Logincomm  {
 public String username = null;
 public String loginno = null;
 public String logininfo=null;
 public String tag = null;
 public String definepara = null;
 public int taskmu;
 public String expreuslt= null;
 public Login login;
 public String logintag="0";
 //获取当前系统时间
 static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 static String cal =df.format(new Date());
 
 @Parameters
 public static Collection<Object> spreadsheetData() throws IOException {
    InputStream excelFile = new FileInputStream("D:/prj/BOH2G/workspace3/test/src/data/TestData.xls");
    return new DataExtractTool(excelFile).getData();
   }
 public Open(String username,
   String loginno,String logininfo,String tag,double taskmu,String expreuslt) {
  super(driver);
  this.username = username;
  this.loginno = loginno;
  this.logininfo = logininfo;
  this.tag = tag;
  this.taskmu=(int)taskmu;
  this.expreuslt=expreuslt;
 }
 @Test
 public void controtestflow() throws Exception {
      if(100001<=taskmu & taskmu<=200010){

          //实现关键字驱动       }
     }else{
       Assert.assertEquals(false, true);
     }
 }
      }

}

 

 

//读取excel内容的代码

package commen;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
 
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
 
public class DataExtractTool {
 private transient Collection<Object> data = null;
 
    public DataExtractTool(final InputStream excelFile) throws IOException {
        this.data = extractData(excelFile);
    }
 
    public Collection<Object> getData() {
        return data;
    }
 
    private Collection<Object> extractData(final InputStream excelFile)
            throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook(excelFile);
 
        data = new ArrayList<Object>();
        Sheet sheet = workbook.getSheetAt(0);
 
        int numberOfColumns = countNonEmptyColumns(sheet);
        List<Object> rows = new ArrayList<Object>();
        List<Object> rowData = new ArrayList<Object>();
       
        for (Row row : sheet) {      
         if (isEmpty(row)) {
          break;
         } else {
          rowData.clear();     
          for (int column = 0; column < numberOfColumns; column++) {         
           Cell cell = row.getCell(column);
           rowData.add(objectFrom(workbook, cell));
          }        
          rows.add(rowData.toArray());
         }
        }      
        return rows;       
    }
   
    private boolean isEmpty(final Row row) {
     Cell firstCell = row.getCell(0);  
     boolean rowIsEmpty = (firstCell == null) || (firstCell.getCellType() == Cell.CELL_TYPE_BLANK);   
     return rowIsEmpty;
    }
   
    private int countNonEmptyColumns(final Sheet sheet) {  
     Row firstRow = sheet.getRow(0);
     return firstEmptyCellPosition(firstRow);   
    }
   
    private int firstEmptyCellPosition(final Row cells) { 
     int columnCount = 0;
    
     for (Cell cell : cells) {    
      if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
       break;
      }
      columnCount++;   
     }   
     return columnCount;   
    }
   
    private Object objectFrom(final HSSFWorkbook workbook, final Cell cell) {   
     Object cellValue = null;
    
     if (cell.getCellType() == Cell.CELL_TYPE_STRING) {   
      cellValue = cell.getRichStringCellValue().getString();    
     }   
     else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {   
      cellValue = getNumericCellValue(cell); 
     }
     else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {  
      cellValue = cell.getBooleanCellValue();   
     } 
     else if (cell.getCellType() ==Cell.CELL_TYPE_FORMULA) {
      cellValue = evaluateCellFormula(workbook, cell);
     }
     return cellValue;  
    }
   
    private Object getNumericCellValue(final Cell cell) {  
     Object cellValue;
  
     if (DateUtil.isCellDateFormatted(cell)) {
      cellValue = new Date(cell.getDateCellValue().getTime());  
     } else {
      cellValue = cell.getNumericCellValue();    
     }    
     return cellValue;
    }
   
    private Object evaluateCellFormula(final HSSFWorkbook workbook, final Cell cell) {
     FormulaEvaluator evaluator = workbook.getCreationHelper() .createFormulaEvaluator();
     CellValue cellValue = evaluator.evaluate(cell);  
     Object result = null;
    
     if (cellValue.getCellType() == Cell.CELL_TYPE_BOOLEAN) {   
      result = cellValue.getBooleanValue(); 
     } 
     else if (cellValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {  
      result = cellValue.getNumberValue(); 
     }  
     else if (cellValue.getCellType() == Cell.CELL_TYPE_STRING) { 
      result = cellValue.getStringValue();     
     }    
     return result;    
    }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值