java httpunit

1.import java.io.IOException;  
2.import java.net.MalformedURLException;
3.
4.import org.xml.sax.SAXException;
5.
6.import com.meterware.httpunit.GetMethodWebRequest;
7.import com.meterware.httpunit.PostMethodWebRequest;
8.import com.meterware.httpunit.WebConversation;
9.import com.meterware.httpunit.WebForm;
10.import com.meterware.httpunit.WebLink;
11.import com.meterware.httpunit.WebRequest;
12.import com.meterware.httpunit.WebResponse;
13.import com.meterware.httpunit.WebTable;
14.
15.
16.public class httpUnitTestSample {
17.
18. /**
19. * 页面内容测试
20. * @throws MalformedURLException
21. * @throws IOException
22. * @throws SAXException
23. */
24. public static void testGetHtmlContent() throws MalformedURLException,
25. IOException, SAXException {
26. System.out.println("直接获取网页内容:");
27. // 建立一个WebConversation实例
28. WebConversation wc = new WebConversation();
29. // 向指定的URL发出请求,获取响应
30. WebResponse wr = wc.getResponse("http://www.baidu.com");
31. // 用getText方法获取相应的全部内容
32. // 用System.out.println将获取的内容打印在控制台上
33. System.out.println(wr.getText());
34. }
35.
36. /**
37. * 用get方法获取页面内容
38. * @throws MalformedURLException
39. * @throws IOException
40. * @throws SAXException
41. */
42. public static void testGetMethod() throws MalformedURLException,
43. IOException, SAXException {
44. System.out.println("向服务器发送数据,然后获取网页内容:");
45. // 建立一个WebConversation实例
46. WebConversation wc = new WebConversation();
47. // 向指定的URL发出请求
48. WebRequest req = new GetMethodWebRequest(
49. "http://localhost:8080/test.html");
50. // 给请求加上参数
51. req.setParameter("query", "四氯化碳");
52. // 获取响应对象
53. WebResponse resp = wc.getResponse(req);
54.
55. // 用getText方法获取相应的全部内容
56. // 用System.out.println将获取的内容打印在控制台上
57. System.out.println(resp.getText());
58.
59. }
60.
61. /**
62. * 用post方法获取页面内容
63. * @throws MalformedURLException
64. * @throws IOException
65. * @throws SAXException
66. */
67. public static void testPostMethod() throws MalformedURLException,
68. IOException, SAXException {
69. System.out.println("使用Post方式向服务器发送数据,然后获取网页内容:");
70. // 建立一个WebConversation实例
71. WebConversation wc = new WebConversation();
72. // 向指定的URL发出请求
73. WebRequest req = new PostMethodWebRequest(
74. "http://localhost:8080/test.html");
75. // 给请求加上参数
76. req.setParameter("user_name", "test");
77. req.setParameter("password", "111111");
78. // 获取响应对象
79. WebResponse resp = wc.getResponse(req);
80.
81. // 用getText方法获取相应的全部内容
82. // 用System.out.println将获取的内容打印在控制台上
83. System.out.println(resp.getText());
84. }
85.
86.
87. /**
88. * 获取页面链接并模拟点击
89. * @throws MalformedURLException
90. * @throws IOException
91. * @throws SAXException
92. */
93. public static void testClickLink() throws MalformedURLException,
94. IOException, SAXException {
95. System.out.println("获取页面中链接指向页面的内容:");
96. // 建立一个WebConversation实例
97. WebConversation wc = new WebConversation();
98. // 获取响应对象
99. WebResponse resp = wc.getResponse("http://www.265.com/");
100. // 获得页面链接对象
101. WebLink link = resp.getLinkWith("百度");
102. // 模拟用户单击事件
103. link.click();
104. // 获得当前的响应对象
105. WebResponse nextLink = wc.getCurrentPage();
106.
107. // 用getText方法获取相应的全部内容
108. // 用System.out.println将获取的内容打印在控制台上
109. System.out.println(nextLink.getText());
110.
111. }
112.
113. /**
114. * 获取页面内容的table内容
115. * @throws MalformedURLException
116. * @throws IOException
117. * @throws SAXException
118. */
119. public static void testTableContent() throws MalformedURLException,
120. IOException, SAXException {
121. System.out.println("获取页面中表格的内容:");
122. // 建立一个WebConversation实例
123. WebConversation wc = new WebConversation();
124. // 获取响应对象
125. WebResponse resp = wc
126. .getResponse("http://www.w3school.com.cn/tiy/loadtext.asp?f=html_table_test");
127.
128. System.out.println(resp.getText());
129. // 获得对应的表格对象
130. WebTable webTable = resp.getTables()[0];
131. // 将表格对象的内容传递给字符串数组
132. String[][] datas = webTable.asText();
133. // 循环显示表格内容
134. int i = 0, j = 0;
135. int m = datas[0].length;
136. int n = datas.length;
137. while (i < n) {
138. j = 0;
139. while (j < m) {
140. System.out.println("表格中第" + (i + 1) + "行第" + (j + 1) + "列的内容是:"
141. + datas[i][j]);
142. ++j;
143. }
144. ++i;
145. }
146. }
147.
148. /**
149. * 获取页面的表单控件内容
150. * @throws MalformedURLException
151. * @throws IOException
152. * @throws SAXException
153. */
154. public static void testHtmlContentForm() throws MalformedURLException,
155. IOException, SAXException {
156. System.out.println("获取页面中表单的内容:");
157. // 建立一个WebConversation实例
158. WebConversation wc = new WebConversation();
159. // 获取响应对象
160. WebResponse resp = wc.getResponse("http://www.w3school.com.cn/tiy/t.asp?f=html_table_test");
161.
162. System.out.println(resp.getText());
163. // 获得对应的表单对象
164. WebForm webForm = resp.getForms()[0];
165. // 获得表单中所有控件的名字
166. String[] pNames = webForm.getParameterNames();
167. int i = 0;
168. int m = pNames.length;
169. // 循环显示表单中所有控件的内容
170. while (i < m) {
171. System.out.println("第" + (i + 1) + "个控件的名字是" + pNames[i] + ",里面的内容是"
172. + (webForm.getParameterValues(pNames[i])));
173. ++i;
174. }
175. }
176.
177. public static void main(String[] args) throws MalformedURLException,
178. IOException, SAXException {
179. // testGetHtmlContent();
180. // testGetMethod();
181. // testPostMethod();
182. // testClickLink();
183. // testTableContent();
184. testHtmlContentForm();
185. }
186.
187.}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值