【发布时间】:2017-12-04 00:15:46
【问题描述】:
我使用 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 |用户名1 |密码1
|ID2 |用户名2 |密码2
|ID3 |用户名3 |密码3
|ID4 |用户名4 |密码4
【问题讨论】:
-
这几天我也在努力学习 Selenium。您应该搜索“Apache POI”API。我认为这将解决您的问题
-
感谢您的回复。我已经使用 Excel 作为我的数据表,但我不知道如何迭代 Cucumber-Selenium。基本上问题不是使用功能文件中的示例表,而是我想使用 excel,因为我正在处理数千个数据。
-
@Sachi 我认为这不可能。我不确定您想要实现的是数据驱动方法,还是您只想将数据抽象到不同的 excel 文件中。如果稍后您可以使用 Apache POI 执行此操作,但第一件事是不可能的。
标签: java excel selenium automation cucumber