Skip to content

Commit c7c52b3

Browse files
committed
added template for reading data from xlsx file
1 parent 66f9d7d commit c7c52b3

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

src/test/java/step_definition/templates/ui/AuthenticationFormStepDefinition.java

+14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
import io.cucumber.java.en.And;
44
import io.cucumber.java.en.Then;
55
import io.cucumber.java.en.When;
6+
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
7+
import utils.ExcelReader;
8+
9+
import java.io.IOException;
10+
import java.util.List;
11+
import java.util.Map;
612

713
import static modules.page_objects.AuthenticationFormPage.buttonLogin;
814
import static modules.page_objects.AuthenticationFormPage.setCredentials;
@@ -26,4 +32,12 @@ public void iClickLoginButton() {
2632
public void theShouldBeDisplayed(String arg0) {
2733

2834
}
35+
36+
@When("I type loaded login data from external file")
37+
public void iTypeLoadedLoginDataFromExternalFile() throws IOException, InvalidFormatException {
38+
List<Map<String, String>> data = ExcelReader.getData("src/test/resources/data/login-data.xlsx", "Feuil1");
39+
String username = data.get(0).get("username");
40+
String password = data.get(0).get("password");
41+
setCredentials(username,password);
42+
}
2943
}

src/test/java/utils/ExcelReader.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.apache.poi.ss.util.NumberToTextConverter;
1818

1919
public class ExcelReader {
20-
public List<Map<String, String>> getData(String excelFilePath, String sheetName)
20+
public static List<Map<String, String>> getData(String excelFilePath, String sheetName)
2121
throws InvalidFormatException, IOException {
2222
Sheet sheet = getSheetByName(excelFilePath, sheetName);
2323
return readSheet(sheet);
@@ -29,7 +29,7 @@ public List<Map<String, String>> getData(String excelFilePath, int sheetNumber)
2929
return readSheet(sheet);
3030
}
3131

32-
private Sheet getSheetByName(String excelFilePath, String sheetName) throws IOException, InvalidFormatException {
32+
private static Sheet getSheetByName(String excelFilePath, String sheetName) throws IOException, InvalidFormatException {
3333
Sheet sheet = getWorkBook(excelFilePath).getSheet(sheetName);
3434
return sheet;
3535
}
@@ -39,11 +39,11 @@ private Sheet getSheetByIndex(String excelFilePath, int sheetNumber) throws IOEx
3939
return sheet;
4040
}
4141

42-
private Workbook getWorkBook(String excelFilePath) throws IOException, InvalidFormatException {
42+
private static Workbook getWorkBook(String excelFilePath) throws IOException, InvalidFormatException {
4343
return WorkbookFactory.create(new File(excelFilePath));
4444
}
4545

46-
private List<Map<String, String>> readSheet(Sheet sheet) {
46+
private static List<Map<String, String>> readSheet(Sheet sheet) {
4747
Row row;
4848
int totalRow = sheet.getPhysicalNumberOfRows();
4949
List<Map<String, String>> excelRows = new ArrayList<Map<String, String>>();
@@ -63,7 +63,7 @@ private List<Map<String, String>> readSheet(Sheet sheet) {
6363
return excelRows;
6464
}
6565

66-
private int getHeaderRowNumber(Sheet sheet) {
66+
private static int getHeaderRowNumber(Sheet sheet) {
6767
Row row;
6868
int totalRow = sheet.getLastRowNum();
6969
for (int currentRow = 0; currentRow <= totalRow + 1; currentRow++) {
@@ -90,11 +90,11 @@ private int getHeaderRowNumber(Sheet sheet) {
9090
return (-1);
9191
}
9292

93-
private Row getRow(Sheet sheet, int rowNumber) {
93+
private static Row getRow(Sheet sheet, int rowNumber) {
9494
return sheet.getRow(rowNumber);
9595
}
9696

97-
private LinkedHashMap<String, String> getCellValue(Sheet sheet, Row row, int currentColumn) {
97+
private static LinkedHashMap<String, String> getCellValue(Sheet sheet, Row row, int currentColumn) {
9898
LinkedHashMap<String, String> columnMapdata = new LinkedHashMap<String, String>();
9999
Cell cell;
100100
if (row == null) {
8.62 KB
Binary file not shown.

src/test/resources/features/templates/ui/FormAuthentication.feature

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,10 @@ Feature: Authentication form
2424
Examples:
2525
| title | login | password | login-message |
2626
| wrong username | toto | SuperSecretPassword! | Your username is invalid! |
27-
| wrong password | tomsmith | S | Your password is invalid! |
27+
| wrong password | tomsmith | S | Your password is invalid! |
28+
29+
@excel @test
30+
Scenario: Authenticate using data from external file
31+
When I type loaded login data from external file
32+
And I click login button
33+
Then the "login-message" should be displayed

0 commit comments

Comments
 (0)