• Heading

enter image description here

How to get the username and password from excel using cucumber

package com.qa.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelReader{
//  public static void main(String args[]) throws IOException   {
//ExcelReader reader=new ExcelReader();
//  reader.readExcelData();
//  }
    public Object[][] readExcelData() throws IOException
        String Filepath="E:\\Old\\ExtentReports\\ExcelFiles\\login.xlsx";
        File file=new File(Filepath);
        FileInputStream fis=new FileInputStream(file);
        XSSFWorkbook book=new XSSFWorkbook(fis);
        XSSFSheet sheet=book.getSheetAt(0);
        Object[][] data=new Object[sheet.getLastRowNum()][2]; 
        for(int i=1;i<=sheet.getLastRowNum();i++)
            for(int j=0;j<sheet.getRow(i).getLastCellNum();j++)
                Cell cell=sheet.getRow(i).getCell(j);
                switch(cell.getCellType()) {
                case STRING :
                case NUMERIC:
                default :
        for(int i=0;i<data.length;i++)
            for(int j=0;j<data[i].length;j++)
        return data;

I'm able to get username and password from excel using java but, I'm not understanding how to get the data in step definition in sendkeys.

1 Answer 1


You don't use the Test Parameters when using Cucumber, you use the Scenario outlines. So, when you have:

Scenario Outline: Login As User
When I log in as '<user>'
  | user          |
  | [email protected] |

You would have your test step definition handle that by using an Excel reader.

@When("^I log in as \"([^\"]*)\")
public void WhenILogInAsUser(user) {
  // import Excel file into file reader
  // get pass for {user}
  // perform login action(s)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.