我使用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
步骤定义:公共类登录{
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 | username1 | password1
| ID2 | username2 |密码2
| ID3 | username3 | password3
| ID4 | username4 | password4
1 回答
如果要在Excel工作表中迭代内容,则需要在步骤定义的代码中实现该内容 . 在Gherkin中没有支持 .
实现迭代时,Apache POI可能是一个选项 .
重要的是要理解行为驱动开发BDD的目的是沟通 . 小黄瓜是一种交流方式 . 几乎任何了解问题的人都可以阅读和理解Gherkin场景 .
如果您在Gherkin中有一些事实,而在Excel中有一些事实,那么您将最终处于不使用Cucumber和Gherkin进行通信而是作为测试工具的情况 . 这可能没问题 . 但是如果你使用Cucumber作为测试工具,还有其他工具可能更容易使用 . JUnit就是其中之一 .