工程结构

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. 启动web项目报错:The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone.

    解决: 在application.properties配置文件中的添加标红部分 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/miaosha?se ...

  2. VMware安装CentOS7_1511 mini版本

    这次安装使用的是 CentOS7_1511_mini 如果没有点击下载 点击下载

  3. IT经理工作职责

    IT经理工作职责:     1. 管理公司信息技术以及计算机系统.   2. 确保公司信息技术是可访问的并且配备了现有的可用的硬件和软件.   3. 监控并且维护公司信息技术并确保能够得到最大化的使用 ...

  4. 【异常】org.apache.hadoop.hbase.client.RetriesExhaustedException: Failed after attempts=36, exceptions:

    1 Phoenix远程无法连接但是本地可以连接,详细异常 SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found bindin ...

  5. mysql设置自增id清零 auto_increment

    清空表数据之后,如何让自增id清零,即从0开始计数呢 ; 想让id从1开始,就让 AUTO_INCREMENT = 1 就行了.

  6. Gym - 101915D Largest Group 最大团

    给你一个二分图 问你最大团为多大 解一:状压DP 解二:二分图最大匹配 二分图的最大团=补图的最大独立集 二分图最大独立集=二分图定点个数-最大匹配 //Hungary #include<bit ...

  7. redis缓存穿透-解决方案

    上面的解决方案个人觉得时有误的,因为就算缓存了value的null值,后面的接口请求还是会判断走数据库,所以看解决方案二 解决方案二: https://blog.csdn.net/muyi_amen/ ...

  8. 提高docker加载速度

    由于国情,我们需要对配置一下docker的下载镜像,提高一下后续的加载速度. 使用vim编辑 /etc/docker/daemon.json, 增加如下内容. { "registry-mir ...

  9. javaweb 一个坑—— writer必须放在response相关设置后面

    https://blog.csdn.net/weixin_33962621/article/details/86454414

  10. 使用 mybatis-Generator 自动生成DAO、Model、Mapping相关文件

    1.Maven项目 2.配置generatorConfig.xml文件 3.在pom.xml中配置插件 2.generatorConfig.xml文件信息 <?xml version=" ...