黄瓜硒使用 Excel 文件作为数据表
我使用 Cucumber-Selenium 和 Excel 作为我的数据文件,我的问题是如何根据我在 Excel 上的数据多次运行我的功能文件.例如我在Excel中有10行数据,想一个一个地运行,在第一行数据之后它会移动到下一行并执行它.
I am using Cucumber-Selenium and Excel as my Data file, my question is how can I run my feature file multiple time based on the data I have on the Excel. For Example I have 10 rows of data in Excel and wanted to run it one by one, after the first row of data it will move to the next row and execute it.
功能文件:场景:登录
Given I open the browser and access this URL
When I enter the "<Username>" and "<Password>"
Then I am able to login
步骤定义:公共类登录 {
Step Definition: public class 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 文件)
Here is my Data Table(Excel File)
|ID |用户名 |密码
|ID | UserName | Password
|ID1 |用户名1 |密码1
|ID1 |username1 |password1
|ID2 |用户名2 |密码2
|ID2 | username2 | password2
|ID3 |用户名3 |密码3
|ID3 | username3 | password3
|ID4 |用户名4 |密码4
|ID4 | username4 | password4
推荐答案
如果你想迭代 Excel 工作表中的内容,你需要在步骤定义中的代码中实现它.Gherkin 不支持这样做.
If you want to iterate of the content in an Excel sheet you need to implement that in code in the step definition. There is no support for doing it in Gherkin.
Apache POI 可能是实现迭代时的一个选项.
Apache POI may be an option when implementing the iteration.
了解行为驱动开发(BDD)的目的是沟通是很重要的.小黄瓜是一种交流方式.几乎任何了解问题的人都可以阅读和理解 Gherkin 场景.
It is important to understand that the purpose of Behaviour-Driven Development, BDD, is communication. Gherkin is one way of communicating. The Gherkin scenarios can be read and understood by almost anyone understanding the problem.
如果您在 Gherkin 中掌握了一些真相,而在 Excel 中掌握了一些真相,那么您最终会遇到这样的情况:您不使用 Cucumber 和 Gherkin 进行交流,而是将其用作测试工具.这可能没问题.但是如果你使用 Cucumber 作为测试工具,还有其他工具可能更容易使用.JUnit 就是其中之一.
If you have some of the truth in Gherkin and some in Excel you will end up in a situation where you don't use Cucumber and Gherkin for communication but rather as a test tool. This may be ok. But if you use Cucumber as a test tool, there are other tools that may be easier to use. JUnit is one of them.
相关文章