在用selenium做web automation的时候,我们经常需要读取外部的测试数据。对于同一个场景,同一个类操作,去遍历我们的测试数据。这个时候参数化测试就可以派上用场了。
本文阐述如何结合Selenium和TestNG,利用Excel文件存储测试数据写参数化的测试
首先看下TestNG官方网站的一段代码:
//This method will provide data to any test method that declares that its Data Provider
//is named "test1"
@DataProvider(name = "test1")
public Object[][] createData1() {
return new Object[][] {
{ "Cedric", new Integer(36) },
{ "Anne", new Integer(37)},
};
}
//This test method declares that its data should be supplied by the Data Provider
//named "test1"
@Test(dataProvider = "test1")
public void verifyData1(String n1, Integer n2) {
System.out.println(n1 + " " + n2);
}
所以,我们的思路很简单,只要写一个ExcelReader类,去读取测试测试数据,并以二维数组返回, 再将返回的值传递给我们的测试方法就可以了.以下是getRunData(File file)方法:
// Excel文件中最后一列值为n的行将被忽略,用以管理某个测试是否要执行
public static String[][] getRunData(File file)throws IOException {
List<String[]> results = new ArrayList<String[]>();
FileInputStream fis = new FileInputStream(file);
POIFSFileSystem POIStream = new POIFSFileSystem(fis);
HSSFWorkbook workBook = new HSSFWorkbook(POIStream);
HSSFSheet sheet = workBook.getSheetAt(0);
int rowSize = sheet.getLastRowNum();
System.out.println("The row size is: " + rowSize);
for (int rowIndex =1; rowIndex <= rowSize; rowIndex++ ) {
HSSFRow row = sheet.getRow(rowIndex);
int colSize = row.getLastCellNum();
HSSFCell lastcell = row.getCell(colSize-1);
if (lastcell.getStringCellValue().equalsIgnoreCase("n")){
continue;
}
System.out.println("The column size is: " + colSize);
String[] values = new String[colSize-1];
Arrays.fill(values, "defaultvalue");
for (int columnIndex=0; columnIndex < colSize-1 ; columnIndex++){
String value = "";
HSSFCell cell = row.getCell(columnIndex);
if (cell != null) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {
value = new SimpleDateFormat("yyyy-MM-dd").format(date);
} else {
value = "";
}
} else {
value = new DecimalFormat("0").format(cell.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_FORMULA:
if (!cell.getStringCellValue().equals("")) {
value = cell.getStringCellValue();
} else {
value = cell.getNumericCellValue() + "";
}
break;
case HSSFCell.CELL_TYPE_BLANK:
break;
case HSSFCell.CELL_TYPE_ERROR:
value = "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
value = (cell.getBooleanCellValue() == true ? "Y" : "N");
break;
default:
value = "default";
}
}
values[columnIndex] = value.trim();
}
results.add(values);
}
fis.close();
String[][] returnArray = new String[results.size()][rowSize];
for (int i = 0; i < returnArray.length; i++) {
returnArray[i] = (String[]) results.get(i);
}
return returnArray;
}
以下是测试用例代码:
// 读取当前包下面同名的.xls文件
@DataProvider(name = "dcpages")
public Object[][] createData() throws IOException {
URL url = DCHelpAllLinksValidationTest.class.getResource(this.getClass().getSimpleName()+".xls");
File file = new File(url.getPath());
return (Object[][]) ExcelReader.getRunData(file);
}
//Excel中有效数据有几列,就设置几个测试参数
@Test(dataProvider = "dcpages")
public void verifyPageLinks(String menu, String submenu) throws HttpException, IOException {
System.out.println(menu+ submenu);
moveToElement(driver.findElement(By.linkText(menu)));
String pagelink = driver.findElement(By.linkText(submenu)).getAttribute("href");
driver.get(pagelink);
Set<String> links = getDCPageLinks();
assert (links.size()>=1);
for (String link : links) {
assert (HttpClientValidation.getHttpResonseCode(httpClient, link)== 200);
}
}
运行测试方法后,代码将对Excel文件种最后一列值为非‘n’的所有行进行遍历。每一行测试数据最终将产生一个测试结果。
由于每个测试用例还可能需要在不同的测试环境比如dev, uat, prod中运行,测试数据也不一样,所以还可以再写一个String[][] getRunData(File file, String env)方法,针对不同的测试环境自动读取Excel下面不同的sheet