Вот формат ExcelDataToDataTable, который преобразует Exceldata в таблицу данных
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
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";
}
}
Это класс ExcelReader для чтения данных из Excel
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 Каждый раз, когда данные отображаются только с первого листа