jWebUnit
Top
- jWebUnit は、オープンソースのテストフレームワークです。
- jWebUnit を利用して、テストしたいメソッドを呼び出すテスト・クラスを作成し、テストします。
- jWebUnit では、jWebUnit の assert メソッド を使って、メソッドから返される値が期待される値と一致するかをテストします。
- Eclipse の jWebUnit プラグインを用いてテストを実行すると、JUnit パースペクティブが開かれ、テストの実行結果がすぐに得られます。
Top
Top
- つぎの URL から、ファイル(jwebunit-1.2.zip)をダウンロードします。
- つぎの手順にしたがってインストールします。
- ダウンロードしたファイル(jwebunit-1.2.zip)を解凍します。展開したディレクトリーは以下のとおりです(省略しているところもあります)。
├─docs
│ ├...
├─lib
├─licenses
├─sample
│ ├...
├─src
│ ├...
├─test
│ ├...
└─xdocs
├...
- Eclipse のプロジェクトの Java ビルドパスに、この中にある jar ファイルを外部 JAR として追加します。
- jwebunit-1.2.jar
- lib/xxx.jar (このディレクトリーにある jar ファイルすべて)
Top
- Web アプリケーションの場合、つぎのようなパターンでテストケースを作ります。
- 単純に Web ページを確認する
- リンクをクリックしてリンク先のページを確認する
- フォームにデータを設定してsubmitし、アクション後のページを確認する
Top
Top
beginAt("確認したいページのURL");
assertTextPresent("確認したい文字列");
- import net.sourceforge.jwebunit.TestContext;
- import net.sourceforge.jwebunit.WebTestCase;
-
- public class WhipShopTest extends WebTestCase {
-
- TestContext context = getTestContext();
-
- public void setUp() throws Exception {
- context.setBaseUrl("http://localhost:8080/osjava");
- }
-
- // login - 基本メニュー表示
- public void test01() {
- beginAt("/login.jsp");
- assertTextPresent("maruno");
- }
- }
Top
beginAt("確認したいページのURL");
clickLink("アンカータグのID属性の値");
assertTextPresent("確認したい文字列");
- import net.sourceforge.jwebunit.TestContext;
- import net.sourceforge.jwebunit.WebTestCase;
-
- public class WhipShopTest extends WebTestCase {
-
- TestContext context = getTestContext();
-
- public void setUp() throws Exception {
- context.setBaseUrl("http://localhost:8080/osjava");
- }
-
- // ユーザー登録
- public void test11() {
- beginAt("/login.jsp");
- clickLink("userregist"); // ID属性を参照
- assertTextPresent(context.toEncodedString("ユーザー情報登録"));
- }
- }
Top
beginAt("確認したいページのURL");
setFormElement("フォーム要素の名前", "設定する値");
submit();
assertTextPresent("確認したい文字列");
- import net.sourceforge.jwebunit.TestContext;
- import net.sourceforge.jwebunit.WebTestCase;
-
- public class WhipShopTest extends WebTestCase {
-
- TestContext context = getTestContext();
-
- public void setUp() throws Exception {
- context.setBaseUrl("http://localhost:8080/osjava");
- }
-
- // login - 基本メニュー表示
- public void test01() {
- beginAt("/login.jsp");
- assertFormElementPresent("name");
- assertFormElementPresent("password");
- setFormElement("name", "maruno"); // 1.4 では、setTextField() に置き換え
- setFormElement("password", "himitu"); // 1.4 では、setTextField() に置き換え
- submit(); // 基本メニュー画面に行く
- assertTextPresent("maruno");
- assertTextPresent(context.toEncodedString("こんにちは、maruno さん"));
- }
- }
Top
void assertButtonNotPresent(String buttonID)
void assertButtonPresent(String buttonID)
void assertCheckboxNotSelected(String checkBoxName)
void assertCheckboxSelected(String checkBoxName)
void assertCookiePresent(String cookieName)
void assertCookieValueEquals(String cookieName, String expectedValue)
void assertElementNotPresent(String anID)
void assertElementPresent(String anID)
void assertFormElementEmpty(String formElementName)
void assertFormElementEquals(String formElementName, String expectedValue)
void assertFormElementNotPresent(String formElementName)
void assertFormElementNotPresentWithLabel(String formElementLabel)
void assertFormElementPresent(String formElementName)
void assertFormElementPresentWithLabel(String formElementLabel)
void assertFormNotPresent()
void assertFormNotPresent(String formName)
void assertFormPresent()
void assertFormPresent(String formName)
void assertFramePresent(String frameName)
void assertKeyInTable(String tableSummaryOrId, String key)
void assertKeyNotInTable(String tableSummaryOrId, String key)
void assertKeyNotPresent(String key)
void assertKeyPresent(String key)
void assertKeysInTable(String tableSummaryOrId, String[] keys)
void assertLinkNotPresent(String linkId)
void assertLinkNotPresentWithImage(String imageFileName)
void assertLinkNotPresentWithText(String linkText)
void assertLinkNotPresentWithText(String linkText, int index)
void assertLinkPresent(String linkId)
void assertLinkPresentWithImage(String imageFileName)
void assertLinkPresentWithText(String linkText)
void assertLinkPresentWithText(String linkText, int index)
void assertOptionEquals(String selectName, String option)
void assertOptionsEqual(String selectName, String[] options)
void assertOptionsNotEqual(String selectName, String[] options)
void assertOptionValuesEqual(String selectName, String[] options)
void assertOptionValuesNotEqual(String selectName, String[] options)
void assertRadioOptionNotPresent(String radioGroup, String radioOption)
void assertRadioOptionNotSelected(String radioGroup, String radioOption)
void assertRadioOptionPresent(String radioGroup, String radioOption)
void assertRadioOptionSelected(String radioGroup, String radioOption)
void assertSubmitButtonNotPresent(String buttonName)
void assertSubmitButtonPresent(String buttonName)
void assertSubmitButtonValue(String buttonName, String expectedValue)
void assertTableEquals(String tableSummaryOrId, ExpectedTable expectedTable)
void assertTableEquals(String tableSummaryOrId, String[][] expectedCellValues)
void assertTableNotPresent(String tableSummaryOrId)
void assertTablePresent(String tableSummaryOrId)
void assertTableRowsEqual(String tableSummaryOrId, int startRow, ExpectedTable expectedTable)
void assertTableRowsEqual(String tableSummaryOrId, int startRow, String[][] expectedCellValues)
void assertTextInElement(String elID, String text)
void assertTextInTable(String tableSummaryOrId, String text)
void assertTextInTable(String tableSummaryOrId, String[] text)
void assertTextNotInElement(String elID, String text)
void assertTextNotInTable(String tableSummaryOrId, String text)
void assertTextNotInTable(String tableSummaryOrId, String[] text)
void assertTextNotPresent(String text)
void assertTextPresent(String text)
void assertTitleEquals(String title)
void assertTitleEqualsKey(String titleKey)
void assertWindowPresent(String windowName)
void beginAt(String relativeURL)
void checkCheckbox(String checkBoxName)
void checkCheckbox(String checkBoxName, String value)
void clickButton(String buttonId)
void clickLink(String linkId)
void clickLinkWithImage(String imageFileName)
void clickLinkWithText(String linkText)
void clickLinkWithText(String linkText, int index)
void clickLinkWithTextAfterText(String linkText, String labelText)
void dumpCookies()
void dumpCookies(PrintStream stream)
void dumpResponse(PrintStream stream)
void dumpTable(String tableNameOrId, PrintStream stream)
void dumpTable(String tableNameOrId, String[][] table)
HttpUnitDialog getDialog()
String getMessage(String key)
TestContext getTestContext()
WebTester getTester()
void gotoFrame(String frameName)
void gotoPage(String page)
void gotoRootWindow()
void gotoWindow(String windowName)
void reset()
void selectOption(String selectName, String option)
void setFormElement(String formElementName, String value)
(注: 1.4 では、このメソッドは、推奨されなくなり、setTextField() に置き換わりました)
void setFormElementWithLabel(String formElementLabel, String value)
void setWorkingForm(String nameOrId)
void submit()
void submit(String buttonName)
void uncheckCheckbox(String checkBoxName)
void uncheckCheckbox(String checkBoxName, String value)
Top
void addCookie(String name, String value)
String getBaseUrl()
List getCookies()
String getEncodingScheme()
java.util.Locale getLocale()
String getPassword()
String getProxyName()
int getProxyPort()
String getResourceBundleName()
String getUser()
String getUserAgent()
com.meterware.httpunit.WebClient getWebClient()
boolean hasAuthorization()
boolean hasCookies()
boolean hasProxy()
boolean hasUserAgent()
void setAuthorization(String user, String passwd)
void setBaseUrl(String url)
void setEncodingScheme(String encodingScheme)
void setLocale(java.util.Locale locale)
void setProxyName(String proxyName)
void setProxyPort(int proxyPort)
void setResourceBundleName(String name)
void setUserAgent(String userAgent)
void setWebClient(com.meterware.httpunit.WebClient client)
String toEncodedString(String text)
Top
- jWebUnit のテスト・ケース(クラス)は、"JUnit テスト・ケース" で作成します。
□ "WhipShopTest.java"
- import net.sourceforge.jwebunit.TestContext;
- import net.sourceforge.jwebunit.WebTestCase;
-
- public class WhipShopTest extends WebTestCase {
-
- TestContext context = getTestContext();
-
- public void setUp() throws Exception {
- context.setBaseUrl("http://localhost:8080/osjava");
- }
-
- // login - 基本メニュー表示
- public void test01() {
- beginAt("/login.jsp");
- assertFormElementPresent("name");
- assertFormElementPresent("password");
- setFormElement("name", "maruno");
- setFormElement("password", "himitu");
- submit(); // 基本メニュー画面に行く
- assertTextPresent("maruno");
- assertTextPresent(context.toEncodedString("こんにちは、maruno さん"));
- }
-
- // login - 商品リスト表示
- public void test02() {
- beginAt("/login.jsp");
- setFormElement("name", "maruno");
- setFormElement("password", "himitu");
- submit(); // 基本メニュー画面に行く
- submit(); // 商品リスト画面に行く
- assertTextPresent(context.toEncodedString("ふつうホイップ"));
- }
-
- // login - 買い物かご表示
- public void test03() {
- beginAt("/login.jsp");
- setFormElement("name", "maruno");
- setFormElement("password", "himitu");
- submit(); // 基本メニュー画面に行く
- submit(); // 商品リスト画面に行く
- submit("submit0"); // 買い物かご画面に行く
- // このため、productlist.jsp で、
- // <html:submit property="submit${i}" ...
- // とする。
- assertTextPresent(context.toEncodedString("ふつうホイップ"));
- assertFormElementPresent("qty(W01)");
- }
-
- // login - 個数を2個にする - 発注確認画面表示
- public void test04() {
- beginAt("/login.jsp");
- setFormElement("name", "maruno");
- setFormElement("password", "himitu");
- submit(); // 基本メニュー画面に行く
- submit(); // 商品リスト画面に行く
- submit("submit0"); // 買い物かご画面に行く
- assertFormElementPresent("qty(W01)");
- setFormElement("qty(W01)", "2");
- submit(); // 発注確認画面に行く
- assertTextPresent(context.toEncodedString("4,000"));
- }
-
- // login - 個数を2個にする - 発注 - 基本メニュー画面
- public void test05() {
- beginAt("/login.jsp");
- setFormElement("name", "maruno");
- setFormElement("password", "himitu");
- submit(); // 基本メニュー画面に行く
- submit(); // 商品リスト画面に行く
- submit("submit0"); // 買い物かご画面に行く
- submit(); // 発注確認画面に行く
- submit(); // 発注する
- assertTextPresent(context.toEncodedString("発注しました。"));
- }
-
- // ユーザー登録
- public void test11() {
- beginAt("/login.jsp");
- clickLink("userregist"); // ID属性を参照
- // このため、login.jsp で、
- // <html:link styleId="userregist" ...
- // とする。
- assertTextPresent(context.toEncodedString("ユーザー情報登録"));
- }
-
- // ユーザー登録 - 登録確認画面
- public void test12() {
- beginAt("/login.jsp");
- clickLink("userregist"); // ID属性を参照
-
- setFormElement("name", "maruno");
- setFormElement("password", "himitu");
- checkCheckbox("gender", "2");
- checkCheckbox("merried", "true");
- setFormElement("sendName", "丸野");
- setFormElement("postalCode", "123-4567");
- setFormElement("address[0]", "東京都");
- setFormElement("address[1]", "新宿区西新宿1-2-3");
- setFormElement("address[2]", "新宿ビル405");
- setFormElement("tel(home)", "03-9876-5432");
- setFormElement("tel(mobile)", "090-8765-4321");
- checkCheckbox("hobbies", "ikebana");
- checkCheckbox("hobbies", "sado");
- submit(); // 登録確認画面に行く
-
- dumpResponse(System.out);
- assertTextPresent("maruno");
- assertTextPresent("himitu");
- assertTextPresent(context.toEncodedString("女性"));
- assertTextPresent(context.toEncodedString("既婚"));
- assertTextPresent(context.toEncodedString("丸野"));
- assertTextPresent(context.toEncodedString("123-4567"));
- assertTextPresent(context.toEncodedString("東京都"));
- assertTextPresent(context.toEncodedString("新宿区西新宿1-2-3"));
- assertTextPresent(context.toEncodedString("新宿ビル405"));
- assertTextPresent(context.toEncodedString("03-9876-5432"));
- assertTextPresent(context.toEncodedString("090-8765-4321"));
- assertTextPresent(context.toEncodedString("生花, 茶道"));
- }
- }
Top