selenium - 即使提供了新数据,testNG 数据提供程序中使用的 Excel 仍在使用旧数据
问题描述
我正在使用 dataprovider 从我的 selenium 脚本中读取数据并将其作为输入。我基本上是在我的应用程序中添加系统。早些时候,我在 excel 中有大约 110 行和 7 列的数据。现在我已经删除了它并在同一个 excel 中添加了 1 行和 7 列数据,但脚本仍在尝试添加 110 个系统。我不明白为什么会这样。
这是数据提供者:
@DataProvider (name= "systemData")
public static Object[][] systemData() throws IOException
{
Object Data[][] = ExcelUtils.readData("D:\\Eclipse O\\Data\\addSystem\\AddSystem.xlsx", "AddSystem");
return Data;
}
}
这是 excel 工具
package Utility;
import java.io.File;
import java.io.FileInputStream;
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.ss.usermodel.DataFormatter;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
public class ExcelUtils
{
WebDriver driver;
WebDriverWait wait;
static XSSFWorkbook workbook;
static XSSFSheet sheet;
static XSSFCell cell;
@Test
public static Object[][] readData(String sheetLocation, String sheetName) throws IOException
{
DataFormatter formatter = new DataFormatter();
// Import excel sheet.
File src=new File(sheetLocation);
// Load the file.
FileInputStream finput = new FileInputStream(src);
// Load the workbook.
workbook = new XSSFWorkbook(finput);
// Load the sheet in which data is stored.
sheet= workbook.getSheet(sheetName);
int RowNum = sheet.getPhysicalNumberOfRows();// count my number of Rows
System.out.println("Number of rows is " +RowNum);
int ColNum = sheet.getRow(0).getPhysicalNumberOfCells();
System.out.println("Number of columns is " +ColNum);
String Data[][]= new String[RowNum][ColNum];
for (int i = 0; i < RowNum; i++)
{
//System.out.println("First for loop entered");
XSSFRow row = sheet.getRow(i);
for (int j = 0; j < ColNum; j++)
{
//System.out.println("Second for loop entered");
if (row == null) {
Data[i][j] = "";
System.out.println("row = null entered");
}
else {
XSSFCell cell = row.getCell(j);
if (cell == null) {
Data[i][j] = "";
System.out.println("Cell = null entered");
}
else {
String value = formatter.formatCellValue(cell);
Data[i][j] = value.trim();
}
}
}
}
return Data;
}
}
解决方案
如果使用 Intellij,则使缓存无效并重新启动。保存 excel 文件,将其从源中删除并再次添加。试试看。
推荐阅读
- python - Django - OneToOneField 不断返回 None
- c++ - 为什么我不能在 `while` 循环中验证 `pow` 函数?
- r - 使用 ggplot 将两个矩阵合二为一
- php - 无法访问本地主机中的数据库,即使它确实存在
- r-markdown - 我可以在 RMarkdown 中引用使用 table1 制作的表格吗?
- python - 寻找关键词
- perl - -s 是否总是在 Perl 中的目录上返回零?
- java - 如何在 Java 中使用 ArrayList 删除不等于对象
- monaco-editor - 如何在自定义令牌提供程序中启用多行匹配
- python - 参数 1 必须是类型,而不是 super() 调用上的 pygame.surface