cucumber & selenium & bddtest
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的更多相关文章
- 行为驱动:Cucumber + Selenium + Java(一) - 环境搭建
1.1 什么是行为驱动测试 说起行为驱动,相信很多人听说过. 行为驱动开发-BDD(Behavior Driven Development)是一个诞生于2003年的软件开发理念.其关键思想在于通过与利 ...
- 使用cucumber & selenium实现一个简单的bddtest
1.Cucumber介绍 + feature : read requirement +scenario : testing situation,including + Given/ + when/ + ...
- 行为驱动:Cucumber + Selenium + Java(五) - 使用maven来实现cucumber测试和报告
在上一篇中,我们介绍了Selenium + Cucumber + Java框架下的测试用例参数化/数据驱动,这一篇我们来使用maven去搭建cucumber框架以及实现测试报告. 5.1 为什么要用m ...
- 行为驱动:Cucumber + Selenium + Java(四) - 实现测试用例的参数化
在上一篇中,我们介绍了Selenium + Cucumber + Java框架下的使用Tags对测试用例分组的实现方法,这一篇我们用数据表格来实现测试用例参数化. 4.1 什么是用例参数化 实际测试中 ...
- 行为驱动:Cucumber + Selenium + Java(三) - 使用标签实现测试分组
在上一篇中,我们写出了Selenium + Cucumber + Java环境下的第一个BDD自动化测试用例,这一篇我们说说怎么用标签对用例进行分组. 3.1 Cucumber标签 实际工作中,我们的 ...
- 行为驱动:Cucumber + Selenium + Java(二) - 第一个测试
在上一篇中,我们搭建好了Selenium + Cucumber + Java的自动化测试环境,这一篇我们就赶紧开始编写我们的第一个BDD测试用例. 2.1 创建features 我们在新建的java项 ...
- 行为驱动:Cucumber + Selenium + Java(一) - Cucumber简单操作实例
场景(Scenarios) 场景是Cucumber结构的核心之一.每个场景都以关键字“Scenario:”(或本地化一)开头,后面是可选的场景标题.每个Feature可以有一个或多个场景,每个场景由一 ...
- cucumber+selenium
工程结构 pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="ht ...
- 行为驱动:Cucumber + Selenium + Java(二) - extentreports 测试报告+jenkins持续集成
1.extentreports 测试报告 pom文件 <dependency> <groupId>com.vimalselvam</groupId> <art ...
随机推荐
- 大前端涉猎之前后端交互总结2:使用PHP进行表单数据上传与更新
1:使用PHP进行表单上传 1.1 form表单的数据收集 HTML页面: 代码解释:核心模块是form的属性: --提交方式 : method="post" --指定 name ...
- HDU 4714 Tree2cycle (树形DP)
题意:给定一棵树,断开一条边或者接上一条边都要花费 1,问你花费最少把这棵树就成一个环. 析:树形DP,想一想,要想把一棵树变成一个环,那么就要把一些枝枝叶叶都换掉,对于一个分叉是大于等于2的我们一定 ...
- 在iOS中使用百度地图
就如同在百度地图的文档中所说的一样,这么来.但是,有一个小疏忽. 到添加完所需要的framework之后,一定要记得把你的(Class-Prefix)AppDelegate的后缀改成mm. 估计百度的 ...
- Cocosd-x-2.2.2 & VS2012 & Eclipse 开发环境搭建
1.安装软件: 1.1 安装JDK(JDK1.7.0_51) JAVA_HOME C:\Program Files\Java\jdk1..0_51 CLASSPATH .;%JAVA_HOME%\li ...
- UT源码162
(3)设计佣金问题的程序 commission方法是用来计算销售佣金的需求,手机配件的销售商,手机配件有耳机(headphone).手机壳(Mobile phone shell).手机贴膜(Cellp ...
- unigui不是单个网页相应的链接,而是整体Web Application,如何把webApp的子功能映射到微信公众号菜单?
只需要用UniApplication.Parameters.Values[‘xxx’]读取url的参数然后调用就可以 例如:要打开公众号菜单的取样送检指南查询模块,在自定义菜单设定:http://ww ...
- HTML5+CSS3+jQuery Mobile轻松构造APP与移动网站 (陈婉凌) 中文pdf扫描版
<HTML5+CSS3+jQuery Mobile轻松构造APP与移动网站>以HTML与CSS为主,配合jQuery制作网页,并搭配jQueryMobile制作移动网页,通过具体的范例从基 ...
- uwsgi启动提示:probably another instance of uWSGI is running on the same address (:8002). bind(): Address already in use [core/socket.c line 769]
提示8002端口被占用,可以这样终止掉后再启动 sudo fuser -k 8002/tcp
- 【09】循序渐进学 docker:docker swarm
写在前面的话 至此,docker 的基础知识已经了解的差不多了,接下来就来谈谈对于 docker 容器,我们如何来管理它. docker swarm 在学习 docker swarm 之前,得先知道容 ...
- CookieJar和HTTPCookieProcessor
CookieJar和HTTPCookieProcessor 我们在使用爬虫的时候,经常会用到cookie进行模拟登陆和访问.在使用urllib库做爬虫,我们需要借助http.cookiejar库中的C ...