- package com.amway.test;
- import java.io.IOException;
- import org.xml.sax.SAXException;
- import com.meterware.httpunit.GetMethodWebRequest;
- import com.meterware.httpunit.PostMethodWebRequest;
- import com.meterware.httpunit.TableCell;
- import com.meterware.httpunit.WebConversation;
- import com.meterware.httpunit.WebForm;
- import com.meterware.httpunit.WebLink;
- import com.meterware.httpunit.WebRequest;
- import com.meterware.httpunit.WebResponse;
- import com.meterware.httpunit.WebTable;
- import junit.framework.TestCase;
- public class WebExistTest extends TestCase{
- //测试页面是否存在1
- public void testWebExists(){
- //模拟浏览器对象,拥有一个浏览器
- WebConversation webConversation = new WebConversation();
- try {
- webConversation.getResponse(“http://localhost:8080/Httpunit/”);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- }
- }
- //测试页面是否存在2
- public void testWebExists2(){
- //模拟浏览器对象,拥有一个浏览器
- WebConversation webConversation = new WebConversation();
- //用get方法得到 一个请求对象
- WebRequest request = new GetMethodWebRequest(“http://localhost:8080/Httpunit/”);
- try {
- webConversation.getResponse(request);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- }
- }
- //测试获取页面的内容 (源代码)
- public void testWebContenct(){
- WebConversation webConversation = new WebConversation();
- try {
- WebResponse response = webConversation.getResponse(“http://localhost:8080/Httpunit/”);
- System.out.println(response.getText());
- } catch (IOException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- }
- }
- //测试获取页面内容,并传递参数(get方式)
- public void testWebContenctWithParameterGet(){
- WebConversation webConversation = new WebConversation();
- WebRequest request = new GetMethodWebRequest(“http://localhost:8080/Httpunit/”);
- request.setParameter(“username”, “xaiobai”);
- try {
- WebResponse response = webConversation.getResponse(request);
- System.out.println(response.getText());
- } catch (IOException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- }
- }
- //测试获取页面的内容,并传递参数(post方式 )
- public void testWebContenctWithParameterPost(){
- WebConversation webConversation = new WebConversation();
- WebRequest request = new PostMethodWebRequest(“http://localhost:8080/Httpunit/doForm.jsp”);
- request.setParameter(“username”, “xaiobai”);
- try {
- WebResponse response = webConversation.getResponse(request);
- System.out.println(response.getText());
- } catch (IOException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- }
- }
- //测试页面成功跳转
- public void testWebPageRedirectSuccess(){
- WebConversation webConversation = new WebConversation();
- WebRequest request = new PostMethodWebRequest(“http://localhost:8080/Httpunit/validate.jsp”);
- request.setParameter(“username”, “xiaobai”);
- request.setParameter(“password”, “123”);
- try {
- WebResponse response = webConversation.getResponse(request);
- String expectedUrl = “http://localhost:8080/Httpunit/success.jsp”;
- this.assertEquals(“验证失败”,expectedUrl ,response.getURL().toString());
- } catch (IOException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- }
- }
- //测试页面失败跳转
- public void testWebPageRedirectFailed(){
- WebConversation webConversation = new WebConversation();
- WebRequest request = new PostMethodWebRequest(“http://localhost:8080/Httpunit/validate.jsp”);
- request.setParameter(“username”, “xiaobai”);
- request.setParameter(“password”, “123111”);
- try {
- WebResponse response = webConversation.getResponse(request);
- String expectedUrl = “http://localhost:8080/Httpunit/failed.jsp”;
- this.assertEquals(“验证失败”,expectedUrl ,response.getURL().toString());
- } catch (IOException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- }
- }
- //测试网页表格
- public void testWebTable(){
- WebConversation webConversation = new WebConversation();
- WebRequest request = new GetMethodWebRequest(“http://localhost:8080/Httpunit/tableTest.jsp”);
- try{
- WebResponse response = webConversation.getResponse(request);
- //获取外层表格的第一个
- WebTable table = response.getTables()[0];
- this.assertEquals(2, table.getColumnCount());
- //行由0开始
- TableCell cell= table.getTableCell(2, 0);
- this.assertEquals(“版次”, cell.getText());
- //this.assertEquals(“”, table.g)
- }catch(Exception e) {
- e.printStackTrace();
- }
- }
- //测试网络中的连接
- public void testWebClickPage(){
- WebConversation webConversation = new WebConversation();
- WebRequest request = new GetMethodWebRequest(“http://localhost:8080/Httpunit/”);
- try {
- WebResponse response = webConversation.getResponse(request);
- System.out.println(response.getText());
- WebLink link = response.getLinkWith(“HttpUnit ABC”);
- request = link.getRequest();
- String expetedURL = request.getURL().toString();
- this.assertEquals(expetedURL, “http://localhost:8080/Httpunit/tableTest.jsp”);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- }
- }
- //测试网络中的form
- public void testWebForm(){
- WebConversation webConversation = new WebConversation();
- WebRequest request = new GetMethodWebRequest(“http://localhost:8080/Httpunit/formTest.jsp”);
- try {
- WebResponse response = webConversation.getResponse(request);
- System.out.println(response.getText());
- WebForm form = response.getForms()[0];
- String name = form.getParameterValue(“Name”);
- String food = form.getParameterValue(“Food”);
- String location = form.getParameterValue(“Location”);
- String CreditCard = form.getParameterValue(“CreditCard”);
- this.assertEquals(“La Cerentolia”, name);
- this.assertEquals(“Chinese”, food);
- this.assertEquals(“CenterCity”, location);
- this.assertEquals(“on”, CreditCard);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- }
- }
- //测试表单提交
- public void testFormSubmit(){
- WebConversation webConversation = new WebConversation();
- WebRequest request = new GetMethodWebRequest(“http://localhost:8080/Httpunit/formTest.jsp”);
- try {
- WebResponse response = webConversation.getResponse(request);
- System.out.println(response.getText());
- WebForm form = response.getForms()[0];
- form.setParameter(“Name”, “xiaobai”);
- form.setParameter(“Food”, “Italian”);
- form.setParameter(“Location”, “MiddleCity”);
- form.setParameter(“CreditCard”, “on”);
- //获取提交的url
- response = form.submit();
- String expetedURL = response.getURL().toString();
- this.assertEquals(“http://localhost:8080/Httpunit/doForm.jsp”, expetedURL);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- }
- }
- }
今天的文章HttpUnit入门的简单例子 .分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/31404.html