标签:ade 科学 mave read column print ber 下载链接 过程
我的环境是mac + firefox42 + selenium 2.9.1
火狐历史版本的下载链接:http://ftp.mozilla.org/pub/firefox/releases/
本次实验需要下载大量jar包,如下图所示
需要下载链接的同学可以私信我- -
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
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.xssf.usermodel.XSSFWorkbook;
读取xlsx的代码如下所示(将这些代码放入到主函数里即可):
Workbook wb =null;
Sheet sheet = null;
Row row = null;
List<Map<String,String>> list = null;
String cellData = null;
String filePath = "/Users/izayoi/Desktop/input.xlsx";
String columns[] = {"name","git"};
wb = readExcel(filePath);
if(wb != null){
//用来存放表中数据
list = new ArrayList<Map<String,String>>();
//获取第一个sheet
sheet = wb.getSheetAt(0);
//获取最大行数
int rownum = sheet.getPhysicalNumberOfRows();
//获取第一行
row = sheet.getRow(0);
//获取最大列数
int colnum = row.getPhysicalNumberOfCells();
for (int i = 1; i<rownum; i++) {
Map<String,String> map = new LinkedHashMap<String,String>();
row = sheet.getRow(i);
if(row !=null){
for (int j=0;j<colnum;j++){
cellData = getStringVal(row.getCell(j));
//System.out.println(cellData.trim());
map.put(columns[j], cellData.trim());
}
}else{
break;
}
list.add(map);
}
}
//遍历解析出来的list
for (Map<String,String> map : list) {
String username = "";
String giturl = "";
String password = "";
int flag = 0;
for (Entry<String,String> entry : map.entrySet()) {
if(flag == 0)
{
username = entry.getValue();
flag++;
}
else
{
giturl = entry.getValue();
System.out.println(giturl);
flag--;
}
}
try {
assertEquals(giturl, driver.findElement(By.xpath("//p")).getText());
System.out.println(giturl);
} catch (Error e) {
verificationErrors.append(e.toString());
}
System.out.println();
}
其中,读取xlsx的函数如下所示:
public static Workbook readExcel(String filePath){
Workbook wb = null;
if(filePath==null){
return null;
}
String extString = filePath.substring(filePath.lastIndexOf("."));
InputStream is = null;
try {
is = new FileInputStream(filePath);
if(".xls".equals(extString)){
return wb = new HSSFWorkbook(is);
}else if(".xlsx".equals(extString)){
return wb = new XSSFWorkbook(is);
}else{
return wb = null;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return wb;
}
public static Object getCellFormatValue(Cell cell){
Object cellValue = null;
if(cell!=null){
//判断cell类型
switch(cell.getCellType()){
case Cell.CELL_TYPE_NUMERIC:{
cellValue = String.valueOf(cell.getNumericCellValue());
break;
}
case Cell.CELL_TYPE_FORMULA:{
//判断cell是否为日期格式
if(DateUtil.isCellDateFormatted(cell)){
//转换为日期格式YYYY-mm-dd
cellValue = cell.getDateCellValue();
}else{
//数字
cellValue = String.valueOf(cell.getNumericCellValue());
}
break;
}
case Cell.CELL_TYPE_STRING:{
cellValue = cell.getRichStringCellValue().getString();
break;
}
default:
cellValue = "";
}
}else{
cellValue = "";
}
return cellValue;
}
因为有些表格的数字是用科学计数法表示的,所以需要下面这个函数来将科学计数法的数字转化为字符串
private static String getStringVal(Cell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
return cell.getBooleanCellValue() ? "true" : "false";
case Cell.CELL_TYPE_FORMULA:
return cell.getCellFormula();
case Cell.CELL_TYPE_NUMERIC:
cell.setCellType(Cell.CELL_TYPE_STRING);
return cell.getStringCellValue();
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
default:
return "";
}
}
这个表格里有不少坑,比如有一个学生没有github链接,这里我直接判断他匹配失败,还有一些学生的github链接前有空格,我用trim()消除了这些空格
最后的运行结果如下图所示:
附一个github的网址:
https://github.com/IzayoiNamida/tjuscs-_software_testing
软件测试 实验02 MAC环境下Firefox配置selenium java读取xlsx文件
标签:ade 科学 mave read column print ber 下载链接 过程
原文地址:https://www.cnblogs.com/IzayoiNamida/p/8837313.html