HttpUnitAdvertisementHttpUnitとは
HttpUnitはHttpによる通信を擬似的に行うためのフレームワークである。ProxyやJavaScriptなどに対応しているため、さまざまな状況の下で利用することができる。
HttpUnitとJUnitと組み合わせて使うことで、クライアントとWebアプリケーション間のテストを行うことができる。 入手〜設定基本(リクエストの生成〜レスポンスの取得)
基本的に、次のような実装を行う。
サンプルコード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());
}
}
結果
200http://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ブログ ・このカテゴリのトップページに戻る ・Incubator(Pukiwiki) ・634ラボ UIコレクションギャラリー ZO-3ジェネレーター サイト検索Y!ログールビリヤード |