【问题标题】:Unable to read excel file and use the data as test data in test script无法读取 excel 文件并将数据用作测试脚本中的测试数据
【发布时间】:2023-04-01 14:32:02
【问题描述】:

我是 Java Selenium 的新手,并试图从 excel 文件中获取登录输入作为测试数据(2 个登录详细信息)。但是,我无法从 excel 文件中读取测试数据并使用它来登录。有人可以给我一个代码吗?

我尝试了以下代码:

import org.testng.annotations.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeTest;

public class Sample1 {
    WebDriver driver;
    WebDriverWait wait;
    XSSFWorkbook workbook;
    XSSFSheet sheet;
    XSSFCell cell;
  @Test
  public void f() throws IOException {
      File src=new File("C:\\Standalone\\TestData.xls");
      FileInputStream finput = new FileInputStream(src);
      workbook = new XSSFWorkbook(finput);
      sheet= workbook.getSheetAt(0);
      for(int i=1; i<=sheet.getLastRowNum();i++)
      {
          cell = sheet.getRow(i).getCell(1);
          cell.setCellType(Cell.CELL_TYPE_STRING);
          driver.findElement(By.id("j_username")).sendKeys(cell.getStringCellValue());
          cell = sheet.getRow(i).getCell(2);
          cell.setCellType(Cell.CELL_TYPE_STRING);
          driver.findElement(By.id("j_password")).sendKeys(cell.getStringCellValue());
      }


  }
  @BeforeTest
  public void beforeTest() {
      System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("c");
  }

}

【问题讨论】:

  • 如果答案对您有帮助,请accept it 所以它可能对其他人有帮助

标签: java excel selenium


【解决方案1】:

我会建议一种非常简单的方法来做到这一点。根据给定的单元格编号创建一个用于读取值的类

例如ExcelFileReader.java

public class ExcelFileReader 
{
        FileInputStream fis;
        XSSFWorkbook workbook;
        XSSFSheet sheet;

        public ExcelFileReader() throws IOException
        {
            // Read Excel File
            fis = new FileInputStream(new File("src/test/resources/SiteTestData.xlsx"));
            workbook = new XSSFWorkbook(fis);

            // To get Sheet 1
            sheet = workbook.getSheetAt(0);

        }

        public String getCellData(int rowNum, int colNum)
        {
            return sheet.getRow(rowNum).getCell(colNum).getStringCellValue();
        }
}

现在在你的测试方法中调用getCellData 方法

@Test
public void login() throws IOException, InterruptedException
{    
    ExcelFileReader read = new ExcelFileReader();
    String uname = read.getCellData(1, 0);
    String passwd = read.getCellData(1, 1);

    driver.findElement(By.id("username")).sendKeys(uname);
    driver.findElement(By.id("password")).sendKeys(passwd);
    driver.findElement(By.id("loginButton")).click();

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-11
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 2014-02-13
    • 2012-12-20
    相关资源
    最近更新 更多