Guiceberry+Webdriver+TestNG
1. Guiceberry
Leverage Guice to achieve Dependency Injection on your JUnit tests
https://code.google.com/p/guiceberry/
GuiceBerry brings the joys of dependency injection to your test cases and test infrastructure. It leverages Guice to accomplish this. It allows you to use a composition model for the services your test needs, rather than the traditional extends MyTestCase approach.
GuiceBerry does not supplant your JUnit testing framework -- it builds on top of it (and works around it, when necessary), so you can run your tests normally, from your favorite command line or IDE environment.
简单来说,通过注入和绑定,可以利用Guiceberry实现依赖边缘化,Guiceberry是基于Google的guice框架实现的。
所以在UI的自动化中,使用Guiceberry来管理例如现在流行的webdriver以及浏览器运行环境,测试人员可以将更多的精力投入到具体的项目功能或业务的自动化测试中。
2. Guiceberry相关文档视频
Guiceberry 2.0 Slide
A walk-through video of this tutorial by Zorzella
Video:http://www.youtube.com/watch?v=yqre07YfPXQ
现在Guiceberry发布了3.3.1版本,下载地址见这里https://code.google.com/p/guiceberry/downloads/list
3. Google guice,Google用于Java开发的开放源码依赖项注入框架。它不需要您自己编写工厂,从而提供更好的测试性和模块性。
https://code.google.com/p/google-guice/
http://tech.it168.com/zt/guice/
4. Demo
Eclipse上创建一个Maven项目,添加以下依赖
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.7</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.32.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.guiceberry</groupId>
<artifactId>guiceberry</artifactId>
<version>3.3.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
Env class
package foo; import com.google.common.testing.TearDownAccepter;
import com.google.guiceberry.GuiceBerryModule;
import com.google.guiceberry.TestScoped;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver; public class WebDriverEnv extends AbstractModule {
@Override
protected void configure() {
install(new GuiceBerryModule());
} @Provides
@TestScoped
public WebDriver getWebDriver() {
final WebDriver driver = new FirefoxDriver();
return driver;
} }
Page class
package foo; import static org.testng.Assert.assertTrue; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.LoadableComponent;
import com.google.inject.Inject; public class GooglePage extends LoadableComponent<GooglePage> {
@Inject
private WebDriver webdriver; @Override
protected void load() {
webdriver.get("http://www.google.com/ncr");
} @Override
protected void isLoaded() throws Error {
assertTrue((webdriver.getTitle()).contains("Google"));
} public void search(String query) {
searchField = webdriver.findElement(By.id("gbqfq"));
searchField.clear();
searchField.sendKeys(query);
searchField.submit();
} public String getSearchResult() {
return webdriver.getPageSource();
} }
Test class
package foo; import org.openqa.selenium.WebDriver;
import com.google.inject.Inject;
import org.testng.annotations.Test;
import com.google.common.testing.TearDown;
import com.google.guiceberry.testng.TestNgGuiceBerry;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import java.lang.reflect.Method; public class SearchGoogleTest {
private TearDown toTearDown;
@BeforeMethod
public void setUp(Method m) {
toTearDown = TestNgGuiceBerry.setUp(this, m,
WebDriverEnv.class);
} @AfterMethod
public void tearDown() throws Exception {
toTearDown.tearDown();
webdriver.close();
} @Inject
private WebDriver webdriver; @Inject
private GooglePage googlePage; @Test
public void testGoogleHomePageTitle() {
googlePage.load();
googlePage.isLoaded();
}
}
PS:github真是好东西,看到个更好的例子:https://github.com/abendt/uitest-webdriver-guiceberry
我们知道Webdriver可以支持IE,Firefox,Chrome等浏览器+各种操作系统组合,在Env class中专注构建各种环境,而Test class只需要指定我使用哪个浏览器就可以了,不用关心运行环境是如何配置的
Capabilities ie = DesiredCapabilities.internetExplorer();
Capabilities firefox = DesiredCapabilities.firefox();
Capabilities chrome = DesiredCapabilities.chrome(); WebDriver driver = new WebDriverBuilder()
.of(ie, firefox, chrome)
.preferHeadless()
.build();
Guiceberry+Webdriver+TestNG的更多相关文章
- linux搭建phantomjs+webdriver+testng+ant自动化工程
因为项目的原因,需要将脚本在linux环境无浏览器化去跑,那么原有的在windows系统下有浏览器化的自动化脚本场景就不适用了,这里给出linux系统下搭建phantomjs+webdriver+te ...
- selenium webdriver testng自动化测试数据驱动
selenium webdriver testng自动化测试数据驱动 selenium webdriver testng自动化测试数据驱动 一.数据驱动测试概念 数据驱动测试是相同的测试脚本使用不同的 ...
- 基于WebDriver&TestNG 实现自己的Annotation @TakeScreenshotOnFailure
相信用过Selenium WebDriver 的朋友都应该知道如何使用WebDriver API实现Take Screenshot的功能. 在这篇文章里,我主要来介绍对failed tests实现 t ...
- Selenium WebDriver TestNg Maven Eclipse java 简单实例
环境准备 前提条件Eclipse 已经安装过 TestNg ,Maven 插件 新建一个普通的java项目 点击右键 configure->convert to Maven Project 之后 ...
- Webdriver+testNG+ReportNG+Maven+SVN+Jenkins自动化测试框架的pom.xml配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- Webdriver+Testng实现测试用例失败自动截图功能
testng执行测试用例的时候,如果用例执行失败会自动截图,方便后续排查问题 1.首先定义一个截图类: package com.rrx.utils; import java.io.File;impor ...
- [Training Video - 1] [Selenium Basics] [What is Selenium IDE,RC,Webdriver, TestNG, Junit And Ant]
Selenium IDE (Only support in Firefox): - Record and Run - UI interface - User extensions - Conversi ...
- WebDriver+TestNG的一个典型例子
想让测试更加灵活,1. 可以配置使用任意支持的浏览器进行测试:2. 配置所有Google的URL:3. 配置搜索的关键字.修改后的代码: public class GoogleTest { WebDr ...
- UI自动化测试篇 :Selenium2(Webdriver)&TestNG自动化测试环境搭建
最开始学习UI自动化,用的工具是QTP10,用起来确实比较容易上手,自学了没多久,大家都说QTP过时了.这么好用的的工具怎么一下子就过时了呢?因为它的“笨重”,因为它作为商业软件带来的巨大使用成本,还 ...
随机推荐
- 微信导出群记录V3.0
一.序 导出东北师范大学2017级软件工程微信群的聊天记录,形式不限,但需要包含文字.图片和链接,不允许截图. 聊天记录的时间段为2017年11月3日12:00起至2018年1月3日12:00. 二. ...
- nginx防盗链、nginx访问控制、nginx解析php相关配制、nginx代理
1.nginx防盗链编辑:vim /usr/local/nginx/conf/vhost/test.com.conf写入: location ~* ^.+\.(gif|jpg|png|swf|flv| ...
- linux下使用小票打印
linux下使用小票打印 打印机: Xprinter XP-58IIH指令支持: ESC/POS接口: USB, 蓝牙 Linux系统: Centos7 蓝牙配对很快, 配对好后就是连接状态. 但很快 ...
- maven settings.xml配置优化
<?xml version="1.0" encoding="UTF-8"?> <settings> <localRepositor ...
- 【BZOJ3238】【AHOI2013】差异
sam好,好写好调好ac! 原题: 图片题面好评 2<=N<=500000 在syq大神的指点下终于理解一道后缀自动姬了quq (其实是因为这道题的dp主要是在后缀树(就是拓扑序)上搞树形 ...
- 【java编程】ServiceLoader使用看这一篇就够了
转载:https://www.jianshu.com/p/7601ba434ff4 想必大家多多少少听过spi,具体的解释我就不多说了.但是它具体是怎么实现的呢?它的原理是什么呢?下面我就围绕这两个问 ...
- 【mysql】索引原理-MySQL索引原理以及查询优化
转载:https://www.cnblogs.com/bypp/p/7755307.html 一.介绍 1.什么是索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性 ...
- hibernate中Restrictions的用法
方法 说明 Restrictions.eq = Restrictions.allEq 利用Map来进行多个等于的限制 Restrictions.gt > Restrictions.ge > ...
- tile38 roaming-geofences 试用
tile38 支持动态实时的移动对象的数据监控 环境准备 docker-compose 文件 version: "3" services: app: image: tile ...
- nginx ssi + ngx_pagespeed 实现micro frontends 开发
nginx 的ssi 功能让我们可以将不同的拼接起来,ngx_pagespeed 是pagespeed 的nginx 模块,可以帮助 我们解决前端的一些性能优化的问题,通过简单的配置就可以搞定 一张参 ...