工程结构

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. SpringCloud 第一篇:服务的注册和发现(Eureka)

    一.spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运 ...

  2. nginx配置详解---学校资料

    #配置worker进程运行用户 nobody也是一个linux用户,一般用于启动程序,没有密码 user nobody; #配置工作进程数目,根据硬件调整,通常等于CPU数量或者2倍于CPU数量 wo ...

  3. Codeforces 837D - Round Subset DP

    先算出每个数的pop1(twonum),pop(fivenum)然后DP ans[i][j]表示选i个数有j个2时最多有多少个5 转移方程是 ;j--) { ;w++) { ans[j][w]=max ...

  4. VScode VUE+PYTHON习惯用的编辑器

    本人从事运维开发工作,经常要开发一些web系统工具,这就需要用到前后端开发.vscode是我用得最舒服的一种编辑器,前端后端都习惯在上面写,这里记录一些配置. 一,插件安装 目前我最主要用的是:    ...

  5. keystonejs实战之页头页脚

    前两篇介绍了入门相关知识及对keystonejs整体可用性评估,这篇介绍下开始实际运用中的页头页脚部分,因为马上项目忙了,这个先匆匆的作个收尾. 不管是用WordPress还是其他CMS系统,应用最宽 ...

  6. 前端接受base64的excel,点击立即下载

    由于使用框架的特殊性,后台返回的文件流进行了base64加密,所以返回的是base64格式,所以需要将其转变为blob,上代码

  7. [深度学习] pytorch利用Datasets和DataLoader读取数据

    本文简单描述如果自定义dataset,代码并未经过测试(只是说明思路),为半伪代码.所有逻辑需按自己需求另外实现: 一.分析DataLoader train_loader = DataLoader( ...

  8. “M_PI_2”: 重复定义的宏

    问题警告:“M_PI_2”: 重复定义的宏 分析:在Visual Studio上使用math.h库时与其他库数学库冲突,我们可以通过添加宏定义“_USE_MATH_DEFINES”来消除math.h定 ...

  9. head first 设计模式笔记6-命令模式

    命令模式:将“请求”封装成对象,以便使用不同的请求.队列或者日志来参数化其他对象.命令模式也支持可撤销的操作.这个模式允许我们将动作封装成命令对象,然后可以传递和调用. 1)命令模式将发出请求的对象和 ...

  10. re.compile 函数

    re.compile 函数 compile 函数用于编译正则表达式,生成一个正则表达式( Pattern )对象,供 match() 和 search() 这两个函数使用. 语法格式为: re.com ...