【发布时间】:2016-11-28 13:42:17
【问题描述】:
我正在尝试编写代码来自动化使用 Appium 进行移动网站测试。我需要从 Excel 中读取数据。我正在将项目编写为 Maven 代码。要从 maven 存储库中获取什么?
注意:我是 Java 和任何类型自动化的新手。
【问题讨论】:
我正在尝试编写代码来自动化使用 Appium 进行移动网站测试。我需要从 Excel 中读取数据。我正在将项目编写为 Maven 代码。要从 maven 存储库中获取什么?
注意:我是 Java 和任何类型自动化的新手。
【问题讨论】:
你应该使用jxl或poi jar文件从excel中读取数据,它也提供maven依赖
【讨论】:
你可以使用 poi。
在 pom.xml 中添加依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
在您的项目中添加一个实用程序类,如下所示:确保您的工作表名称(类中的 Sheet1)正确
package com.appium.utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.util.POILogger;
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
public class ExcelDriven {
public static XSSFWorkbook wb;
public static XSSFSheet sheet;
public static XSSFRow row;
public static XSSFCell cell;
public static FileInputStream fis;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// fis = new FileInputStream("/Users/tabish/Downloads/data.xlsx");
}
public static String getCellData(int rownum,int col, String fileName)
{
try {
fis = new FileInputStream("/path/to/file/"+fileName+".xlsx");
wb = new XSSFWorkbook(fis);
sheet= wb.getSheet("Sheet1");
row = sheet.getRow(rownum);
cell = row.getCell(col);
// System.out.println(cell.getStringCellValue());
if (cell==null){
return "";
}
return cell.getStringCellValue();
}
catch (Exception e)
{
System.out.println("In the Catch Block:"+e);
return "Exception Occured";
}
}
}
访问数据
while (!ExcelDriven.getCellData(0,i,"fileName").equals(""))
{
SomeClass.SomeMethod(ExcelDriven.getCellData(0,i,"fileName"));
i++;
}
【讨论】: