0

I am running Automation test cases with @RunWith(CucumberWithSerenity.class). We want to expose and maintain the Testdata separately in the Excel sheets instead of placing it in the Feature files.

The Template for Excel Testdata looks like:

|Scenario |UserName |Password|Name     |Address|City    |Pincode|
|Testcase1|testuser1|pass1   |testUser1|US     |Jersy   |12345  |
|Testcase1|testuser2|pass1   |testUser1|US     |Virginia|78955  |

We have chose to use Primary Key as 'Scenario' which would be present in both Feature file and Excel sheet and based on that we will read the specific row from excel and refer the specific row data as Testdata for that particular Scenario.

Questions:

  1. Is there a way to get the Scenario Name at run time from Feature file when Test is running, so that we can get the Excel sheet the extract the data for it from the Excel Sheets?
  2. Is there a default way/method available in above mentioned use case, so that we can use it for above use case?
Jed Fox
  • 2,979
  • 5
  • 28
  • 38
varunanilgupta
  • 21
  • 1
  • 2
  • 4
  • 1
    Possible duplicate of [Cucumber Selenium using Excel File as Data Table](https://stackoverflow.com/questions/44839635/cucumber-selenium-using-excel-file-as-data-table) – Marit Jun 04 '19 at 13:10

2 Answers2

2

Cucumber doesn't support external sources by design (it is a collaboration tool, not a test automation tool). In Serenity, you can build a parameterised JUnit test that gets data from a CSV file: http://serenity-bdd.info/docs/serenity/#_using_test_data_from_csv_files

John Smart
  • 969
  • 1
  • 5
  • 5
  • This will loop a "feature" file or "@Test Java class"? **Why Serenity developers can't leave to up to user's discretion, whether the user wants it to be a collaboration tool or test Automation tool?** – paul Sep 26 '19 at 22:26
0

public class ExcelDataTable {

private XSSFWorkbook book;
private FileInputStream file;

public String ReadDataSheet(String page, String path, int rowValue, int cellValue) throws IOException {
    String pointer;
    file = new FileInputStream(new File(path));
    book = new XSSFWorkbook(file);
    Sheet sheet = book.getSheet(page);
    Row row = sheet.getRow(rowValue);
    Cell cell = row.getCell(cellValue);
    pointer = cell.getStringCellValue();
    book.close();
    file.close();
    return pointer;
}

}

Just create a class with this code and here is how I'm using it

public class OpenWebSite implements Task {

    ExcelDataTable data = new ExcelDataTable();

    public static OpenWebSite openWebSite(){
        return Tasks.instrumented(OpenWebSite.class);
    }

@Override
public <T extends Actor> void performAs(T actor) {

    try {
        actor.attemptsTo(Open.url(data.ReadDataSheet("info", "Data.xlsx", 1, 1)));
    }
    catch (IOException e) {
        e.printStackTrace();
    }

}

}

Sort that out to make yours bro