HttpUnit

Advertisement

HttpUnitとは

HttpUnitはHttpによる通信を擬似的に行うためのフレームワークである。ProxyやJavaScriptなどに対応しているため、さまざまな状況の下で利用することができる。

HttpUnitとJUnitと組み合わせて使うことで、クライアントとWebアプリケーション間のテストを行うことができる。

入手〜設定

http://httpunit.sourceforge.net/からバイナリを入手する。

入手したzipファイルを解凍後、jarファイルにクラスパスを通す。

基本(リクエストの生成〜レスポンスの取得)

基本的に、次のような実装を行う。
  1. クライアントの動作を擬似的に実現するためのクラスである、WebConversationクラスのオブジェクトを生成する。
  2. リクエストを組み立てる。
  3. リクエストに対するレスポンスを取得する。

サンプルコード

Basic.java
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

public class Basic {
    public static void main(String[] args) throws Exception{
        String url = "http://www.google.co.jp/";

        WebConversation wc = new WebConversation();
        WebRequest request = new GetMethodWebRequest(url);
        WebResponse response = wc.getResponse(request);
        System.out.println(response.getResponseCode());
    }
}
結果
200
http://www.google.co.jp/にリクエストを送信し、ステータスコード200が戻ってきている。

Proxyの利用

WebConversationクラスのsetProxyServer();メソッドを利用して、Proxyサーバーの設定を行う。
WebConversation wc = new WebConversation();
wc.setProxyServer("proxy.hogehoge.com", 8888);

認証が必要な場合。
WebConversation wc = new WebConversation();
wc.setProxyServer("proxy.hogehoge.com", 8888, "user", "pass");

リンククリック

リンクはWebLinkクラスで表現される。WebLinkクラスは参照専用クラスなので、明示的に値を設定することができない。オブジェクトの組み立てには、レスポンスからオブジェクトを受け取るなどの方法を利用する。

サンプルコード

Link.java
動作:http://google.co.jp/→「ニュース」リンクをクリック
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

public class Basic {
    public static void main(String[] args) throws Exception{
        String url = "http://www.google.co.jp/";

        WebConversation wc = new WebConversation();
        WebRequest request = new GetMethodWebRequest(url);
        WebResponse response = wc.getResponse(request);
        System.out.println(response.getURL().toString());

        WebLink link = response.getLinkWith("ニュース");
        System.out.println(link.getText());
        System.out.println(link.getTitle());
        System.out.println(link.getTarget());
        System.out.println(link.getName());
        System.out.println(link.getURLString());

        response = link.click();
        System.out.println(response.getURL().toString());
    }
}

結果
http://www.google.co.jp/
ニュース

_top

/nwshp?hl=ja&tab=wn&ie=UTF-8
http://news.google.co.jp/nwshp?hl=ja&gl=jp

Form送信

フォームの操作には、WebFormクラスを利用する。
ButtonClick.java
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.SubmitButton;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

public class ButtonClick {
    public static void main(String[] args) throws Exception{
        String url = "http://www.google.co.jp/";

        WebConversation wc = new WebConversation();
        WebRequest request = new GetMethodWebRequest(url);
        WebResponse response = wc.getResponse(request);
        System.out.println(response.getURL().toString());

        WebForm form = response.getFormWithName("f");
        form.setParameter("q", "yahoo");
        form.setParameter("lr", "lang_ja");
        SubmitButton button = form.getSubmitButton("btnG");
        response = form.submit(button);
        System.out.println(response.getURL().toString());
    }
}
結果
http://www.google.co.jp/
http://www.google.co.jp/search?hl=ja&ie=Shift_JIS&q=yahoo&btnG=Google+%8C%9F%8D%F5&lr=lang_ja

各部品の数を取得

リンクや画像は配列で取得することができる。
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

public class ObjectNumberOfItems {

    public static void main(String[] args) throws Exception{
        String url = "http://www.google.co.jp/";

        WebConversation wc = new WebConversation();
        WebRequest request = new GetMethodWebRequest(url);
        WebResponse response = wc.getResponse(request);

        System.out.println("forms"+response.getForms().length);
        System.out.println("links"+response.getLinks().length);
        System.out.println("frames"+response.getFrameNames().length);
        System.out.println("images"+response.getImages().length);
        System.out.println("tables"+response.getTables().length);
        System.out.println("textblocks"+response.getTextBlocks().length);
    }
}
結果
forms	1
links	12
frames	0
images	1
tables	2
textblocks	1

Advertisement

ショートカット

634トップページ
このカテゴリのトップページに戻る
634ラボ

サイト検索

Google

Web サイト内

Y!ログール