使用 Cucumber 场景大纲处理 excel 个电子表格
Handling excel spreadsheets with Cucumber Scenario Outline
如果可能的话,我正在尝试寻找一种更优雅的方式来处理从与 excel spreadsheet row(nth) 相关的 Cucumber 场景大纲中调用第 n 个数字。
目前我正在使用迭代编号来定义 excel 传播的行号 sheet 以从中提取数据。我想看看是否有可能以比下面的场景大纲示例更优雅的方式将黄瓜与 excel 一起使用。
一些背景:
- 每次迭代都需要有自己的场景。因此,为什么我不使用
row.count.
的简单 for 循环
- 我完全知道场景大纲是一种制作数据表的方法,但我的公司希望看到一个 POF,我们可以通过 excel.
集成大型数据集
- 当前设置适用于小型数据集,但当我们进入大型 excel 传播时sheets 我不想在大纲上输入第 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();
}
您可以使用 QMetry Automation Framework with gherkin factory。它支持在特征文件之外提供的测试数据,例如 excel、xml、json、csv 或数据库。您可以为以下示例提供数据文件:
Examples:{'datafile':'resources/testdata.xls'}
这是example您可以查看的。
如果可能的话,我正在尝试寻找一种更优雅的方式来处理从与 excel spreadsheet row(nth) 相关的 Cucumber 场景大纲中调用第 n 个数字。
目前我正在使用迭代编号来定义 excel 传播的行号 sheet 以从中提取数据。我想看看是否有可能以比下面的场景大纲示例更优雅的方式将黄瓜与 excel 一起使用。
一些背景:
- 每次迭代都需要有自己的场景。因此,为什么我不使用 row.count. 的简单 for 循环
- 我完全知道场景大纲是一种制作数据表的方法,但我的公司希望看到一个 POF,我们可以通过 excel. 集成大型数据集
- 当前设置适用于小型数据集,但当我们进入大型 excel 传播时sheets 我不想在大纲上输入第 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();
}
您可以使用 QMetry Automation Framework with gherkin factory。它支持在特征文件之外提供的测试数据,例如 excel、xml、json、csv 或数据库。您可以为以下示例提供数据文件:
Examples:{'datafile':'resources/testdata.xls'}
这是example您可以查看的。