`
zzc1684
  • 浏览: 1190388 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

Spring4.1新特性——页面自动化测试框架Spring MVC Test HtmlUnit简介

阅读更多

目录

Spring4.1新特性——综述

Spring4.1新特性——Spring核心部分及其他

Spring4.1新特性——Spring缓存框架增强

Spring4.1新特性——异步调用和事件机制的异常处理

Spring4.1新特性——数据库集成测试脚本初始化

Spring4.1新特性——Spring MVC增强

Spring4.1新特性——页面自动化测试框架Spring MVC Test HtmlUnit简介

Spring4.1新特性——静态资源处理增强

 

本文其实不应该算作Spring4.1新特性,该测试框架目前是独立于Spring Framework发展的。Spring MVC Test HtmlUnit提供了Spring MVC测试框架HtmlUnit、 WebDriverGeb的 集成测试,简化页面自动化测试,利用这些技术可以完成无需启动服务器即可进行页面测试、自动化页面/页面流程测试、Javascript测试、Mock Service提高集成测试速度。本文只会带你使用HtmlUnit和WebDriver进入基本的页面自动化测试一览,不会深入。

 

注:目前不支持JSP页面模板,因为其运行需要web容器支持,请选择如velocity、freemarker等模板引擎。

 

1、定义控制器 

Java代码  收藏代码
  1. @Controller  
  2. public class TestController {  
  3.   
  4.     @RequestMapping("/test1")  
  5.     public String test1(Model model) {  
  6.         return "test1";  
  7.     }  
  8.   
  9.     @RequestMapping("/test2")  
  10.     public String test2(@RequestParam Long id, @RequestParam String name, Model model) {  
  11.         model.addAttribute("id", id);  
  12.         model.addAttribute("name", name);  
  13.         return "test2";  
  14.     }  
  15. }  

2、页面test1.vm

Java代码  收藏代码
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title></title>  
  5.     <link href="/static/css/style.css" rel="stylesheet" type="text/css">  
  6.     <script type="text/javascript" src="/static/js/jquery-1.11.1.min.js"></script>  
  7. </head>  
  8. <body>  
  9.   
  10. <form id="form" action="/test2" method="post">  
  11.     <label for="id">id:</label>  
  12.     <input type="text" id="id" name="id"/><br/>  
  13.   
  14.     <label for="name">name:</label>  
  15.     <input type="text" id="name" name="name"/><br/>  
  16.   
  17.     <input type="submit" value="submit"/>  
  18. </form>  
  19.   
  20. </body>  
  21. </html>  

输入id和name会跳转到test2页面

 

3、页面test2.vm 

Java代码  收藏代码
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title></title>  
  5.     <link href="/static/css/style.css" rel="stylesheet" type="text/css">  
  6.     <script type="text/javascript" src="/static/js/jquery-1.11.1.min.js"></script>  
  7. </head>  
  8. <body>  
  9.   
  10. <form id="form" method="post">  
  11.     <label for="id">id:</label>  
  12.     <input type="text" id="id" name="id" value="${id}"/><br/>  
  13.   
  14.     <label for="name">name:</label>  
  15.     <input type="text" id="name" name="id" value="${name}"/><br/>  
  16.   
  17.     <input id="submit-btn" type="submit" value="submit"/>  
  18.   
  19. </form>  
  20.   
  21. <script type="text/javascript">  
  22.     $("#submit-btn").click(function() {  
  23.         $(this).closest("form").attr("action""/submit");  
  24.         $("#id").val("123");  
  25.         $("#name").val("zhangsan");  
  26.         return false;  
  27.     });  
  28. </script>  
  29.   
  30. </body>  
  31. </html>  

在该页面绑定id和name数据,然后点击submit按钮会重新设置id和name数据。

 

4、使用HtmlUnit测试 

4.1、初始化Web环境

Java代码  收藏代码
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(value = "classpath:spring-mvc.xml")  
  3. @WebAppConfiguration(value = "spring4.1-htmlunit/src/main/webapp")  
  4. public class MockMvcHtmlUnitHelloWorldTest {  
  5.     @Autowired  
  6.     private WebApplicationContext context;  
  7.   
  8.     MockMvc mockMvc;  
  9.     WebClient webClient;  

服务端端配置请参考《Spring MVC测试框架详解——服务端测试》。

 

4.2、创建WebClient

Java代码  收藏代码
  1. @Before  
  2. public void setup() throws Exception {  
  3.     mockMvc = webAppContextSetup(context).build();  
  4.   
  5.     String contextPath = "";  
  6.     webClient = new WebClient();  
  7.     webClient.setWebConnection(new MockMvcWebConnection(mockMvc, contextPath));  
  8. }  

此处需要指定contextPath,如果不指定会把uri路径中的第一个目录作为上下文,如http://localhost/ctx/path,即ctx是上下文,如果不想带上下文需要指定为“”。

 

获取页面1数据,然后设置form表单数据,其操作方式和Javascript DOM类似:

Java代码  收藏代码
  1. HtmlPage page1 = webClient.getPage("http://localhost/test1");  
  2. HtmlForm form1 = page1.getHtmlElementById("form");  
  3. assertEquals("/test2", form1.getAttribute("action"));  
  4.   
  5. page1.getElementById("id").setAttribute("value""1");  
  6. page1.getElementById("name").setAttribute("value""lisi");  

接着提交表单,当前页面会跳转到test2:

Java代码  收藏代码
  1. HtmlPage page2 = form1.getElementsByAttribute("input""type""submit").get(0).click();  
  2. assertEquals("http://localhost/test2", page2.getUrl().toString());  
  3. assertEquals("1", page2.getElementById("id").getAttribute("value"));  
  4. assertEquals("lisi", page2.getElementById("name").getAttribute("value"));  

然后断言该页面的数据是否是上个页面提交过来的。

 

接着点击表单的submit按钮:

Java代码  收藏代码
  1. HtmlForm form2 = page2.getHtmlElementById("form");  
  2. form2.getElementsByAttribute("input""type""submit").get(0).click();  
  3.   
  4. assertEquals("123", page2.getElementById("id").getAttribute("value"));  
  5. assertEquals("zhangsan", page2.getElementById("name").getAttribute("value"));  

点击该按钮后,会重新设置该页面的id和name输入框的数据。

 

整个测试过程还是比较简单的,当然实际页面要比这个复杂很多。

 

5、使用WebDriver进行测试

5.1、初始化Web环境

Java代码  收藏代码
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(value = "classpath:spring-mvc.xml")  
  3. @WebAppConfiguration(value = "spring4.1-htmlunit/src/main/webapp")  
  4. public class MockMvcWebDriverHelloWorldTest {  
  5.     @Autowired  
  6.     private WebApplicationContext context;  
  7.   
  8.     MockMvc mockMvc;  
  9.     MockMvcHtmlUnitDriver webDriver;  

和使用HtmlUnit类似,就不多介绍了。

 

5.2、创建MockMvcHtmlUnitDriver

Java代码  收藏代码
  1. @Before  
  2. public void setup() throws Exception {  
  3.     mockMvc = webAppContextSetup(context).build();  
  4.     boolean enableJavascript = true;  
  5.     String contextPath = "";  
  6.     webDriver = new MockMvcHtmlUnitDriver(mockMvc, enableJavascript);  
  7.     DirectFieldAccessor accessor = new DirectFieldAccessor(webDriver);  
  8.     BeanWrapper wrapper = new BeanWrapperImpl(accessor.getPropertyValue("webClient"));  
  9.     wrapper.setPropertyValue("webConnection"new MockMvcWebConnection(mockMvc, contextPath));  
  10. }  

此处需要使用反射把WebClient的上下文修改掉,否则必须带着上下文,这是目前它考虑不完善的地方。

 

最后测试完成后,关闭WebDriver

Java代码  收藏代码
  1. @After  
  2. public void tearDown() {  
  3.     webDriver.close();  
  4. }  

 

首先请求test1页面,然后查找相应的元素并输入数据

Java代码  收藏代码
  1. webDriver.get("http://localhost/test1");  
  2. WebElement form1 = webDriver.findElement(By.id("form"));  
  3. webDriver.findElement(By.id("id")).sendKeys("1");  
  4. webDriver.findElement(By.id("name")).sendKeys("lisi");  
  5. form1.findElement(By.cssSelector("input[type=submit]")).click();  

WebDriver支持CSS选择器,在实现负责逻辑时非常有用。

 

提交表单后,跳转到test2页面

Java代码  收藏代码
  1. assertEquals("http://localhost/test2", webDriver.getCurrentUrl());  
  2. assertEquals("1", webDriver.findElementById("id").getAttribute("value"));  
  3. assertEquals("lisi", webDriver.findElementById("name").getAttribute("value"));  

 

接着点击test2页面的submit按钮

Java代码  收藏代码
  1. webDriver.findElementByCssSelector("#form input[type=submit]").click();  
  2.   
  3. assertEquals("/submit", webDriver.findElementById("form").getAttribute("action"));  
  4. assertEquals("123", webDriver.findElementById("id").getAttribute("value"));  
  5. assertEquals("zhangsan", webDriver.findElementById("name").getAttribute("value"));  

   

整个测试过程和HtmlUnit类似,不过API更易用。

 

从目前来看,Spring MVC Test HtmlUnit框架本身只是起到了Spring MVC测试框架和HtmlUnit和WebDriver之间的粘合剂,把它们结合起来,如果没有Spring MVC测试框架的强大,这种融合还是比较麻烦的。

 

相关文章

http://htmlunit.sourceforge.net/

https://code.google.com/p/selenium/wiki/HtmlUnitDriver(需 翻 墙)

https://github.com/spring-projects/spring-test-htmlunit/blob/master/src/asciidoc/index.adoc

Spring MVC测试框架详解——服务端测试

Spring MVC测试框架详解——客户端测试

 

Spring4新特性

Spring4新特性——泛型限定式依赖注入

Spring4新特性——核心容器的其他改进

Spring4新特性——Web开发的增强

Spring4新特性——集成Bean Validation 1.1(JSR-349)到SpringMVC 

Spring4新特性——Groovy Bean定义DSL

Spring4新特性——更好的Java泛型操作API 

Spring4新特性——JSR310日期API的支持

Spring4新特性——注解、脚本、任务、MVC等其

 

源码下载

https://github.com/zhangkaitao/spring4-1-showcase/tree/master/spring4.1-htmlunit

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics