Java Sellinum Cucumber Excell 数据驱动 [英] Java Sellinum Cucumber Excell data driven

查看:42
本文介绍了Java Sellinum Cucumber Excell 数据驱动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要获取价值";基于给定的密钥"从 Excel 文件我有excel文件文件名 测试 xlsx和工作表名称 sheet1

Need to get "value" based on given "key" from Excel file I have excel file File name Test xlsx and sheet name sheet1

并且工作表包含以下键值对和.JIRA 票是独一无二的.

And sheet contains following key and value pairs and. JIRA ticket is unique .

<头>
测试用例描述测试数据键测试数据值testdata2 键Testdata2 值testdata3 键Testdata3 值
样本测试描述1Testcase-jira-1user1id哈沙德密码123ggg
Sampiletest2 说明Testcase-jira-2用户2拉穆密码123333ggg
Sampiletest3 说明测试用例 jira-3用户3拉塔密码55673hhh

最多 N 行

在这里,我需要使用 Java Selenium Cucumber 以下列方式获取数据.我将使用上面的测试数据通过BDD方式传入Cucumber step definition类文件.

Here, I needs to get the data in following way by using Java Selenium Cucumber. I am going to use above test data to pass in Cucumber step definition class file by BDD way.

我们如何获取 definition 文件中的数据以用于以下方式1)如果从当前行传递键值,我们如何获取值的值,以便为 webSeleinum 元素提供测试输入示例第 4 行数据

How can we get the data in definition file for following way 1)If pass Key value from current row how can we get the value of value for provide test input for webSeleinum element Example 4th row data

Sampiletest3 discription|Test case jira-3| user3|latha|Password556|73hhh
.....

如果我调用user3"应该返回Password556"以同样的方式,我需要获得价值的任何行.请指导我

If I call the "user3" that should return "Password556" Same way any row I need to get the value. Please guide me

推荐答案

你可以试试下面的代码.

You can try the below code.

功能文件:

  • 在示例中,您可以提供行号和工作表名称以使用数据进行迭代.
    Scenario Outline: Login to the application with multiple users.
    
        Given get data from datasheet with "<test_id>" and "<sheetName>"
        And login to the application
    
        Examples: 
          | test_id | sheetName   |
          |       1 | Login       |
          |       2 | Login       |

Excel 数据:

从 excel 中读取数据并将其存储在 hashmap 中:

Read the data from excel and store it in a hashmap:

  • 创建一个类来读取数据(例如:ExcelReader)
  • 使用 org.apache.poi.ss.usermodelorg.apache.poi.xssf.usermodel 导入
  • Create a class to read the data (Example: ExcelReader)
  • Use org.apache.poi.ss.usermodel and org.apache.poi.xssf.usermodel imports
    
    public class ExcelReader {
    
        private File file;
        private FileInputStream inputStream;
        private String testID;
        private String sheetName;
        private int testIdColumn;
        private int numberOfColumns;
        private XSSFCell cell;
        public  HashMap<String, String> fieldsAndValues;
    
        public ExcelReader(String testId, String sheetName) {
            file = new File(System.getProperty("user.dir") + "Excel location path");
            try {
                inputStream = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                System.out.println("File not found at given location: " + e);
            }
            this.testID = testId;
            this.sheetName = sheetName;
    
            this.readExcelAndCreateHashMapForData();
        }
    
        public HashMap<String, String> readExcelAndCreateHashMapForData() {
            try {
                fieldsAndValues = new HashMap<String, String>();
    
                XSSFWorkbook workBook = new XSSFWorkbook(inputStream);
                XSSFSheet sheet = workBook.getSheet(sheetName);
    
                /* Get number of rows */
                int lastRow = sheet.getLastRowNum();
                int firstRow = sheet.getFirstRowNum();
                int numberOfRows = lastRow - firstRow;
    
                /*
                 * Get test_Id column number.
                 */
                outerloop: for (int row = 0; row < numberOfRows; row++) {
                    numberOfColumns = sheet.getRow(row).getLastCellNum();
                    for (int cellNumber = 0; cellNumber < numberOfColumns; cellNumber++) {
                        cell = sheet.getRow(row).getCell(cellNumber);
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        if (sheet.getRow(row).getCell(cellNumber).getStringCellValue().equalsIgnoreCase("test_ID")) {
                            testIdColumn = sheet.getRow(row).getCell(cellNumber).getColumnIndex();
                            break outerloop;
                        }
                    }
                }
    
                /*
                 * Search for the test id value.
                 */
                outerloop: for (int i = 0; i <= numberOfRows; i++) {
                    
                    cell = sheet.getRow(i).getCell(testIdColumn);
                    cell.setCellType(Cell.CELL_TYPE_STRING);
    
                    if (testID.equals(sheet.getRow(i).getCell(testIdColumn).getStringCellValue())) {
    
                        
                        for (int j = 0; j < numberOfColumns; j++) {
                            XSSFCell key = sheet.getRow(testIdColumn).getCell(j);
                            XSSFCell value = sheet.getRow(i).getCell(j);
    
                            key.setCellType(Cell.CELL_TYPE_STRING);
    
                            if (value == null) {
                                // Not capturing blank cells.
                            } else if (value.getCellType() == XSSFCell.CELL_TYPE_BLANK) {
                                // Not capturing blank cells.
                            } else {
                                value.setCellType(Cell.CELL_TYPE_STRING);
                                String fieldName = sheet.getRow(testIdColumn).getCell(j).getStringCellValue().trim();
                                String fieldValue = sheet.getRow(i).getCell(j).getStringCellValue().trim();
                                fieldsAndValues.put(fieldName, fieldValue);
                            }
                        }
                        System.out.println("Fields and values: " + Arrays.toString(fieldsAndValues.entrySet().toArray()));
                        break outerloop;
                    }
                }
            } catch (Exception e) {
                System.out.println("Exception occurred at getting the sheet: " + e);
            }
            /* Return the hash map */
            return fieldsAndValues;
        }
    
    }

步骤定义:


         ExcelReader excelReader;
    
        @Given("get data from datasheet with "(.*)" and "(.*)"$")
            public void get_data_from_datasheet(String testId, String sheetName) {            
            excelReader = new ExcelReader(testId, sheetName);
            }
    

     @And("login to the application")
        public void loginApplication(){      
driver.findElement(By.xpath("element")).sendKeys(excelReader.fieldsAndValues.get("UserName"));         
driver.findElement(By.xpath("element")).sendKeys(excelReader.fieldsAndValues.get("PassWord"));      
          driver.findElement(By.xpath("element")).click();  
        }

这篇关于Java Sellinum Cucumber Excell 数据驱动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆