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. mybatis之generator生成代码

    首先在pom文件中引入以下代码 <plugin> <groupId>org.mybatis.generator</groupId> <artifactId&g ...

  2. a 标签 name 属性 页面定位 (二)

    <a href="to_url#somewhere">名字</a> <a name="somewhere">名字</a ...

  3. C# 单例模式(Singleton)

    摘要 在我们日常的工作中经常需要在应用程序中保持一个唯一的实例,如:IO处理,数据库操作等,由于这些对象都要占用重要的系统资源,所以我们必须限制这些实例的创建或始终使用一个公用的实例,这就是我们今天要 ...

  4. 【Web学习笔记】浅析CGI概念及用法

    1. CGI是什么         CGI是Common Gateway Interface的简写,它提供了一种标准方法使得位于WebServer后端的web应用可以根据client的请求动态生成网页 ...

  5. Raspberry Pi 3 安装 Lazarus 1.6.2(2017-02-09更新)

    Raspberry Pi3 Lazarus 1.6.2 安装步骤如下: 安装环境:Raspbian Jessie, RPi3 1.安装subversion和unzip Sudo Apt-get upd ...

  6. C#读取Excel的数据,并且以混合模式读取,防止数据类型变更

    /// <summary> /// Read Excel to DataSet /// </summary> /// <param name="filename ...

  7. Https&证书

    参考 http站点转https站点教程 http站点转https站点教程 HTTP与HTTPS的区别 Http站点转为Https: 为域名购买/申请证书(免费型DV SSL) 把证书部署到web se ...

  8. Performance profile of a typical interior scene

    ------------------------------- Num faces: 7000k+ Num lights: 38 Num textures: 79 Textures on disk: ...

  9. powshel 控制目录文件权限

    for($file=[IO.File]::OpenText("C:\Users\Administrator\Desktop\user_list.txt") ; !($file.En ...

  10. PHP 性能优化一

    PHP性能优化?对于这个问题,我们首先要知道影响PHP的性能的原因是什么? 1.什么情况下会出现PHP性能问题? 1)PHP语法使用 不当(包括某些业务可以使用PHP本身自带的函数来处理) 2)使用P ...