使用junit4+NekoHTML对页面进行单元测试

首先科普一下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(测试父类,主要用于加载配置文件)

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类,加载页面所有的节点,取其中的某一个节点的属性与预期的节点属性进行比较,如果结果相同则页面的单元测试通过。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值