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. Appium常用API(一)

    Appium作为当下一款移动应用的自动化测试工具,对于测试来说重要性不言可寓,废话不多说,下面总结下它常用的API: 1.contextscontexts(self): Returns the con ...

  2. mysql - 查看表字段和字段描述

    1.mysql查看表字段和字段描述 SELECT column_name, column_comment FROM information_schema.columns WHERE table_sch ...

  3. 每天多学一个Linux命令--man

    今天学习的是“有问题,找男人”.man是manual(操作说明)的简写.它提供强大的帮助文档,当你需要查看某个命令的参数时不必到处上网查找,只要man一下即可. 第一列代表的是章节代号,第二列代表的是 ...

  4. cxgrid取消过滤下拉框

    选择tableview1.optionscustomize.columnfiltering=fasle;

  5. CentOS 下 zookeeper 安装

     搭建zookeeper需要几个条件 a. 配置Java环境  c. centos d. 下载 xshell5 (下载它只是为了更方便的使用linux)     一.新建一个myapp目录: 二.下载 ...

  6. C#面向对象的三大基本特征

    封装: 封装是指将数据与具体操作的实现代码放在某个对象内部,使这些代码的实现细节不被外界发现(可以使代码更加安全),外界只能通过接口使用该对象,而不能通过任何形式修改对象内部实现,正是由于封装机制,程 ...

  7. 三个数组求中位数,以及中位数的中位数----java算法实现

    求三个数组的中位数,以及中位数的中位数.   import java.util.Arrays; public class median { public static void main(String ...

  8. 当在安卓低版本呈现的界面(H5)出现问题的时候,我们怎么解决?

    昨天,在医院现场的客服人员,向我们反馈一个问题:说一位用户用他的安卓手机打开我们的app之后,界面是乱掉的:如下图: 向客服询问了具体的设备信息:安卓系统版本号是4.2 下意识觉得是因为css的兼容问 ...

  9. pageadmin网站制作 如何修改和管理网站模板

    在使用pageadmin CMS 的同时,遇到问题可以参考官网帮助中心.1.网站模板目录地址/templates目录, 2.点击展开后,每个目录就是一个网站模板, 前端设计师制作的新版本都可以放到这个 ...

  10. 转载-----Java Longest Palindromic Substring(最长回文字符串)

    转载地址:https://www.cnblogs.com/clnchanpin/p/6880322.html 假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic st ...