即使提供了新数据,testNG数据提供程序中使用的Excel也使用旧数据

问题描述 投票:0回答:1

我正在使用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 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;
          } 
    }
selenium selenium-webdriver testng testng-dataprovider testng-eclipse
1个回答
0
投票

如果使用Intellij,则无效缓存并重新启动。保存excel文件,将其从源中删除并再次添加。试试看。

© www.soinside.com 2019 - 2024. All rights reserved.