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 ...
随机推荐
- mybatis-generator命令行生成代码
目录文件如下: generator.xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <!DOC ...
- up7.1-asp.net-本地测试教程
1.1. ASP.NET 框架:.NET Framework 4.5 依赖库:csredis,Newtonsoft.Json 安装redis 下载 redis-x64:http://pan.bai ...
- HALCON机器视觉软件
HALCON是德国MVtec公司开发的一套完善的标准的机器视觉算法包,拥有应用广泛的机器视觉集成开发环境.它节约了产品成本,缩短了软件开发周期——HALCON灵活的架构便于机器视觉,医学图像和图像分析 ...
- GC分析中提到的根对象是什么
一些文章在分析GC时,不可逾越的说到要先从根对象扫描出不可达对象,然后标记那些不可达对象为垃圾.那么源头根对象是什么玩意呢? 几分钟后google到比较可信源是http://stackoverflow ...
- GitHub操作总结
GitHub操作总结 : 总结看不明白就看下面的详细讲解. . 作者:万境绝尘 转载请注明出处:http://blog.csdn.net/shulianghan/article/details/188 ...
- Mac安装Tomcat
1. 到Tomcat官网下载,如下找tar格式文件: http://ftp.twaren.net/Unix/Web/apache/tomcat/tomcat-8/v8.0.41/bin/apache- ...
- luogu AC自动机(模板)
完全忘了AC自动机怎么写了qwq,更别说AC自动机上DP了. 今天就好好地学习字符串好了qwq 提一下AC自动机的时间复杂度--设n是模式串的个数,m是文本串的长度,l是模式串的平均长度,那么它的时间 ...
- OCP 12c最新考试原题及答案(071-6)
6.(4-21) choose the best answer: View the Exhibit and examine the structure of the CUSTOMERS table. ...
- java的排序算法
分享网页:https://yq.aliyun.com/articles/136085?utm_content=m_26483
- php面向对象编程_2
1, 抽象类 ,用abstract关键字来修饰一个类,这个类就是抽象类:如果用abstract关键字来修饰一个方法,这个方法就是抽象方法,如果是抽象方法就不能实现(即抽象方法只能声明,不能定义). 抽 ...