项目创建
<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: 在百度中搜索
@TestngScenario
Scenario: 搜索testng
Given 打开百度,验证title
When 输入 "testng"
Then 点击搜索按钮
Then 清除搜索框
| 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);
}
}
@CucumberOptions(strict = true, monochrome = true, features = "src/test/resources/features",
glue = "testcase",
plugin = {"pretty", "html:target/cucumber-html-report;","json:target/cucumber.json" },
tags = { "@TestngScenario" })
public class CucumbeRunner extends AbstractTestNGCucumberTests {
public static BrowserDriver driver = null;
@BeforeSuite(alwaysRun = true)
public void setUp() throws Exception {
driver = new BrowserDriver("http://www.baidu.com");
}
@AfterSuite(alwaysRun = true)
public void quit() throws IOException, InterruptedException {
driver.close();
}
}
与jenkins集成
- 在jenkins中安装cucumber插件,如下:
- 项目构建完的效果如下图: