我使用 Cucumber-Selenium 和 Excel 作为我的数据文件,我的问题是如何根据 Excel 上的数据多次运行我的功能文件。例如,我在Excel中有10行数据,想要逐条运行它,在第一行数据之后它将移动到下一行并执行它。
功能文件: 场景:登录
Given I open the browser and access this URL
When I enter the "<Username>" and "<Password>"
Then I am able to login
步骤定义: 公共(public)类登录{
WebDriver driver = null;
String url;
@Given("^I open the browser and access this URL$")
public void navigateToUrl() throws Throwable{
System.setProperty("webdriver.chrome.driver", "");
driver = new ChromeDriver();
url = DataTable.getDataTableValue(0, 2, 2);
driver.get(url);
driver.manage().window().maximize();
}
@When("^I enter the \"([^\"]*)\" and \"([^\"]*)\"$")
public void enterCredentials(String userName, String password ) throws Throwable {
userName = DataTable.getDataTableValue(0, 1, 1);
password = DataTable.getDataTableValue(0, 1, 2);
driver.findElement(By.id("username")).sendKeys(userName);
driver.findElement(By.id("password")).sendKeys(password);
}
@Then("^I am able to login$")
public void clickLoginButton() throws Throwable {
driver.findElement(By.id("Login")).click();
}
}
这是我的数据表(Excel 文件)
|ID |用户名 |密码
|ID1 |用户名1 |密码1
|ID2|用户名2 |密码2
|ID3 |用户名3 |密码3
|ID4|用户名4 |密码4
最佳答案
如果您想要迭代 Excel 工作表中的内容,您需要在步骤定义中的代码中实现该操作。在 Gherkin 中不支持这样做。
Apache POI 可能是实现迭代时的一个选项。
重要的是要理解行为驱动开发 (BDD) 的目的是沟通。小 cucumber 是一种沟通方式。几乎任何理解该问题的人都可以阅读和理解 Gherkin 场景。
如果您在 Gherkin 中掌握了一些真理,在 Excel 中掌握了一些真理,那么您最终会遇到这样的情况:您不使用 Cucumber 和 Gherkin 进行通信,而是将其用作测试工具。这可能没问题。但如果你使用 Cucumber 作为测试工具,还有其他工具可能更容易使用。 JUnit 就是其中之一。
关于java - Cucumber Selenium 使用 Excel 文件作为数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44839635/