工程结构

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId>
<artifactId>mycucumber</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<!-- Maven 自带的中央仓库使用的Id为central 如果其他的仓库声明也是用该Id
就会覆盖中央仓库的配置 -->
<id>mvnrepository</id>
<name>mvnrepository</name>
<url>http://www.mvnrepository.com/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository> </repositories> <dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-core</artifactId>
<version>4.2.3</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>4.2.3</version>
</dependency> <!-- 加入reportNG依赖,代替testNG测试报告 -->
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.4</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>io.cucmber</groupId>
<artifactId>cucmber-testng</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 加入selenium做webUI测试,选用selenium2 -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.1</version>
</dependency>
<!-- 依赖Guice -->
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies> <build>
<plugins>
<!-- 加入maven-surefire-plugin插件用来使用maven执行用例,
其中suiteXmlFile配置的就是testNG用例执行文件的地址 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/java/cucumber/testng/testng.xml</suiteXmlFile>
</suiteXmlFiles>
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
<property>
<name>listener</name>
<value>org.uncommons.reportng.HTMLReporter</value>
</property>
</properties>
<!-- 加入编码设置,否则生成的报告会中文乱码 -->
<argLine>-Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>
<!-- 添加插件,添加ReportNg的监听器,修改最后的TestNg的报告 -->
</plugins>
</build> </project>

testng

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Baidu Search Demo Suite" verbose="0" parallel="tests" thread-count="2">
<test name="Web Scenarios Test" enabled="true" thread-count="2">
<!--<groups>-->
<!--<run>-->
<!--<include name="@BaiduSearch"/>-->
<!--</run>-->
<!--</groups>-->
<classes>
<class name="common.core.Runner"/>
</classes>
</test>
</suite>

common.core

package common.core;

import common.myenum.DriverType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver; public class BrowserDriver { private WebDriver driver; public WebDriver getDriver(String browerName) {
switch (DriverType.getDriverType(browerName)) {
case chrome:
System.setProperty("webdriver.chrome.bin", "D:\\Program Files\\ChromeGAE\\Chrome\\chrome.exe");
System.setProperty("webdriver.chrome.driver", "D:\\Program Files\\ChromeGAE\\Chrome\\chromedriver.exe");
driver = new ChromeDriver();
break;
case firefox:
driver = new FirefoxDriver();
break;
case ie:
driver = new InternetExplorerDriver();
break;
}
return driver;
}
}
package common.core;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests; @CucumberOptions(
features = "src/test/resource/features/BaiduSearch.feature",
format = {
"pretty",
"html:target/site/cucumber-pretty",
"rerun:target/site/return.text",
"json:target/cucumberjson.json"
},
tags = {"@BaiduSearch"},
glue = {"cucumber.steps"}
)
public class Runner extends AbstractTestNGCucumberTests {
}

common.myenum

package common.myenum;

public enum DriverType {
chrome, ie, firefox; public static DriverType getDriverType(String driverType) {
return valueOf(driverType.toLowerCase());
}
}

cucumber.pages

package cucumber.pages.baidu;

import cucumber.pages.BasePage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy; public class BaiduSearchPage extends BasePage {
//private final String searchBtn = "xpath=//input[@id='su']";
//private final String searchText = "xpath=//input[@id='kw']"; @FindBy(xpath = "//*[@id=\"kw\"]")
private WebElement searchText; @FindBy(xpath = "//*[@id=\"su\"]")
private WebElement searchBtn; @FindBy(xpath = "//div[@id=\"1\"]/h3/a")
private WebElement firstLink; @FindBy(xpath = "//div[@id=\"2\"]/h3/a")
private WebElement secondLink; public BaiduSearchPage(WebDriver driver) {
super(driver);
} public void searchByText(String text) throws InterruptedException {
this.sendString(searchText, text);
this.click(searchBtn);
Thread.sleep(3000);
} public boolean verifyFirstLinkTitle(String title) throws InterruptedException {
System.out.println(this.driver.getWindowHandle());
return this.firstLink.getText().contains(title); } public boolean verifySecondLinkTitle(String title) {
return this.secondLink.getText().contains(title);
}
}
package cucumber.pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait; public class BasePage { protected WebDriver driver;
private final int timeOut = 10;//等待时间 public BasePage(WebDriver driver) {
this.driver = driver;
this.driver.manage().window().maximize();
} protected void sendString(WebElement element, String s) {
new WebDriverWait(driver, timeOut).until(ExpectedConditions.visibilityOf(element));// 加入显式等待
element.clear();// 清空输入框
element.sendKeys(s);// 输入数据
} protected void click(WebElement element) {
new WebDriverWait(driver, timeOut).until(ExpectedConditions.visibilityOf(element));
element.click();
}
}

cucumber.steps

package cucumber.steps;

import common.core.BrowserDriver;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import cucumber.pages.baidu.BaiduSearchPage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory; public class MySteps {
private WebDriver driver;
private BaiduSearchPage baiduSearchPage; public MySteps() { } @Given("^I start \"([^\"]*)\"$")
public void startBrowser(String browerName) throws Throwable {
this.driver = new BrowserDriver().getDriver(browerName);
this.baiduSearchPage = new BaiduSearchPage(this.driver);
} @When("^I search \"([^\"]*)\" at baidu$")
public void searchText(String text) throws Throwable {
baiduSearchPage = PageFactory.initElements(driver, BaiduSearchPage.class);
this.driver.get("http://www.baidu.com");
this.baiduSearchPage.searchByText(text);
} @Then("^The result should have \"([^\"]*)\" in the title of the \"([^\"]*)\" link$")
public void theResultShouldHaveInTheTitleOfTheLink(String text, String ordinal) throws Throwable {
if (ordinal.equalsIgnoreCase("first")) {
assert this.baiduSearchPage.verifyFirstLinkTitle(text);
} else if (ordinal.equalsIgnoreCase("second")) {
assert this.baiduSearchPage.verifySecondLinkTitle(text);
} else {
throw new Exception("Error:ordinal is not available.");
}
this.driver.close();
}
}

resource.features

Feature:  Baidu Search

  @BaiduSearch
Scenario Outline: Search text
Given I start "<browser>"
When I search "<text>" at baidu
Then The result should have "<title>" in the title of the "<ordinal>" link Examples:
| browser | text | title | ordinal |
| chrome | xxx1 | CSDN | first |
| chrome | xxx2 | CSDN | second |

cucumber+selenium的更多相关文章

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

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

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

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

  3. 行为驱动:Cucumber + Selenium + Java(四) - 实现测试用例的参数化

    在上一篇中,我们介绍了Selenium + Cucumber + Java框架下的使用Tags对测试用例分组的实现方法,这一篇我们用数据表格来实现测试用例参数化. 4.1 什么是用例参数化 实际测试中 ...

  4. 行为驱动:Cucumber + Selenium + Java(三) - 使用标签实现测试分组

    在上一篇中,我们写出了Selenium + Cucumber + Java环境下的第一个BDD自动化测试用例,这一篇我们说说怎么用标签对用例进行分组. 3.1 Cucumber标签 实际工作中,我们的 ...

  5. 行为驱动:Cucumber + Selenium + Java(二) - 第一个测试

    在上一篇中,我们搭建好了Selenium + Cucumber + Java的自动化测试环境,这一篇我们就赶紧开始编写我们的第一个BDD测试用例. 2.1 创建features 我们在新建的java项 ...

  6. 行为驱动:Cucumber + Selenium + Java(一) - Cucumber简单操作实例

    场景(Scenarios) 场景是Cucumber结构的核心之一.每个场景都以关键字“Scenario:”(或本地化一)开头,后面是可选的场景标题.每个Feature可以有一个或多个场景,每个场景由一 ...

  7. cucumber & selenium & bddtest

    目录 1.Cucumber介绍 refer link: 2.使用步骤 2.1 引入依赖 2.2新建test.future文件 2.3在resource目录下创建cucumber.bat,执行bat,c ...

  8. 使用cucumber & selenium实现一个简单的bddtest

    1.Cucumber介绍 + feature : read requirement +scenario : testing situation,including + Given/ + when/ + ...

  9. 行为驱动:Cucumber + Selenium + Java(二) - extentreports 测试报告+jenkins持续集成

    1.extentreports 测试报告 pom文件 <dependency> <groupId>com.vimalselvam</groupId> <art ...

随机推荐

  1. Java---- 静态内部类与非静态内部类的区别

    静态类(只有内部类才能被声明为静态类,即静态内部类)1.只能在内部类中定义静态类 2.静态内部类与外层类绑定,即使没有创建外层类的对象,它一样存在. 3.静态类的方法可以是静态的方法也可以是非静态的方 ...

  2. php函数之substr()

    问题: 希望从字符串的某个特定位置开始抽取这个字符串的一部分.例如,对于输入到一个表单的用户名,想要得到这个用户名的前8个字符. 解决: 使用substr()选择子串 $substring = sub ...

  3. c++ 用模板类实现顺序储存的线性表

    首先要注意的一点是模板类在VS中编译时如果将定义和声明分开会出现无法解析的问题,所以一般比较常见的解决的办法是将声明和定义放在同一个头文件中然后统一的调用,下面就是用模板类实现线性表的编写 #prag ...

  4. java8学习之收集器枚举特性深度解析与并行流原理

    首先先来找出上一次[http://www.cnblogs.com/webor2006/p/8353314.html]在最后举的那个并行流报错的问题,如下: 在来查找出上面异常的原因之前,当然得要一点点 ...

  5. hdu2159 二维02bag

    设f[i][j]为杀第j只怪时耐久度为i的最大经验值 完全背包类型:有N种物品和一个容量为V 的背包,每种物品都有无限件可用.放入第i种物品的耗费的空间是Ci,得到的价值是Wi. 求解:将哪些物品装入 ...

  6. Http请求Response Code含义

    http状态返回代码 1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码.100 (继续) 请求者应当继续提出请求. 服务器返回此代码表示已收到请求的第一部分,正在等待其余部分.101 ...

  7. 基于idea的maven(一)Maven的安装

    1.Maven前置依赖 检查电脑是是否安装java 2.下载maven 网址 www.apache.org 解压 maven 压缩包, 并创建相应的maven本地仓库的路径. 打开 conf文件夹中 ...

  8. java8 新用法

    /** * 得到优先级最高的集合 * @param list es查询结果 * @return */public List<Group> getMaxPriorityGroup(List& ...

  9. Java-Shiro(五):Shiro Realm讲解(二)IniRealm的用法、JdbcRelam的用法、自定义Realm

    引入 上一篇在讲解Realm简介时,介绍过Realm包含大概4类缺省的Realm,本章主要讲解: 1)IniRealm的用法: 2)JdbcRealm基于mysql   默认表及查询语句实现认证.授权 ...

  10. Centos 由字符界面 init 3 切换图形界面 init 5

    Centos6 和 Centos7 由字符界面切换成 图形界面方法不同,下面分别介绍. 一.Centos6 切换方法 yum -y install xorg* yum -y groupinstall ...