java - 无法使用 java 从 Cucumber 中的第二张纸打印/获取数据 每次仅从第一张纸渲染数据

标签 java excel cucumber

这里是将exceldata转换为数据表的ExcelDataToDataTable格式

        package com.api.cucumber.transform;

        import java.io.File;
        import java.io.FileInputStream;
        import java.io.IOException;enter code here
        import java.net.URI;
        import java.util.Arrays;
        import java.util.LinkedList;
        import java.util.List;
        import java.util.Locale;

        import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
        import org.apache.poi.xssf.usermodel.XSSFWorkbook;

        import com.automation.custom.utilities.ExcelLibrary;
        import com.automation.custom.utilities.ExcelReader;


        import cucumber.api.DataTable;
        import cucumber.api.Transformer;
        import cucumber.runtime.ParameterInfo;
        import cucumber.runtime.table.TableConverter;
        import cucumber.runtime.xstream.LocalizedXStreams;
        import gherkin.formatter.model.Comment;
        import gherkin.formatter.model.DataTableRow;



        public class ExcelDataToDataTable extends Transformer<DataTable> {




            @Override
            public DataTable transform(String filePath) {


                String file[] = filePath.split(";");

                String path = getfilePath(file[0]);

                ExcelReader reader = new ExcelReader.ExcelReaderBuilder()

                        .setFileLocation(path)
                        .setSheet(file[1])
                        .build();

                List<List<String>> excelData = getExcelData(reader);

                List<DataTableRow> dataTableRows = getDataTableRows(excelData);

                DataTable table = getDataTable(dataTableRows);

                return table;
            }



        public String getfilePath(String path) {
            // File file = new File("TestData.xlsx");

        //return file.toString();

            }



private DataTable getDataTable(List<DataTableRow> dataTableRows) {
                    ParameterInfo parameterInfo = new ParameterInfo(null, null, null, null);
                    TableConverter tableConverter = new TableConverter(new LocalizedXStreams(Thread.currentThread().getContextClassLoader()).get(Locale.getDefault()), parameterInfo);



                DataTable table = new DataTable(dataTableRows, tableConverter);
                return table;
            }

            private List<DataTableRow> getDataTableRows(List<List<String>> excelData) {
                List<DataTableRow> dataTableRows = new LinkedList<>();
                int line = 1;

                for(List<String> list : excelData){
                    Comment commnet = new Comment("", line);
                    DataTableRow tableRow = new DataTableRow(Arrays.asList(commnet), list, line++);
                    dataTableRows.add(tableRow);
                }
                return dataTableRows;
            }

            private List<List<String>> getExcelData(ExcelReader reader) {
                List<List<String>> excelData = new LinkedList<>();

                try {
                    excelData = reader.getSheetDataAt();
                } catch (InvalidFormatException | IOException e) {
                    throw new RuntimeException(e.getMessage());
                }
                return excelData;
            }

        }

这是 StepDefination 文件

//Testdata.xlsx;0 sheet
    @Then("^I validate list of properties in page zero with data in excel at \"([^\"]*)\"$")
    public void i_validate_list_of_properties_in_page_zero_with_data_in_excel_at(@Transform(ExcelDataToDataTable.class)  DataTable table) throws Throwable {
    System.out.println(table.toString());
    List<String> dataList=table.asList(String.class);

for(String str : dataList)
{
    System.out.println(str);
}


    //Testdata.xlsx;1

    @Then("^I validate list of properties in page one with data in excel at \"([^\"]*)\"$")
    public void i_validate_list_of_properties_in_page_one_with_data_in_excel_at(@Transform(ExcelDataToDataTable.class)  DataTable table) throws Throwable {
    System.out.println(table.toString());
    List<String> dataList=table.asList(String.class);

for(String str : dataList)
{
    System.out.println(str);
}

这是功能文件

Feature: Validate values in application pages
    Description: Validate dynamic values 
    @Test
    Scenario: User navigates to application site and validates tags
    Given As User I need to tags in the application
    When I navigate to zero page "application url"
    Then I validate list of properties in page zero with data in excel at "TestData.xlsx;0”
    @Test1
    Scenario: User navigates to application site and validates tags
    Then i select country
    Then i validate list of properties in page one with data in exel at "TestData.xlsx;1”

这是transformData类

package com.api.cucumber.transform;

    import cucumber.api.Transformer;

    public class TransformData extends Transformer<String>{

        @Override
        public String transform(String args) {
            return args + " Transform";
        }

    }

这是从Excel读取数据的ExcelReader类

    package com.automation.custom.utilities;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Collections;
    import java.util.LinkedList;
    import java.util.List;

    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.usermodel.WorkbookFactory;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.openqa.selenium.JavascriptExecutor;

    import com.api.cucumber.transform.ExcelDataToDataTable;

    public class ExcelReader {

        public String fileName;
        public String sheetName;
        public int sheetIndex;
        public XSSFWorkbook book;

        public ExcelReader(ExcelReaderBuilder excelReaderBuilder) {

            this.fileName = excelReaderBuilder.fileName;
            this.sheetIndex  = excelReaderBuilder.sheetIndex;
            this.sheetName = excelReaderBuilder.sheetName;
        }

        public static class ExcelReaderBuilder{

            public String fileName;
            public String sheetName;
            public int sheetIndex;

            public ExcelReaderBuilder setFileLocation(String location) {
                this.fileName = location;
                return this;
            }

            public ExcelReaderBuilder setSheet(String sheetName) {
                this.sheetName = sheetName;
                return this;
            }

            public ExcelReaderBuilder setSheet(int sheetIndex) {
                this.sheetIndex = sheetIndex;
                return this;
            }

            public ExcelReader build() {
                return new ExcelReader(this);
            }

            @Override
            public String toString() {
                return "fileName = "+this.fileName+" , sheetName= "+this.sheetName+", sheetIndex="+this.sheetIndex;
            }
        }

        public XSSFWorkbook getWorkBook(String filePath) throws InvalidFormatException, IOException {
            return new XSSFWorkbook(new File(filePath));
        }

        public XSSFSheet getWorkBookSheet(String fileName, String sheetName) throws InvalidFormatException, IOException {
            this.book = getWorkBook(fileName);
            return this.book.getSheet(sheetName);
        }

        public XSSFSheet getWorkBookSheet(String fileName, int sheetIndex) throws InvalidFormatException, IOException {
            this.book = getWorkBook(fileName);
            return this.book.getSheetAt(sheetIndex);
        }

        public List<List<String>> getSheetData() throws IOException{
            XSSFSheet sheet;
            List<List<String>> outerList = new LinkedList<>();

            try {
                sheet = getWorkBookSheet(fileName, sheetName);
                outerList = getSheetData(sheet);
            } catch (InvalidFormatException e) {
                throw new RuntimeException(e.getMessage());
            }
            return outerList;
        }

        public List<List<String>> getSheetDataAt() throws InvalidFormatException, IOException {

            XSSFSheet sheet;
            List<List<String>> outerList = new LinkedList<>();

            try {
                sheet = getWorkBookSheet(fileName, sheetIndex);
                outerList = getSheetData(sheet);
            } catch (InvalidFormatException e) {
                throw new RuntimeException(e.getMessage());
            }
            return outerList;
        }

        public List<List<String>> getSheetData(XSSFSheet sheet) {
            List<List<String>> outerList = new LinkedList<>();
            prepareOutterList(sheet, outerList);
            return Collections.unmodifiableList(outerList);
        }

        public void prepareOutterList(XSSFSheet sheet, List<List<String>> outerList) {
            for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
                List<String> innerList = new LinkedList<>();
                XSSFRow xssfRow = sheet.getRow(i);

                for (int j = xssfRow.getFirstCellNum(); j < xssfRow.getLastCellNum(); j++) {
                    prepareInnerList(innerList, xssfRow, j);
                }
                outerList.add(Collections.unmodifiableList(innerList));
            }
        }

        public void prepareInnerList(List<String> innerList, XSSFRow xssfRow, int j) {
            switch (xssfRow.getCell(j).getCellType()) {

            case Cell.CELL_TYPE_BLANK:
                innerList.add("");
                break;

            case Cell.CELL_TYPE_STRING:
                innerList.add(xssfRow.getCell(j).getStringCellValue());
                break;

            case Cell.CELL_TYPE_NUMERIC:
                innerList.add(xssfRow.getCell(j).getNumericCellValue() + "");
                break;

            case Cell.CELL_TYPE_BOOLEAN:
                innerList.add(xssfRow.getCell(j).getBooleanCellValue() + "");
                break;

            default:
                throw new IllegalArgumentException("Cannot read the column : " + j);
            }
        }

注意 - 每次仅从第一个工作表渲染数据时,无法使用 java 从 Cucumber 中的第二个工作表打印/获取数据

最佳答案

在功能中: 然后我使用“TestData.xlsx;2”'''

处的 exel 中的数据验证第一页中的属性列表

但是在 StepDefinition 文件中: @Then("^我使用 Excel 中的数据验证第 0 页中的属性列表,位置为\"([^\"]*)\"$")

请注意:您确定在两行中都写有“excel”和“c”吗?或者任何其他方式,但始终相同?

关于java - 无法使用 java 从 Cucumber 中的第二张纸打印/获取数据 每次仅从第一张纸渲染数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60313326/

相关文章:

excel - 在 Word 文档中从 Excel VBA 插入换行符

java - CXF客户端: How to ignore @WebServiceClient annotation

java - 从更新的 SQL 数据库刷新 JavaFX 场景?

java - 等到 JQuery .replaceWith 完成

rest - 在 Karate DSL 框架中,我们如何添加自定义步骤定义以将其功能扩展到 REST 功能之外?

ruby - 当 Net::ReadTimeout 错误发生时, capybara 不会关闭浏览器

ruby-on-rails - 在 Cucumber 场景中使用 click_link

java - 如何使 Java Spring 组件类线程安全?

javascript - Excel 公式到 JavaScript 的转换

r - openxlsx 包,read.xlsx check.names=false 仍然放置一个 .在列名中