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项目,添加以下依赖

 1   <dependencies>
 2     <dependency>
 3         <groupId>org.testng</groupId>
 4         <artifactId>testng</artifactId>
 5         <version>6.8.7</version>
 6         <type>jar</type>
 7         <scope>test</scope>
 8     </dependency>
 9     <dependency>
10         <groupId>org.seleniumhq.selenium</groupId>
11         <artifactId>selenium-java</artifactId>
12         <version>2.32.0</version>
13         <type>jar</type>
14         <scope>compile</scope>
15     </dependency>
16     <dependency>
17         <groupId>com.google.guiceberry</groupId>
18         <artifactId>guiceberry</artifactId>
19         <version>3.3.1</version>
20         <type>jar</type>
21         <scope>compile</scope>
22     </dependency>
23     <dependency>
24         <groupId>com.google.inject</groupId>
25         <artifactId>guice</artifactId>
26         <version>3.0</version>
27         <type>jar</type>
28         <scope>compile</scope>
29     </dependency>
30   </dependencies>

Env class

 1 package foo;
 2 
 3 import com.google.common.testing.TearDownAccepter;
 4 import com.google.guiceberry.GuiceBerryModule;
 5 import com.google.guiceberry.TestScoped;
 6 import com.google.inject.AbstractModule;
 7 import com.google.inject.Provides;
 8 import org.openqa.selenium.WebDriver;
 9 import org.openqa.selenium.firefox.FirefoxDriver;
10 
11 public class WebDriverEnv extends AbstractModule {
12     @Override
13     protected void configure() {
14         install(new GuiceBerryModule());
15     }
16 
17     @Provides
18     @TestScoped
19     public WebDriver getWebDriver() {
20         final WebDriver driver = new FirefoxDriver();
21         return driver;
22     }
23 
24 }

Page class

 1 package foo;
 2 
 3 import static org.testng.Assert.assertTrue;
 4 
 5 import org.openqa.selenium.By;
 6 import org.openqa.selenium.WebDriver;
 7 import org.openqa.selenium.WebElement;
 8 import org.openqa.selenium.support.ui.LoadableComponent;
 9 import com.google.inject.Inject;
10 
11 
12 
13 public class GooglePage extends LoadableComponent<GooglePage> {
14     @Inject
15     private WebDriver webdriver;
16     
17     @Override
18     protected void load() {
19         webdriver.get("http://www.google.com/ncr");
20     }
21 
22     @Override
23     protected void isLoaded() throws Error {
24         assertTrue((webdriver.getTitle()).contains("Google"));
25     }
26 
27     public void search(String query) {
28         searchField = webdriver.findElement(By.id("gbqfq"));
29         searchField.clear();
30         searchField.sendKeys(query);
31         searchField.submit();
32     }
33     
34     public String getSearchResult() {
35         return webdriver.getPageSource();
36     }
37 
38 }

Test class

 1 package foo;
 2 
 3 import org.openqa.selenium.WebDriver;
 4 import com.google.inject.Inject;
 5 import org.testng.annotations.Test;
 6 import com.google.common.testing.TearDown; 
 7 import com.google.guiceberry.testng.TestNgGuiceBerry; 
 8 import org.testng.annotations.AfterMethod; 
 9 import org.testng.annotations.BeforeMethod;
10 import java.lang.reflect.Method;
11 
12 public class SearchGoogleTest {
13     private TearDown toTearDown; 
14     @BeforeMethod 
15     public void setUp(Method m) { 
16         toTearDown = TestNgGuiceBerry.setUp(this, m, 
17                 WebDriverEnv.class); 
18     } 
19 
20     @AfterMethod 
21     public void tearDown() throws Exception { 
22         toTearDown.tearDown(); 
23         webdriver.close();
24     } 
25     
26     @Inject
27     private WebDriver webdriver;
28 
29     @Inject
30     private GooglePage googlePage;
31     
32     @Test
33     public void testGoogleHomePageTitle() {
34         googlePage.load();
35         googlePage.isLoaded();
36     }
37 }

PS:github真是好东西,看到个更好的例子:https://github.com/abendt/uitest-webdriver-guiceberry

我们知道Webdriver可以支持IE,Firefox,Chrome等浏览器+各种操作系统组合,在Env class中专注构建各种环境,而Test class只需要指定我使用哪个浏览器就可以了,不用关心运行环境是如何配置的

1 Capabilities ie = DesiredCapabilities.internetExplorer();
2 Capabilities firefox = DesiredCapabilities.firefox();
3 Capabilities chrome = DesiredCapabilities.chrome();
4 
5 WebDriver driver = new WebDriverBuilder()
6     .of(ie, firefox, chrome)
7     .preferHeadless()
8     .build();

 

你可能感兴趣的:(webdriver)