Cucumber-java初使用

来源:互联网 发布:淘宝代理加盟违法吗 编辑:程序博客网 时间:2024/05/16 23:58

项目创建

  • 创建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);    }}
  • 启动器
@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插件
  • 项目构建完的效果如下图:
    这里写图片描述