工程结构

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. css3实现div自动左右动

    <!DOCTYPE html> <meta charset="UTF-8"/> <html> <head> <style> ...

  2. oracle重置dba用户密码

    1.进入sqlplus里面: [oracle@master ~]$ sqlplus / as sysdba SQL*Plus: Release 12.1.0.2.0 Production on Tue ...

  3. nodejs在Mac下的卸载

    卸载: 在 node 官网上下载的安装包,用安装包安装的node.应该可以用一下命令行卸载: 在终端输入以下命令: sudo rm -rf /usr/local/{bin/{node,npm},lib ...

  4. dedecms 列表标签 去斜杠 去两边空格

    首先:将 include/arc.listview.class.php 文件的第53行: $this->Fields['title'] = ereg_replace("[<> ...

  5. fiddler抓包小技巧之自动保存抓包数据(可根据需求过滤)

    首先 选择 如果有别的提示的话,你就按着提示走,就会安装一个编辑器,叫Fiddler Script. 正常情况下,你按着上面操作之后就会出现如下界面: 完事了之后,按下Ctrl+F来查找OnBefor ...

  6. opengl学习-利用模板测试勾画物体轮廓中出现的一个问题

    我在学习OpenGL模板测试勾画物体轮廓的时候,出现了这个问题: 这个出现的原因就是,改变摄像机的时候,每次绘制,上次绘制中模板缓冲区的数据没有清除的原因.也就是在while循环开始的时候,glCle ...

  7. springboot中使用servlet时返回结果乱码问题

    在总的配置文件:application.properties中做一个配置,把我的问题解决了. #编码格式 spring.http.encoding.force=true spring.http.enc ...

  8. 标准C语言(13)

    函数指针可以作为形式参数使用,会作为实际参数使用的函数叫回调函数 /* * 回调函数演示 * */ #include <stdio.h> void print_cb(int *p_num) ...

  9. oracle order by 自定义

    我们通常需要根据客户需求对于查询出来的结果给客户提供自定义的排序方式,那么我们通常sql需要实现方式都有哪些,参考更多资料总结如下(不完善的和错误望大家指出): 一.如果我们只是对于在某个程序中的应用 ...

  10. [uboot] (番外篇)uboot之fdt介绍 (转)

    以下例子都以project X项目tiny210(s5pv210平台,armv7架构)为例 [uboot] uboot流程系列:[project X] tiny210(s5pv210)上电启动流程(B ...