首页 文章

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

提问于
浏览
0

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

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

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

2 回答

  • 0

    如果你使用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

  • -1

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

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

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

相关问题