java方式实现BDD框架的自然语言(Cucumber-java)
项目创建
- 创建maven项目,在pom中添加依赖包:
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-testng</artifactId>
<version>1.2.4</version>
</dependency>
- 项目目录如下:
cucumber用例编写
- 创建feature文件
Feature: 在百度中搜索
@TestngScenario
Scenario: 搜索testng
Given 打开百度,验证title
When 输入 "testng"
Then 点击搜索按钮
Then 清除搜索框
- feature文件支持的语言输出内容:
| feature | "功能" |
| background | "背景" |
| scenario | "场景", "剧本" |
| scenario outline | "场景大纲", "剧本大纲" |
| examples | "例子" |
| given | "* ", "假如", "假设", "假定" |
| when | "* ", "当" |
| then | "* ", "那么" |
| and | "* ", "而且", "并且", "同时" |
| but | "* ", "但是" |
| given (code) | "假如", "假设", "假定" |
| when (code) | "当" |
| then (code) | "那么" |
| and (code) | "而且", "并且", "同时" |
| but (code) | "但是" |
- 用例执行步骤编写
public class Search extends CucumbeRunner{
@Given("^打开百度,验证title$")
public void setp1() throws Throwable {
assertEquals(BrowserDriver.dr.getTitle(), "百度一下,你就知道");
}
@When("^输入 \"(.*?)\"$")
public void setp2(String text) throws Throwable {
driver.type("//*[@id='kw']", text);
}
@Then("^点击搜索按钮$")
public void setp3() throws Throwable {
driver.click("//*[@id='su']");
}
@Then("^清除搜索框$")
public void setp4() throws Throwable {
String[] ss = {"//*[@id='kw']"};
driver.clearText(ss);
}
}
<