热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

cucumber+selenium

工程结构pomtestngcommon.corecommon.myenumcucumber.pagescucumber.stepsresource.f

工程结构

技术分享图片

pom


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">
4.0.0
com.test
mycucumber
1.0-SNAPSHOT

UTF-8




mvnrepository
mvnrepository
http://www.mvnrepository.com/
default

true


false





info.cukes
cucumber-java
1.2.4
test



info.cukes
cucumber-testng
1.2.4



org.uncommons
reportng
1.1.4
test


org.cucmber.testng
cucmber.testng





org.seleniumhq.selenium
selenium-java
2.53.1



com.google.inject
guice
3.0
test


org.projectlombok
lombok
true






org.apache.maven.plugins
maven-surefire-plugin
2.21.0


src/test/java/cucumber/testng/testng.xml



usedefaultlisteners
false


listener
org.uncommons.reportng.HTMLReporter



-Dfile.encoding=UTF-8






testng















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 ""
When I search "" at baidu
Then The result should have "" in the title of the "" link
Examples:
| browser | text | title | ordinal |
| chrome | xxx1 | CSDN | first |
| chrome | xxx2 | CSDN | second |


推荐阅读
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 本文讨论了在数据库打开和关闭状态下,重新命名或移动数据文件和日志文件的情况。针对性能和维护原因,需要将数据库文件移动到不同的磁盘上或重新分配到新的磁盘上的情况,以及在操作系统级别移动或重命名数据文件但未在数据库层进行重命名导致报错的情况。通过三个方面进行讨论。 ... [详细]
  • 本文介绍了指针的概念以及在函数调用时使用指针作为参数的情况。指针存放的是变量的地址,通过指针可以修改指针所指的变量的值。然而,如果想要修改指针的指向,就需要使用指针的引用。文章还通过一个简单的示例代码解释了指针的引用的使用方法,并思考了在修改指针的指向后,取指针的输出结果。 ... [详细]
  • 在project.properties添加#Projecttarget.targetandroid-19android.library.reference.1..Sliding ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • CentOS 7部署KVM虚拟化环境之一架构介绍
    本文介绍了CentOS 7部署KVM虚拟化环境的架构,详细解释了虚拟化技术的概念和原理,包括全虚拟化和半虚拟化。同时介绍了虚拟机的概念和虚拟化软件的作用。 ... [详细]
author-avatar
HenryJuliju
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有