java 使用 Cucumber Scenario Outline 处理 Excel 电子表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38852952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:49:51  来源:igfitidea点击:

Handling excel spreadsheets with Cucumber Scenario Outline

javaexcelseleniumcucumberqaf

提问by Carl Laneave

I am trying to see, if possible, to have a more elegant way to handle calling nTh numbers from a Cucumber Scenario Outline that correlates to an excel spreadsheet row(nth).

如果可能的话,我试图找到一种更优雅的方法来处理从与 Excel 电子表格行(第 n 个)相关的黄瓜场景大纲中调用第 n 个数字。

Currently I am using iteration numbers to define the row # of the excel spread sheet to pull the data from. I wanted to see if it was possible to use cucumber with excel in a more elegant way than the below example with the scenario outline.

目前,我正在使用迭代编号来定义要从中提取数据的 Excel 电子表格的行号。我想看看是否有可能以比下面带有场景大纲的示例更优雅的方式将 Cucumber 与 excel 结合使用。

Some background:

一些背景:

  • Each iteration needs to be its own scenario. Hence why I'm not using a simple for loop with row.count.
  • I am fully aware of scenario outline as a way to do data tables but my company wants to see a POF that we can integrate large data sets through excel.
  • Current setup works for small data sets but when we get into the large excel spreadsheets I do not want to type out nth numbers on the outline
  • 每个迭代都需要是它自己的场景。因此,为什么我不使用带有 row.count 的简单 for 循环。
  • 我完全了解场景大纲作为一种制作数据表的方式,但我的公司希望看到一个 POF,我们可以通过 excel 集成大型数据集。
  • 当前设置适用于小数据集,但是当我们进入大型 Excel 电子表格时,我不想在大纲上输入第 n 个数字

Cucumber code:

黄瓜代码:

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          |

Step File:

步骤文件:

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

Actual Code:

实际代码:

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

}

采纳答案by user861594

You can use QMetry Automation Frameworkwith gherkin factory. It supports test data provided outside feature file for example excel, xml, json, csv, or database. you can provide datafile for examples like:

您可以将QMetry 自动化框架小黄瓜工厂一起使用。它支持在特征文件之外提供的测试数据,例如 excel、xml、json、csv 或数据库。您可以为以下示例提供数据文件:

Examples:{'datafile':'resources/testdata.xls'}

示例:{'datafile':'resources/testdata.xls'}

Here is the exampleyou can check.

这是您可以检查的示例