0

  • 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 :
            {
                data[i-1][j]=cell.getStringCellValue();
            }
            case NUMERIC:
            {
                data[i-1][j]=cell.getStringCellValue();
            }
            default :
            {
                data[i-1][j]=cell.getStringCellValue();
            }
            }
        }
    }
    for(int i=0;i<data.length;i++)
    {
        for(int j=0;j<data[i].length;j++)
        {
            System.out.println(data[i][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 stepdefinition in sendkeys

0

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>'
  Examples:
  | user          |
  | xyz@gmail.com |

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)
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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