`

HTTPUNIT测试

 
阅读更多

 

测试1:测试网页是否存在:

要测试一個网页是否存在,只要简单的通过WebConversation的getResponse()方法即可,例如:

WebConversation webConversation = new WebConversation();

webConversation.getResponse("http://localhost:8080/httpUnit/");

如果找不到网页,則會引发HttpNotFoundException,由于不是断言错误,所以这会在JUnit中产生一個Error。

<!--EndFragment-->

测试2:Get、Post:

public class AuthActionTest extends BaseTest {

	@Autowired
	private UserSpaceRedisDAO userRedisDAO;
	
	@Test
	public void testLogin() throws IOException {
		String account="sgswebtest";
		String pwd="1111";
		WebConversation webConversation = new WebConversation();
		WebRequest request =new GetMethodWebRequest("http://localhost:8080/auth!doLogin.action");
		request.setParameter("loginAccount",account);
		request.setParameter("loginPwd",pwd);

		WebResponse response =webConversation.getResource(request);
		System.out.println("-----------------------"+response.getResponseMessage());
		Assert.notNull(response);
		Assert.isTrue(response.getResponseCode()==200);
		//登录成功返回userId
		String responseText=response.getText();
		Assert.notNull(responseText);
		Long userId=Long.valueOf(responseText.replaceAll("\"", ""));
		//根据账号查询userID,然后和返回的userId做比较
		super.jedis.select(0);
		Long newUserId=this.userRedisDAO.getUserIdByAccontName(account);
		Assert.notNull(newUserId);
		Assert.isTrue(newUserId.longValue()==userId.longValue(),"登录后返回的userId和账号ID不对应");
		
	}

}
<!--EndFragment-->

 测试3:取得表格信息:

您可以从WebResponse中取得相关的HTML信息,假设网页中有如下这样的一个表格:

书籍名称

设计模式(Design Pattern)

软件版本

书籍版次

第二版

修改时间

2004/12/26

下面的程序演示如何取得表格相关信息进行测试:

WebConversation webConversation = new WebConversation();

WebResponse response = webConversation.getResponse("http://localhost:8080/httpUnit/tableTest.jsp");

WebTable webTable = response.getTables()[0];

assertEquals(2, webTable.getColumnCount());

TableCell cell = webTable.getTableCell(2, 0);

assertEquals("书籍版次", cell.getText());

测试4:跟随超链接:

网页中有很多的超链接,我们可以使用HttpUnit来模拟超链接的点击,例如网页中如果有个超链接如下:

<a href="httpUnitABC.jsp">HttpUnit ABC</a>

则可以使用下面的程序来吵到链接,然后模拟一个click动作来跟随这个超链接:

WebConversation webConversation = new WebConversation();

WebResponse response = webConversation.getResponse("http://localhost:8080/httpUnit/");

WebLink link = response.getLinkWith("HttpUnit ABC");

WebRequest clickRequest = link.getRequest();

WebResponse linkPage =webConversation.getResponse(clickRequest);

测试5:测试Cookie:

如果被测试的网页需要Cookie信息,您可以使用WebConversation的addCookie()方法发送Cookie信息

给网页,例如:

WebConversation webConversation = new WebConversation();

webConversation.addCookie("user", "taobaoge");

WebResponse response = webConversation.getResponse("http://localhost:8080/httpUnit/");

如果网页中包含了Cookie,您可以使用getCookieValue()方法取得网页中包含的Cookie信息,若网页包括下面的Scriptlet:

<%

Cookie cookie = new Cookie("customerId", "12345");

response.addCookie(cookie);

%>

可使用下面的方式來测试传回的Cookie信息:

assertEquals("taobaoge",webConversation.getCookieValue("user"));

测试6:Authorization:

如果您的网页中有预设的HTTP基本验证,则可以使用WebConversation的setAuthorization ()方法來设定验证信息,例如:

webConversation.setAuthorization("justin", "123456");

测试7:设定代理:

有的時候,您测试的目的网页可能必须通过代理服务器才能连上,你可以通过设定系统属性来设定HttpUnit使用代理,例如:

System.getProperties().put("proxySet", "true");

System.getProperties().put("proxyHost", "proxy.ntu.edu.tw");

System.getProperties().put("proxyPort", "8080");

如此之來,HttpUnit就會通过指定的代理服务器來发送请求与接受相应。

<!--EndFragment-->

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics