1.Cucumber介绍

  1. + feature : read requirement
  2. +scenario : testing situation,including
  3. + Given/
  4. + when/
  5. + 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 引入依赖

  1. <dependency>
  2. <groupId>info.cukes</groupId>
  3. <artifactId>cucumber-java</artifactId>
  4. <version>1.2.5</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>info.cukes</groupId>
  8. <artifactId>cucumber-core</artifactId>
  9. <version>1.2.5</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>info.cukes</groupId>
  13. <artifactId>cucumber-jvm-deps</artifactId>
  14. <version>1.0.5</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>info.cukes</groupId>
  18. <artifactId>gherkin</artifactId>
  19. <version>2.12.2</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>info.cukes</groupId>
  23. <artifactId>cucumber-junit</artifactId>
  24. <version>1.2.5</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>junit</groupId>
  28. <artifactId>junit</artifactId>
  29. <version>4.11</version>
  30. </dependency>
  31. <!-- Cucumber Tag related jars (start) -->
  32. <dependency>
  33. <groupId>org.seleniumhq.selenium</groupId>
  34. <artifactId>selenium-server-standalone</artifactId>
  35. <version>3.12.0</version>
  36. </dependency>

2.2新建test.future文件

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

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

  1. @Given("^today is Sunday$")
  2. public void today_is_Sunday() throws Throwable {
  3. // Write code here that turns the phrase above into concrete actions
  4. throw new PendingException();
  5. }
  6. @When("^I ask whether it's Friday yet$")
  7. public void i_ask_whether_it_s_Friday_yet() throws Throwable {
  8. // Write code here that turns the phrase above into concrete actions
  9. throw new PendingException();
  10. }
  11. @Then("^I should be told \"([^\"]*)\"$")
  12. public void i_should_be_told(String arg1) throws Throwable {
  13. // Write code here that turns the phrase above into concrete actions
  14. throw new PendingException();
  15. }

2。4编写测试类

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

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

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

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

  1. public class Stepdefs {
  2. private String today;
  3. private String actualAnswer;
  4. @Given("^today is Sunday$")
  5. public void today_is_Sunday() throws Throwable {
  6. this.today = "Sunday";
  7. }
  8. @When("^I ask whether it's Friday yet$")
  9. public void i_ask_whether_it_s_Friday_yet() throws Throwable {
  10. this.actualAnswer = IsItFriday.isItFriday(today);
  11. }
  12. @Then("^I should be told \"([^\"]*)\"$")
  13. public void i_should_be_told(String expectedAnswer) throws Throwable {
  14. assertEquals(expectedAnswer, actualAnswer);
  15. }
  16. }
  17. IsItFridayjava 如下:
  18. public class IsItFriday {
  19. static String isItFriday(String today) {
  20. if (today.equals("Friday")) {
  21. return "TGIF";
  22. }
  23. return "Nope";
  24. }
  25. }

3.Using variables and examples

3.1 use to define the variable and examples

  1. Feature: Is it Friday yet?
  2. Everybody wants to know when it's Friday
  3. Scenario Outline: Today is or is not Friday
  4. Given today is "<day>"
  5. When I ask whether it's Friday yet
  6. Then I should be told "<answer>"
  7. Examples:
  8. | day | answer |
  9. | Friday | TGIF |
  10. | Sunday | Nope |
  11. | anything else! | Nope |

3.2 define the step class

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

4。Browser Automation

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

4.1 依赖

  1. <dependency>
  2. <groupId>org.seleniumhq.selenium</groupId>
  3. <artifactId>selenium-server-standalone</artifactId>
  4. <version>3.12.0</version>
  5. </dependency>

4.2prepare

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

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

4.3定义feature文件web.feature

  1. Feature: search cheese on google
  2. Scenario: Finding some cheese
  3. Given I am on the Baidu search page
  4. When I search for "Cheese!"
  5. Then the page title should start with "cheese"

4.4定义step类

  1. public class ExampleSteps {
  2. private final WebDriver driver = WebDriverFactory.createWebDriver();
  3. @Given("^I am on the Baidu search page$")
  4. public void I_visit_google() {
  5. driver.get("https:\\www.baidu.com");
  6. }
  7. @When("^I search for \"(.*)\"$")
  8. public void search_for(String query) {
  9. WebElement element = driver.findElement(By.id("kw"));
  10. // Enter something to search for
  11. element.sendKeys(query);
  12. // Now submit the form. WebDriver will find the form for us from the element
  13. element.submit();
  14. }
  15. @Then("^the page title should start with \"(.*)\"$")
  16. public void checkTitle(String titleStartsWith) {
  17. // Google's search is rendered dynamically with JavaScript
  18. // Wait for the page to load timeout after ten seconds
  19. new WebDriverWait(driver,10L).until(new ExpectedCondition<Boolean>() {
  20. public Boolean apply(WebDriver d) {
  21. return d.getTitle().toLowerCase().startsWith("cheese");
  22. // Should see: "cheese! -Google Search"
  23. }
  24. });
  25. }
  26. @After()
  27. public void closeBrowser() {
  28. driver.quit();
  29. }
  30. }

4.5编写测试类

@RunWith(Cucumber.class)

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

public class RunCucumberTest {

}

使用cucumber & selenium实现一个简单的bddtest的更多相关文章

  1. python+selenium之自定义封装一个简单的Log类

    python+selenium之自定义封装一个简单的Log类 一. 问题分析: 我们需要封装一个简单的日志类,主要有以下内容: 1. 生成的日志文件格式是 年月日时分秒.log 2. 生成的xxx.l ...

  2. Python+Selenium中级篇之8-Python自定义封装一个简单的Log类《转载》

    Python+Selenium中级篇之8-Python自定义封装一个简单的Log类: https://blog.csdn.net/u011541946/article/details/70198676

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

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

  4. Selenium + PhantomJS + python 简单实现爬虫的功能

    Selenium 一.简介 selenium是一个用于Web应用自动化程序测试的工具,测试直接运行在浏览器中,就像真正的用户在操作一样 selenium2支持通过驱动真实浏览器(FirfoxDrive ...

  5. 一个简单的JUnit项目

    本人一直很喜欢JAVA,可是真正接触到JUnit也不过半年.由于公司进行网页测试,采用的是 JUnit+selenium的方式搭建的测试框架,然后采用JAVA语言编写,所以本人也好好研究了一下JUni ...

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

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

  7. python 搭建一个简单的 搜索引擎

    我把代码和爬好的数据放在了git上,欢迎大家来参考 https://github.com/linyi0604/linyiSearcher 我是在 manjaro linux下做的, 使用python3 ...

  8. 爬虫基础以及一个简单的实例(requests,re)

    最近在看爬虫方面的知识,看到崔庆才所著的<Python3网络爬虫开发实战>一书讲的比较系统,果断入手学习.下面根据书中的内容,简单总结一下爬虫的基础知识,并且实际练习一下.详细内容请见:h ...

  9. 哪种缓存效果高?开源一个简单的缓存组件j2cache

    背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...

随机推荐

  1. loj10087 Intervals

    传送门 分析 我们设S[i]表示到第i个数为止一共有多少个数在集合Z之中,最终答案就是S[max]-S[min]的最小值.所以我们不难发现对于每一个[ai,bi]都表示S[bi]-S[ai-1]> ...

  2. Postman工具---请求与响应

    参考:http://blog.csdn.net/water_0815/article/details/53311561

  3. java Linkedhashmap源码分析

    LinkedHashMap类似于HashMap,但是迭代遍历它时,取得“键值对”的顺序是插入次序,或者是最近最少使用(LRU)的次序.只比HashMap慢一点:而在迭代访问时反而更快,因为它使用链表维 ...

  4. boost.asio系列(一)——deadline_timer

    一.构造函数 一个deadline_timer只维护一个超时时间,一个deadline_timer不同时维护多个定时器.在构造deadline_timer时指定时间: basic_deadline_t ...

  5. web.xml 注册中央调度器Url-pattern 要注意的地方(五)

    一.url-pattern 里面不能用/*     二.最好不要写成杠,如果使用“/”,中央调度器会把静态资源拦截掉,比如图片会不显示,或者css不能用.   但,如果地址栏传参数的时候比如是/del ...

  6. C#模拟进度条

    自己看源码 using System; namespace ConsoleTest { class Program { static void Main(string[] args) { Consol ...

  7. 写RestApi需要注意些什么?

    PS1="\n[\e[32;1m]([\e[37;1m]\u[\e[32;1m])-([\e[37;1m]jobs:\j[\e[32;1m])-([\e[37;1m]\w[\e[32;1m] ...

  8. java基础之流程控制语句

    一.     分支 1.      三元运算符 ?: 注意:三元运算符虽然简洁但是语法乱,而且必须要有接受者或者直接打印 1.     if else语句 另一种不带括号的写法: if(条件) 语句1 ...

  9. Python列表知识补充

    1.import this  Python之禅,圣经. >>> import this The Zen of Python, by Tim Peters Beautiful is b ...

  10. socket 中read返回0的情况

    当client,调用read(socketfd,buffer,n)时,返回0的情况: 1.server端调用了close(soketfd)函数 2.server调用了close(fd,SHUT_WR) ...