使用junit4+NekoHTML对页面进行单元测试
2016-06-22 10:41
495 查看
首先科普一下NekoHTML。
NekoHTML是一个简单地HTML扫描器和标签补偿器(tag balancer),使得程序能解析HTML文档并用标准的XML接口来访问其中的信息。这个解析器能投扫描HTML文件并“修正”许多作者(人或机器)在编写HTML文档过程中常犯的错误。NekoHTML能增补缺失的父元素、自动用结束标签关闭相应的元素,以及不匹配的内嵌元素标签。NekoHTML的开发使用了Xerces
Native Interface (XNI),后者是Xerces2的实现基础。
具体使用方法请参考http://rensanning.iteye.com/blog/1551831
依赖包:nekohtml-1.9.22.jar,xercesImpl-2.11.0.jar,xml-api-1.4.01.jar
下载地址:http://archive.apache.org/dist/xerces/j/
http://nekohtml.sourceforge.net/
xml-api-1.4.01.jar这个jar包主要是当NekoHTML解析完页面后,用于创建Document和element对象时所使用到的jar包。
AbstractTest.java(测试父类,主要用于加载配置文件)
NekoHTML是一个简单地HTML扫描器和标签补偿器(tag balancer),使得程序能解析HTML文档并用标准的XML接口来访问其中的信息。这个解析器能投扫描HTML文件并“修正”许多作者(人或机器)在编写HTML文档过程中常犯的错误。NekoHTML能增补缺失的父元素、自动用结束标签关闭相应的元素,以及不匹配的内嵌元素标签。NekoHTML的开发使用了Xerces
Native Interface (XNI),后者是Xerces2的实现基础。
具体使用方法请参考http://rensanning.iteye.com/blog/1551831
依赖包:nekohtml-1.9.22.jar,xercesImpl-2.11.0.jar,xml-api-1.4.01.jar
下载地址:http://archive.apache.org/dist/xerces/j/
http://nekohtml.sourceforge.net/
xml-api-1.4.01.jar这个jar包主要是当NekoHTML解析完页面后,用于创建Document和element对象时所使用到的jar包。
AbstractTest.java(测试父类,主要用于加载配置文件)
import static org.junit.Assert.fail; import java.util.Collection; import org.junit.Assert; import org.junit.runner.RunWith; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.transaction.annotation.Transactional; @ContextConfiguration({"classpath:applicationContext.xml"}) @Transactional("coreTransactionManager") @WebAppConfiguration @Rollback(true) @RunWith(SpringJUnit4ClassRunner.class) public abstract class AbstractTest extends AbstractTransactionalJUnit4SpringContextTests{ public AbstractTest() { super(); } public void success(){ Assert.assertTrue(true); } public void error(Throwable e){ fail(e.getMessage()); } public void assertNotEmpty(Collection list){ if(list==null){ Assert.fail("Collection is null"); } if(list.size()>0){ this.success(); }else{ Assert.fail(); } } }RequestHandlerTest.java(用于进行页面测试或进行Control层测试的父类)
import java.io.StringReader; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpSession; import org.cyberneko.html.parsers.DOMParser; import org.junit.Before; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.w3c.dom.Document; import org.xml.sax.InputSource; import com.cmcc.schema.ecity.notifymessage.v1_0.local.response.Response; import com.cmcc.wsdl.ecity.notifymessage.v1_0._interface.NotifyMessagePortType; import com.eshine.mss.soap.handler.RequestHandler; import com.eshine.mss.soap.session.SessionManager; import com.eshine.test.AbstractTest; import com.eshine.util.Json; /** * @author hhs * @date 2016年6月20日 */ public abstract class RequestHandlerTest extends AbstractTest { protected static ApplicationContext ctx=null; protected static NotifyMessagePortType webService; @Autowired private RequestHandler handler; @Before public void setup(){ // if(ctx==null){ // ctx = new ClassPathXmlApplicationContext("client-beans2.xml"); // webService = ctx.getBean("cmccWsClient", NotifyMessagePortType.class); // } } /** * 返回html文档 * @author hhs * @date 2016年6月22日 * @param functionId * @param action * @param params * @return * @throws Exception */ public Document getDocument(String functionId,String action,String params) throws Exception{ String html=action(functionId, action, params); System.out.println(html); //创建一个解析器 DOMParser parser = new DOMParser(); //解析HTML文件 parser.parse(new InputSource(new StringReader(html))); //获取解析后的DOM树 Document document = parser.getDocument(); return document; } /** * 返回JSON对象 * @author hhs * @date 2016年6月22日 * @param functionId * @param action * @param params * @param clazz * @return * @throws Exception */ public <T> T getJsonObject(String functionId,String action,String params,Class<T> clazz) throws Exception{ String json=action(functionId, action, params); return Json.json2Object(json, clazz); } public String action(String functionId,String action,String params) throws Exception{ HttpSession session=SessionManager.getNewSession(); Response res=new Response(); com.cmcc.schema.ecity.notifymessage.v1_0.local.response.MsgCvnt resCvnt=new com.cmcc.schema.ecity.notifymessage.v1_0.local.response.MsgCvnt(); res.setMsgcvnt(resCvnt); com.cmcc.schema.ecity.notifymessage.v1_0.local.response.MsgInfo resInfo=new com.cmcc.schema.ecity.notifymessage.v1_0.local.response.MsgInfo(); res.setMsginfo(resInfo); com.cmcc.schema.ecity.notifymessage.v1_0.local.response.MsgCvnt.Msgdata resData=new com.cmcc.schema.ecity.notifymessage.v1_0.local.response.MsgCvnt.Msgdata(); resCvnt.setBodytype(2); Map headerMap = new HashMap(); String result=handler.handle(functionId, action, params, res, headerMap, session); return result; } }IndexHandlerTest.java(测试首页)
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import org.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import com.eshine.mss.soap.RequestHandlerTest; /** * @author hhs * @date 2016年6月21日 */ public class IndexHandlerTest extends RequestHandlerTest { @Test public void testAppLogin() throws Exception{ //获取解析后的DOM树 Document document =this.getDocument("getmssindex", null, null); //通过getElementsByTagName获取Node NodeList nodeList = document.getElementsByTagName("form"); Element form=(Element) nodeList.item(0); assertNotNull(form); String formAction=form.getAttribute("action"); assertEquals("/appLogin",formAction); // assertTrue(fb.isError()); } }IndexHandlerTest这个类主要用于测试首页登录页面,需要先集成RequestHandlerTest类,加载页面所有的节点,取其中的某一个节点的属性与预期的节点属性进行比较,如果结果相同则页面的单元测试通过。
相关文章推荐
- Android之使用Http协议实现文件上传功能
- mysql集群之MMM简单搭建
- 通晓网络测试常用命令
- Nodejs学习笔记之测试驱动
- 可以测试javascript运行效果的代码
- 使用php测试硬盘写入速度示例
- python 测试实现方法
- 如何测试端口通不通(四种方法)
- 举例详解PHP脚本的测试方法
- 使用Jasmine和Karma对AngularJS页面程序进行测试
- Php-Redis安装测试笔记
- mysql压力测试脚本实例
- JavaScript 组件之旅(四):测试 JavaScript 组件
- js简单网速测试方法完整实例
- Android触屏测试实例代码
- 解析libcurl在android下的移植、编译与测试
- 几个比较实用的JavaScript 测试及效验工具
- Java中对AtomicInteger和int值在多线程下递增操作的测试
- JUnit中获取测试类及方法的名称实现方法
- Python实现测试磁盘性能的方法