1.Cucumber介绍

+ feature : read requirement
+scenario : testing situation,including
+ Given/
+ when/
+ then

Feature:用来描述我们需要测试的功能

Scenario: 用来描述测试场景

Given: 前置条件

When: 描述测试步骤

Then: 断言

features:用来存放自然语言的用例文件

step_definitions:用来存放java代码实现的测试用例

jars:cucumber-jvm的相关jar包都放在这里

implementation:存放被测试的代码,也就是项目的实现文件

refer link:

https://docs.cucumber.io/guides/10-minute-tutorial/

https://docs.cucumber.io/guides/browser-automation/

2。使用步骤

2.1 引入依赖

<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm-deps</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
<version>2.12.2</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- Cucumber Tag related jars (start) -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server-standalone</artifactId>
<version>3.12.0</version>
</dependency>

2.2新建test.future文件

Feature: Is it Friday yet?
Everybody wants to know when it's Friday Scenario: Sunday isn't Friday
Given today is Sunday
When I ask whether it's Friday yet
Then I should be told "Nope"

2.3在resource目录下创建cucumber.bat,执行bat,cucumber会读取test2.feature文件的内容,生成step定义代码,把这个生成的内容,粘贴进去自己定义的java类里com/cucumber/hellocucumber/Stepdefs.java:

@Given("^today is Sunday$")
public void today_is_Sunday() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
} @When("^I ask whether it's Friday yet$")
public void i_ask_whether_it_s_Friday_yet() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
} @Then("^I should be told \"([^\"]*)\"$")
public void i_should_be_told(String arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}

2。4编写测试类

在com/cucumber/hellocucumber目录下面编写com/cucumber/hellocucumber。java类,用junit执行run一下。会自动去读取

@RunWith(Cucumber.class)
@CucumberOptions(features={"src/main/resources/features"})
public class RunCucumberTest { }

一般用junit执行run一下,都会发现很多PendingException这时候就需要编写代码,使测试通过。

2.5编写代码使TodoSteps.java方法执行通过

public class Stepdefs {
private String today;
private String actualAnswer; @Given("^today is Sunday$")
public void today_is_Sunday() throws Throwable {
this.today = "Sunday";
} @When("^I ask whether it's Friday yet$")
public void i_ask_whether_it_s_Friday_yet() throws Throwable {
this.actualAnswer = IsItFriday.isItFriday(today);
} @Then("^I should be told \"([^\"]*)\"$")
public void i_should_be_told(String expectedAnswer) throws Throwable {
assertEquals(expectedAnswer, actualAnswer);
}
} IsItFriday。java 如下:
public class IsItFriday {
static String isItFriday(String today) {
if (today.equals("Friday")) {
return "TGIF";
}
return "Nope";
}
}

3.Using variables and examples

3.1 use to define the variable and examples

Feature: Is it Friday yet?
Everybody wants to know when it's Friday Scenario Outline: Today is or is not Friday
Given today is "<day>"
When I ask whether it's Friday yet
Then I should be told "<answer>" Examples:
| day | answer |
| Friday | TGIF |
| Sunday | Nope |
| anything else! | Nope |

3.2 define the step class

	@Given("^today is \"([^\"]*)\"$")
public void today_is(String today) throws Throwable {
// Write code here that turns the phrase above into concrete actions
this.today = today;
//throw new PendingException();
} @When("^I ask whether it's Friday yet$")
public void i_ask_whether_it_s_Friday_yet() throws Throwable {
this.actualAnswer = IsItFriday.isItFriday(today);
} @Then("^I should be told \"([^\"]*)\"$")
public void i_should_be_told(String expectedAnswer) throws Throwable {
assertEquals(expectedAnswer, actualAnswer);
}

4。Browser Automation

Cucumber本身不是一个浏览器自动化测试工具,但是通过和Selenium WebDriver结合可以完成一些简单的浏览器自动化测试工作。

4.1 依赖

		<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server-standalone</artifactId>
<version>3.12.0</version>
</dependency>

4.2prepare

如果你使用的时候chrom浏览器,那么需要在系统属性里面设置chromedriver.exe的路径

System.setProperty("webdriver.chrome.driver", "C:/swdtools/Chrome/chromedriver.exe");

4.3定义feature文件web.feature

Feature: search cheese on google

	Scenario: Finding some cheese
Given I am on the Baidu search page
When I search for "Cheese!"
Then the page title should start with "cheese"

4.4定义step类

public class ExampleSteps {
private final WebDriver driver = WebDriverFactory.createWebDriver();
@Given("^I am on the Baidu search page$")
public void I_visit_google() {
driver.get("https:\\www.baidu.com");
} @When("^I search for \"(.*)\"$")
public void search_for(String query) {
WebElement element = driver.findElement(By.id("kw"));
// Enter something to search for
element.sendKeys(query);
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
} @Then("^the page title should start with \"(.*)\"$")
public void checkTitle(String titleStartsWith) {
// Google's search is rendered dynamically with JavaScript
// Wait for the page to load timeout after ten seconds
new WebDriverWait(driver,10L).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese");
// Should see: "cheese! -Google Search"
}
});
} @After()
public void closeBrowser() {
driver.quit();
}
}

4.5编写测试类

@RunWith(Cucumber.class)

@CucumberOptions(features={"src/main/resources/features"})

public class RunCucumberTest {

}

cucumber & selenium & bddtest的更多相关文章

  1. 行为驱动:Cucumber + Selenium + Java(一) - 环境搭建

    1.1 什么是行为驱动测试 说起行为驱动,相信很多人听说过. 行为驱动开发-BDD(Behavior Driven Development)是一个诞生于2003年的软件开发理念.其关键思想在于通过与利 ...

  2. 使用cucumber & selenium实现一个简单的bddtest

    1.Cucumber介绍 + feature : read requirement +scenario : testing situation,including + Given/ + when/ + ...

  3. 行为驱动:Cucumber + Selenium + Java(五) - 使用maven来实现cucumber测试和报告

    在上一篇中,我们介绍了Selenium + Cucumber + Java框架下的测试用例参数化/数据驱动,这一篇我们来使用maven去搭建cucumber框架以及实现测试报告. 5.1 为什么要用m ...

  4. 行为驱动:Cucumber + Selenium + Java(四) - 实现测试用例的参数化

    在上一篇中,我们介绍了Selenium + Cucumber + Java框架下的使用Tags对测试用例分组的实现方法,这一篇我们用数据表格来实现测试用例参数化. 4.1 什么是用例参数化 实际测试中 ...

  5. 行为驱动:Cucumber + Selenium + Java(三) - 使用标签实现测试分组

    在上一篇中,我们写出了Selenium + Cucumber + Java环境下的第一个BDD自动化测试用例,这一篇我们说说怎么用标签对用例进行分组. 3.1 Cucumber标签 实际工作中,我们的 ...

  6. 行为驱动:Cucumber + Selenium + Java(二) - 第一个测试

    在上一篇中,我们搭建好了Selenium + Cucumber + Java的自动化测试环境,这一篇我们就赶紧开始编写我们的第一个BDD测试用例. 2.1 创建features 我们在新建的java项 ...

  7. 行为驱动:Cucumber + Selenium + Java(一) - Cucumber简单操作实例

    场景(Scenarios) 场景是Cucumber结构的核心之一.每个场景都以关键字“Scenario:”(或本地化一)开头,后面是可选的场景标题.每个Feature可以有一个或多个场景,每个场景由一 ...

  8. cucumber+selenium

    工程结构 pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="ht ...

  9. 行为驱动:Cucumber + Selenium + Java(二) - extentreports 测试报告+jenkins持续集成

    1.extentreports 测试报告 pom文件 <dependency> <groupId>com.vimalselvam</groupId> <art ...

随机推荐

  1. git hook 自动部署

    1. 当前虚拟站点根目录的 .git/ 权限 2. 当前项目裸仓库创建 hooks/post-receive 文件,并给予x 的权限 3. 复制如下内容 #!/bin/sh unset $(git r ...

  2. 【小梅哥SOPC学习笔记】NIOS II工程目录改变时project无法编译问题

    解决NIOS II工程移动在磁盘上位置后project无法编译问题 说明:本文档于2017年3月4日由小梅哥更新部分内容,主要是增加了讲解以Quartus II13.0为代表的经典版本和以15.1为代 ...

  3. [LeetCode 题解]:Path Sum

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a bi ...

  4. Mathcad操作tips:算式输入、变量定义与计算

    算式输入 1. 数字与符号相乘,输入时不必手动输入乘号(“*”). 2. 以下有助于算式的可视化:a. 使用Math工具栏输入,并合理使用tab键:b. 合理使用空格键. 3. 输入开根号时,可用快捷 ...

  5. async异步操作和同步上下文

    第8章 哪个线程运行我的代码 看到社区里的朋友没有翻译完这本书,我接着对一下的章节进行翻译 像我之前说的,异步编程就是关于线程的.那就意味着我们需要理解在C#程序中哪个.NET线程什么时候运行我们的代 ...

  6. 本地连接腾讯云Mysql失败问题

    腾讯云主机中MySQL无法远程连接的解决办法 在远程主机上,我开启了 mysql服务,用 phpmyadmin 可以打开,比如说用户名为 root,密码为 123456.不过用 Mysql 客户端远程 ...

  7. centos7 安装git

    centos7下git的安装和配置   git的安装: yum 源仓库里的 Git 版本更新不及时,最新版本的 Git 是 1.8.3.1,但是官方最新版本已经到了 2.9.2.想要安装最新版本的的 ...

  8. Binder学习笔记(一)

    网上看了很多关于binder的文章,但我还是想把自己的心路历程记录下来,有些是跟着别人的脚步领略险峻风景,有些则是自己只身探入代码深处打捞出的收获.我不确定是否全部融会贯通,更担心一两个月后会完全不记 ...

  9. dataframe去重 drop_duplicates

    data.drop_duplicates() #默认:data中一行元素全部相同时才去除 data.drop_duplicates(['a','b'])#data根据’a','b'组合列删除重复项,默 ...

  10. linux 安装python3.7 报错No module named '_ctypes'

    ModuleNotFoundError: No module named '_ctypes' 操作系统:centos yum install libffi-devel ./configure --en ...