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);
    }
}
  • 启动器
@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插件
  • 项目构建完的效果如下图:
    这里写图片描述
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值