【问题标题】:How to read excel file in cucumber project?如何读取黄瓜项目中的excel文件?
【发布时间】:2015-11-03 16:10:22
【问题描述】:

我正在使用 java 创建测试自动化框架,但我无法在 cucumber 中读取 excel 文件。

有什么方法可以使用 @DataProvider 功能 og testNG?

我不想使用特征文件的数据表。

【问题讨论】:

标签: testing selenium-webdriver cucumber cucumber-junit cucumber-java


【解决方案1】:

ExcelBDD Java版可以优雅的解决这个问题。代码示例

static Stream<Map<String, String>> provideExampleList() throws IOException {
    String filePath = TestWizard.getExcelBDDStartPath("excelbdd-test")
            + "excelbdd-test\\src\\test\\resources\\excel.xlsx";
    return Behavior.getExampleStream(filePath,"Expected1","Scenario1");
}

@ParameterizedTest(name = "Test{index}:{0}")
@MethodSource("provideExampleList")
void testgetExampleWithExpected(Map<String, String> parameterMap) {
    assertNotNull(parameterMap.get("Header"));
    System.out.println(String.format("=======Header: %s=======", parameterMap.get("Header")));
    for (Map.Entry<String, String> param : parameterMap.entrySet()) {
        System.out.println(String.format("%s --- %s", param.getKey(), param.getValue()));
    }
}

更多详情ExcelBDD Guideline By Java Example

【讨论】:

    【解决方案2】:

    如果对他人有帮助:

    在我的情况下,这非常有用,因为对于每个场景步骤,我必须加载 Excel 数据(来自具有相同组 ID 的多行的数据)才能执行进一步的验证。像这样,Cucumber 功能文件更简洁,而 Excel 包含所有细节。

    【讨论】:

      【解决方案3】:

      这里是如何从 excel 中读取 TestData 的示例

      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);
      		}

      这是模型黄瓜文件和测试数据。

      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

      这是模型步骤定义文件

      @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"));
      		
      	}

      在cucumber中按照以上三个步骤就可以读取测试数据了。

      【讨论】:

        【解决方案4】:

        如果您使用 CucumberJVM,我认为您无法在没有重大黑客攻击的情况下使用 TestNG 数据提供程序。或者至少这不是做事的“黄瓜方式”。 Data Table 相当于 Cucumber 的 TestNG Data Provider:

        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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-09-03
          • 1970-01-01
          • 1970-01-01
          • 2011-10-13
          • 1970-01-01
          • 1970-01-01
          • 2019-10-16
          相关资源
          最近更新 更多