即使提供了新数据,在TestNG数据提供商中使用的Excel也使用旧数据



我正在使用dataprovider读取来自我的Selenium脚本的输入的数据,并将其作为输入。我基本上将系统添加到我的应用程序中。早些时候,我在Excel中有大约110行和7列的数据。现在,我已经删除了它,并在同一Excel中添加了1行和7列数据,但是该脚本仍在尝试添加110个系统。我不明白为什么会发生这种情况。

这是DataProvider:

  @DataProvider (name= "systemData")
  public static Object[][] systemData() throws IOException
  {
      Object Data[][] = ExcelUtils.readData("D:\Eclipse O\Data\addSystem\AddSystem.xlsx", "AddSystem");
      return Data;
  }
}

这是Excel Util

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文件,将其从源中删除,然后再次添加。尝试一下。

最新更新