`
jinnianshilongnian
  • 浏览: 21433491 次
  • 性别: Icon_minigender_1
博客专栏
5c8dac6a-21dc-3466-8abb-057664ab39c7
跟我学spring3
浏览量:2404875
D659df3e-4ad7-3b12-8b9a-1e94abd75ac3
Spring杂谈
浏览量:2997548
43989fe4-8b6b-3109-aaec-379d27dd4090
跟开涛学SpringMVC...
浏览量:5631359
1df97887-a9e1-3328-b6da-091f51f886a1
Servlet3.1规范翻...
浏览量:257535
4f347843-a078-36c1-977f-797c7fc123fc
springmvc杂谈
浏览量:1593116
22722232-95c1-34f2-b8e1-d059493d3d98
hibernate杂谈
浏览量:248952
45b32b6f-7468-3077-be40-00a5853c9a48
跟我学Shiro
浏览量:5847389
Group-logo
跟我学Nginx+Lua开...
浏览量:698077
5041f67a-12b2-30ba-814d-b55f466529d5
亿级流量网站架构核心技术
浏览量:780398
社区版块
存档分类
最新评论

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、定义控制器 

@Controller
public class TestController {

    @RequestMapping("/test1")
    public String test1(Model model) {
        return "test1";
    }

    @RequestMapping("/test2")
    public String test2(@RequestParam Long id, @RequestParam String name, Model model) {
        model.addAttribute("id", id);
        model.addAttribute("name", name);
        return "test2";
    }
}

2、页面test1.vm

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link href="/static/css/style.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="/static/js/jquery-1.11.1.min.js"></script>
</head>
<body>

<form id="form" action="/test2" method="post">
    <label for="id">id:</label>
    <input type="text" id="id" name="id"/><br/>

    <label for="name">name:</label>
    <input type="text" id="name" name="name"/><br/>

    <input type="submit" value="submit"/>
</form>

</body>
</html>

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

 

3、页面test2.vm 

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link href="/static/css/style.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="/static/js/jquery-1.11.1.min.js"></script>
</head>
<body>

<form id="form" method="post">
    <label for="id">id:</label>
    <input type="text" id="id" name="id" value="${id}"/><br/>

    <label for="name">name:</label>
    <input type="text" id="name" name="id" value="${name}"/><br/>

    <input id="submit-btn" type="submit" value="submit"/>

</form>

<script type="text/javascript">
    $("#submit-btn").click(function() {
        $(this).closest("form").attr("action", "/submit");
        $("#id").val("123");
        $("#name").val("zhangsan");
        return false;
    });
</script>

</body>
</html>

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

 

4、使用HtmlUnit测试 

4.1、初始化Web环境

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:spring-mvc.xml")
@WebAppConfiguration(value = "spring4.1-htmlunit/src/main/webapp")
public class MockMvcHtmlUnitHelloWorldTest {
    @Autowired
    private WebApplicationContext context;

    MockMvc mockMvc;
    WebClient webClient;

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

 

4.2、创建WebClient

    @Before
    public void setup() throws Exception {
        mockMvc = webAppContextSetup(context).build();

        String contextPath = "";
        webClient = new WebClient();
        webClient.setWebConnection(new MockMvcWebConnection(mockMvc, contextPath));
    }

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

 

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

        HtmlPage page1 = webClient.getPage("http://localhost/test1");
        HtmlForm form1 = page1.getHtmlElementById("form");
        assertEquals("/test2", form1.getAttribute("action"));

        page1.getElementById("id").setAttribute("value", "1");
        page1.getElementById("name").setAttribute("value", "lisi");

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

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

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

 

接着点击表单的submit按钮:

        HtmlForm form2 = page2.getHtmlElementById("form");
        form2.getElementsByAttribute("input", "type", "submit").get(0).click();

        assertEquals("123", page2.getElementById("id").getAttribute("value"));
        assertEquals("zhangsan", page2.getElementById("name").getAttribute("value"));

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

 

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

 

5、使用WebDriver进行测试

5.1、初始化Web环境

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:spring-mvc.xml")
@WebAppConfiguration(value = "spring4.1-htmlunit/src/main/webapp")
public class MockMvcWebDriverHelloWorldTest {
    @Autowired
    private WebApplicationContext context;

    MockMvc mockMvc;
    MockMvcHtmlUnitDriver webDriver;

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

 

5.2、创建MockMvcHtmlUnitDriver

    @Before
    public void setup() throws Exception {
        mockMvc = webAppContextSetup(context).build();
        boolean enableJavascript = true;
        String contextPath = "";
        webDriver = new MockMvcHtmlUnitDriver(mockMvc, enableJavascript);
        DirectFieldAccessor accessor = new DirectFieldAccessor(webDriver);
        BeanWrapper wrapper = new BeanWrapperImpl(accessor.getPropertyValue("webClient"));
        wrapper.setPropertyValue("webConnection", new MockMvcWebConnection(mockMvc, contextPath));
    }

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

 

最后测试完成后,关闭WebDriver

    @After
    public void tearDown() {
        webDriver.close();
    }

 

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

        webDriver.get("http://localhost/test1");
        WebElement form1 = webDriver.findElement(By.id("form"));
        webDriver.findElement(By.id("id")).sendKeys("1");
        webDriver.findElement(By.id("name")).sendKeys("lisi");
        form1.findElement(By.cssSelector("input[type=submit]")).click();

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

 

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

        assertEquals("http://localhost/test2", webDriver.getCurrentUrl());
        assertEquals("1", webDriver.findElementById("id").getAttribute("value"));
        assertEquals("lisi", webDriver.findElementById("name").getAttribute("value"));

 

接着点击test2页面的submit按钮

        webDriver.findElementByCssSelector("#form input[type=submit]").click();

        assertEquals("/submit", webDriver.findElementById("form").getAttribute("action"));
        assertEquals("123", webDriver.findElementById("id").getAttribute("value"));
        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

 

4
0
分享到:
评论
8 楼 h348592532 2015-01-17  
这个必须顶,还有开涛老师的分享精神
7 楼 jinnianshilongnian 2014-09-27  
zhch152 写道
import com.github.zhangkaitao.pb.UserProtos;

这个类也找不到。。。。从github上下载下来的代码

通过pb插件自动生成的
6 楼 zhch152 2014-09-27  
import com.github.zhangkaitao.pb.UserProtos;

这个类也找不到。。。。从github上下载下来的代码
5 楼 zhch152 2014-09-26  
spring-test-mvc-htmlunit

这些包都下载不下来?[img][/img]
4 楼 jinnianshilongnian 2014-08-25  
ping2010 写道
我们一般知道Controller这层。

恩 大多数是
3 楼 ping2010 2014-08-25  
我们一般知道Controller这层。
2 楼 jinnianshilongnian 2014-08-25  
tianice 写道
实际项目中有这么搞的吗?这工作量可不小

目前想在合适的场景试一把。
1 楼 tianice 2014-08-25  
实际项目中有这么搞的吗?这工作量可不小

相关推荐

Global site tag (gtag.js) - Google Analytics