public class Framework {
static String TestDataPath = System.getProperty("user.dir")
+ "\\ExcelFiles\\TestData.xlsx";
public static HashMap<String, HashMap<String, String>> hm1 = new HashMap<>();
static String s3;
public static void ReadTestData() throws IOException {
FileInputStream file = new FileInputStream(TestDataPath);
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheet("Sheet1");
Row HeaderRow = sheet.getRow(0);
for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
Row currentRow = sheet.getRow(i);
HashMap<String, String> currentHash = new HashMap<String, String>();
for (int j = 0; j < currentRow.getPhysicalNumberOfCells(); j++) {
Cell currentCell1 = currentRow.getCell(0);
switch (currentCell1.getCellType()) {
case Cell.CELL_TYPE_STRING:
s3 = currentCell1.getStringCellValue();
System.out.println(s3);
break;
case Cell.CELL_TYPE_NUMERIC:
s3 = String.valueOf(currentCell1.getNumericCellValue());
System.out.println(s3);
break;
}
Cell currentCell = currentRow.getCell(j);
switch (currentCell.getCellType()) {
case Cell.CELL_TYPE_STRING:
currentHash.put(HeaderRow.getCell(j).getStringCellValue(),
currentCell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
currentHash.put(HeaderRow.getCell(j).getStringCellValue(),
String.valueOf(currentCell.getNumericCellValue()));
break;
}
}
hm1.put(s3, currentHash);
}
这是Model黄瓜文件和testData .
Scenario Outline: Successful Login with Valid Credentials
Given User is on Home Page
When User Navigate to LogIn Page
And User enters mandatory details of "<TextCase>"
Then Message displayed Login Successfully
Examples:
|TextCase|
|Case1 |
|Case2 |
[Test data img Link][1]
[1]: https://i.stack.imgur.com/IjOap.png
这是Model Stepdefination文件
@When("^User enters mandatory details of \"([^\"]*)\"$")
public void user_enters_mandatory_details_of(String arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
driver.FindElement("UserName").sendKeys(Framework.hm1.get(arg1).get("UserName"));
Framework.FindElement("Password").sendKeys(Framework.hm1.get(arg1).get("Password"));
}
2 回答
如果你使用CucumberJVM,我认为你不能在没有重大攻击的情况下使用TestNG数据提供者 . 或者至少这不是做事的“黄瓜方式” . 数据表是TestNG数据提供者的黄瓜等价物:
https://cucumber.io/docs/reference#data-tables
这就是你如何在Cucumber中进行参数测试 . 我不是说你要找的解决方案无法实现,我说你最有可能找错了 . CucumberJVM在内部使用DataProviders,以这种方式处理功能:
https://github.com/cucumber/cucumber-jvm/blob/master/testng/src/main/java/cucumber/api/testng/AbstractTestNGCucumberTests.java
以下是如何从excel读取TestData的示例
这是Model黄瓜文件和testData .
这是Model Stepdefination文件
按照黄瓜上面的三个步骤,您将能够读取测试数据 .