如何在黄瓜项目中阅读excel文件?

时间:2015-08-11 12:23:44

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

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

有没有办法使用@DataProvider功能og testNG?

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

4 个答案:

答案 0 :(得分:1)

如果您使用CucumberJVM,我认为您可以使用TestNG数据提供程序而不会出现重大攻击。或者至少这不是黄瓜的方式"做事。数据表是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

答案 1 :(得分:0)

以防万一:

在我的情况下,这非常有用,因为对于每个方案步骤,我都必须加载Excel数据(来自具有相同组ID的多行数据)以执行进一步的验证。这样,Cucumber功能文件就更干净了,而Excel却隐藏了所有细节。

答案 2 :(得分:0)

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

答案 3 :(得分:-2)

以下是如何从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);
		}

这是模型黄瓜文件和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"));
		
	}

按照黄瓜的上述三个步骤,您将能够读取测试数据。