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

https://docs.google.com/presentation/d/122ckAvyqg5UQA7nUd21t8_T9606d_sL0DGWl3_acdtc/edit?pli=1#slide=id.i0

A walk-through video of this tutorial by Zorzella

Video:http://www.youtube.com/watch?v=yqre07YfPXQ

Slide:https://docs.google.com/presentation/d/1098aqrmz45rtbeA_X71rfbFa4qqjXKs8LtLVBjqP1kc/present#slide=id.i0

现在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();
上一篇:Python selenium chrome 环境配置


下一篇:javascript的this多种场景用法