java - 有人可以提供代码/逻辑来检索 excel(.xlsx) 数据,例如 ""driver.findElement(By.id ("")).sendkeys(getExceldata(row,column));"

标签 java excel selenium apache-poi

有人可以提供代码/逻辑来检索 Excel (.xlsx) 数据。我需要以这样的方式检索数据,以便我可以获取值并将其传递到代码中的任何位置以测试网页。这是我需要得到的代码:

driver.findElement(By.id("")).sendkeys(getExceldata(row, column));

我只需要定义行和列,它应该使用任何方法从 Excel 表中获取数据,如 getExceldata(1,2)
public String cellValue(String filepath,String sheetname,int r,int c)
{
        try{
            FileInputStream fis = new FileInputStream(new File(filepath));              
            book = WorkbookFactory.create(fis);
            sh = book.getSheet(sheetname);

            System.out.println(sh.getSheetName());              

            row = sh.getRow(r);
            cell = row.getCell(c);

            return cell.getStringCellValue();               
        }catch(Exception e)
        {
            return null;
        }
    }

    public int getRows(String filepath,String sheetname)
    {
     try{
        FileInputStream fis= new FileInputStream(filepath);
        book= new XSSFWorkbook(fis);

        return book.getSheet(sheetname).getLastRowNum();
     }
     catch(Exception e)
     {
         return 0;
     }

----------------------------------------下面的新类-------------------- -------------
WebDriver driver;

    Excel excel= new Excel();

    public static String filepath="‪D:\\Chinmaya Work\\Work Space\\simpleProject\\src\\Newcustomerdata.xlsx";
    public static String sheetname="newcusotomer";

    public void setUp() throws InterruptedException
    {
        driver=new FirefoxDriver();
        driver.get("http://www.demo.guru99.com/V4/index.php");
        Thread.sleep(5000);
    }

    public void loginToApplication()
    {
        Homepagegurru99 home=new Homepagegurru99(driver);

        home.enterUsername("mngr45812");
        home.enterPassword("chinu@221");
        home.clickLoogin();
        driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
    }

    public void gotoNewcustomepage()
    {
        Dashbardgurru99 dash=new Dashbardgurru99(driver);
        dash.gotonewCustomerpage();
        driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
    }

    public void createNewcustomer()
    {
        Newcustomeradd create=new Newcustomeradd(driver);           
        int rowCount=excel.getRows(filepath, sheetname);            
        System.out.println(rowCount);


        create.eneterCustomername(excel.cellValue(filepath, sheetname, 2,0));
        create.selectSex();
        create.enterDob(excel.cellValue(filepath, sheetname, 3,0));
        create.enterAddress(excel.cellValue(filepath, sheetname, 4,0));
        create.enterCity(excel.cellValue(filepath, sheetname, 5,0));
        create.enterState(excel.cellValue(filepath, sheetname, 6,0));
        create.enterPin(excel.cellValue(filepath, sheetname, 7,0));
        create.enterMobileno(excel.cellValue(filepath, sheetname, 8,0));
        create.enterEmail(excel.cellValue(filepath, sheetname, 9,0));
        create.enterPassword(excel.cellValue(filepath, sheetname, 10,0));
        create.clickSubmit();
        driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
        driver.switchTo().alert().accept();    

    }

    public void logOut()
    {
        driver.findElement(By.linkText("Log out")).click();
        driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
        driver.switchTo().alert().accept();

        driver.close();
    }

最佳答案

依赖项(poi-ooxml.jar)

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;

保留到 Excel 文件中
fis = new FileInputStream(new File(fileName));
XSSFWorkbook workbook = new XSSFWorkbook (fis);
XSSFSheet sheet = workbook.getSheetAt(0);

XSSFRow row1 = sheet.createRow(0);
XSSFCell r1c1 = row1.createCell(0);
r1c1.setCellValue("Demo");

fis.close();

FileOutputStream fos = new FileOutputStream(new File(outputFile));
workbook.write(fos);
fos.close();

要从 Excel 文件中检索,请点击链接 Get Cell Value from Excel Sheet with Apache Poi .

高温高压

关于java - 有人可以提供代码/逻辑来检索 excel(.xlsx) 数据,例如 ""driver.findElement(By.id ("")).sendkeys(getExceldata(row,column));",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39044273/

相关文章:

java - 如何获取SVN所有页面的历史记录

java - 无法使用 XOM 解析元素属性

java - Excel 追加更新因 Apache POI 失败

html - 单击 html 中的链接下载 Excel(.xlsx) 文件

javascript - Selenium IDE 1.8.1 警告框 : cannot see the alert box while running the tests

java - 为什么用ConfirmDialog关闭Frme窗口会在两种情况下(确定和取消)关闭?

java - @Version 为 null 时乐观锁失败

excel - 如何将公式向下复制到每第 3 行?

java - 使用 Selenium 在表上迭代

java - 使用 Selenium 从 XML 文件填写表单