项目创建
<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>
![这里写图片描述](https://img-blog.csdn.net/20170523155403568?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3UxMzIyMzM5NDY2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
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插件,如下:
![cucumber插件](https://img-blog.csdn.net/20170523161039814?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3UxMzIyMzM5NDY2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
- 项目构建完的效果如下图:
![这里写图片描述](https://img-blog.csdn.net/20170523161250457?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3UxMzIyMzM5NDY2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)