使用Cucumber Scenario Outline

时间:2016-08-09 14:01:49

标签: java excel selenium cucumber qaf

我试图看,如果可能的话,有一个更优雅的方法来处理从与黄金表格行相关的nch数字中调用nTh数字(第n个)。

目前我正在使用迭代编号来定义excel电子表格的行#以从中提取数据。我想看看是否有可能以比下面的例子更优雅的方式使用带有excel的黄瓜和场景轮廓。

一些背景知识:

  • 每次迭代都需要是自己的场景。因此我为什么不使用 使用row.count简单循环。
  • 我完全了解情景大纲作为数据表的一种方式,但我的公司希望看到一个可以通过Excel集成大型数据集的POF。
  • 当前设置适用于小型数据集,但是当我们进入大型Excel电子表格时,我不想在大纲上输入第n个数字

黄瓜代码:

Feature: User is using an excel spreadsheet with cucumber driving it

  Scenario Outline: Data Driven with excel and data sets

   When I am on the amps mainscreen
    Then I input username and passwords with excel row"<row_index>" dataset

    Examples:
    | row_index  |
    | 1          |
    | 2          |
    | 3          |
    | 4          |

步骤文件:

//Excel Steps
@When("^I am on the amps mainscreen$")
public void i_am_on_the_amps_mainscreen()  {
    System.out.println("Im loading");
}
//Excel Steps
@Then("^I input username and passwords with excel row\"([^\"]*)\" dataset$")
public void i_input_username_and_passwords_with_excel_row_dataset(int rownum)    throws IOException {
    login.readExcel(rownum);
}

实际代码:

 public void readExcel (int row) throws IOException{

    File src=new File("src/test/resources/username.xlsx");
    FileInputStream fis=new FileInputStream(src);
    XSSFWorkbook srcBook= new XSSFWorkbook(fis);
    XSSFSheet sourceSheet = srcBook.getSheetAt(0);

    XSSFRow sourceRow = sourceSheet.getRow(row);
    XSSFCell username=sourceRow.getCell(0);
    XSSFCell password=sourceRow.getCell(1);
    String userExcel = username.getStringCellValue();
    String pwExcel = password.getStringCellValue();
    System.out.println("The username is" +userExcel);
    System.out.println("The password is" +pwExcel);
    log.info("The username on " +row + " is: "+userExcel);
    log.info("The password on "+row+ " is: "+pwExcel);
    driver.findElement(txtbox_username).sendKeys(userExcel);
    driver.findElement(txtbox_password).sendKeys(pwExcel);
    driver.findElement(btn_logon).click();

}

1 个答案:

答案 0 :(得分:2)

您可以将QMetry Automation Frameworkgherkin factory一起使用。它支持在特征文件外部提供的测试数据,例如excel,xml,json,csv或数据库。您可以提供数据文件,例如:

  

实例:{&#39;数据文件&#39;:&#39;资源/ testdata.xls&#39;}

以下是您可以查看的example