简体   繁体   English

数据驱动框架——如何使用Selenium WebDriver和java在excel表中读写

[英]Data Driven Framework — how to read and write in excel sheet using Selenium WebDriver with java

Here I'm using this code to read and I want to write out put on the same sheet.......I want to write results for every sys output according to putting output on relevant fields how to read and write the existing excel sheet?在这里,我使用这段代码来阅读,我想写出放在同一张纸上.......我想根据将输出放在相关字段上来为每个 sys 输出写结果如何读取和写入现有excel表格? 1 1

How to write data into an Excel file using Selenium WebDriver?如何使用 Selenium WebDriver 将数据写入 Excel 文件?

@Test(priority=1)
public void Shift() throws Exception {
String dupshiftname=Skadmin.getData(62, 1);
String validshiftname=Skadmin.getData(63, 1);    

driver.findElement(By.linkText("ADMIN")).click();
driver.findElement(By.id("sessionname")).sendKeys(dupshiftname);
if (actualTitle19.contentEquals(expectedTitle19)){
System.out.println("2.Test Passed!-Submitting without shift name alert      displayed as[Shift name is required]");

//Here i want this above output to be written on excel sheet at particular cell } //这里我希望上面的输出写在特定单元格的excel表上}

public static String getData(int r, int c) throws EncryptedDocumentException,   InvalidFormatException, IOException
{    
FileInputStream FIS=new FileInputStream("G://workspace//sample pro//src//testData//excel.xlsx");
  Workbook WB=WorkbookFactory.create(FIS);

DataFormatter formatter = new DataFormatter(); //creating formatter using the default locale
Cell cell = WB.getSheet("Sheet1").getRow(r).getCell(c);
String str = formatter.formatCellValue(cell); //Returns the formatted value  of a cell as a String regardless of the cell type.

return str;
}

for reading and writing in an excel file using java, you need api for this (Apache POI)要使用 java 在 excel 文件中读取和写入,您需要为此使用 api (Apache POI)

Code to read an excel file:读取excel文件的代码:

public class ReadExcel 
{
    public static void main(String[] args) 
    {
        try
        {
            FileInputStream file = new FileInputStream(new File("path\\to\\excel\\file.xlsx"));

            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook = new XSSFWorkbook(file);

            //Get first/desired sheet from the workbook
            XSSFSheet sheet = workbook.getSheetAt(0);

            //Iterate through each rows one by one
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) 
            {
                Row row = rowIterator.next();
                //For each row, iterate through all the columns
                Iterator<Cell> cellIterator = row.cellIterator();

                while (cellIterator.hasNext()) 
                {
                    Cell cell = cellIterator.next();
                    //Check the cell type and format accordingly
                    switch (cell.getCellType()) 
                    {
                        case Cell.CELL_TYPE_NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "t");
                            break;
                        case Cell.CELL_TYPE_STRING:
                            System.out.print(cell.getStringCellValue() + "t");
                            break;
                    }
                }
                System.out.println("");
            }
            file.close();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
}

Hope this will help you希望这会帮助你

Code to write in an excel sheet.写在excel表中的代码。

Code:代码:

public class WriteExcelDemo 
{
    public static void main(String[] args) 
    {
        //Blank workbook
        XSSFWorkbook workbook = new XSSFWorkbook(); 

        //Create a blank sheet
        XSSFSheet sheet = workbook.createSheet("Employee Data");

        //This data needs to be written (Object[])
        Map<String, Object[]> data = new TreeMap<String, Object[]>();
        data.put("1", new Object[] {"ID", "NAME", "LASTNAME"});
        data.put("2", new Object[] {1, "Amit", "Shukla"});
        data.put("3", new Object[] {2, "Lokesh", "Gupta"});
        data.put("4", new Object[] {3, "John", "Adwards"});
        data.put("5", new Object[] {4, "Brian", "Schultz"});

        //Iterate over data and write to sheet
        Set<String> keyset = data.keySet();
        int rownum = 0;
        for (String key : keyset)
        {
            Row row = sheet.createRow(rownum++);
            Object [] objArr = data.get(key);
            int cellnum = 0;
            for (Object obj : objArr)
            {
               Cell cell = row.createCell(cellnum++);
               if(obj instanceof String)
                    cell.setCellValue((String)obj);
                else if(obj instanceof Integer)
                    cell.setCellValue((Integer)obj);
            }
        }
        try
        {
            //Write the workbook in file system
            FileOutputStream out = new FileOutputStream(new File("howtodoinjava_demo.xlsx"));
            workbook.write(out);
            out.close();
            System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
}

Hope this will solve your problem希望这能解决你的问题

Here you go.给你。

Code: 

public class ReadWrite {

    public static void main(String[] args) {

        try {
            File excel = new File("D://raju.xlsx");
            FileInputStream fis = new FileInputStream(excel);
            XSSFWorkbook book = new XSSFWorkbook(fis);
            XSSFSheet sheet = book.getSheetAt(0);

            Iterator<Row> itr = sheet.iterator();

            // Iterating over Excel file in Java
            while (itr.hasNext()) {
                Row row = itr.next();

                // Iterating over each column of Excel file
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) {

                    Cell cell = cellIterator.next();

                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_STRING:
                        System.out.print(cell.getStringCellValue() + "\t");
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.print(cell.getNumericCellValue() + "\t");
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        System.out.print(cell.getBooleanCellValue() + "\t");
                        break;
                    default:

                    }
                }
                System.out.println("");
            }

            // writing data into XLSX file
            Map<String, Object[]> newData = new HashMap<String, Object[]>();
            newData.put("1", new Object[] { 1, "DELL", "7K", "Auto",
                    "USD" });


            Set<String> newRows = newData.keySet();
            int rownum = sheet.getLastRowNum();

            for (String key : newRows) {
                Row row = sheet.createRow(rownum++);
                Object[] objArr = newData.get(key);
                int cellnum = 0;
                for (Object obj : objArr) {
                    Cell cell = row.createCell(cellnum++);
                    if (obj instanceof String) {
                        cell.setCellValue((String) obj);
                    } else if (obj instanceof Boolean) {
                        cell.setCellValue((Boolean) obj);
                    } else if (obj instanceof Date) {
                        cell.setCellValue((Date) obj);
                    } else if (obj instanceof Double) {
                        cell.setCellValue((Double) obj);
                    }
                }
            }

            // open an OutputStream to save written data into Excel file

            FileOutputStream os = new FileOutputStream(excel);
            book.write(os);
            System.out.println("Writing on Excel file Finished ...");

            // Close workbook, OutputStream and Excel file to prevent leak
            os.close();
            book.close();
            fis.close();

        } catch (FileNotFoundException fe) {
            fe.printStackTrace();
        } catch (IOException ie) {
            ie.printStackTrace();
        }
    }
}

I hope this will solve your problem我希望这能解决你的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Selenium WebDriver中使用JAVA使用Excel工作表对不同网页上的内容进行数据驱动测试 - Data Driven testing using excel sheet for the content on different web pages using JAVA in Selenium WebDriver 如何构建数据驱动的 Selenium WebDriver + Java + TestNG 框架 - How to Build a Data Driven Selenium WebDriver + Java + TestNG Framework Selenium WebDriver-Java:如何使用Java将Webtable数据写入Selenium WebDriver中的excel文件? - Selenium WebDriver - Java : How to write webtable data into an excel file in Selenium WebDriver using Java? 通过使用 java selenium 如何在 Excel 表中写入数据 - By using java selenium how to write data in Excel sheet 如何从Selenium Webdriver中的Excel工作表中读取数据 - How to read Data from Excel sheet in selenium webdriver 如何使用Java for Selenium WebDriver在一个Excel工作表中编写2种不同的结果 - How to write the 2 different results in one excel sheet using java for selenium webdriver 为 selenium webdriver 数据驱动框架读取 excel 中的空单元格 - Reading empty cells in excel for selenium webdriver data driven framework 如何使用Selenium WebDriver TestNG数据提供程序方法从Excel工作表中读取特定值? - How to read a specific value from Excel sheet using Selenium WebDriver TestNG Data Provider Method? 读取多个Excel工作表selenium-webdriver,java,eclipse - read multiple excel sheet selenium-webdriver, java, eclipse 在 Selenium WebDriver 中读写 excel - Read and write excel in Selenium WebDriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM