watijのステキコード

cfneoでも言っているように、「ブラウザを直接叩くテストってぶっちゃけどーなの?」と思えてならない僕ですが、このwatijはそういった残念な部分を良い感じに何とかしてくれそうなライブラリです。

コードを見て頂ければ分かるかと。

@Test
public void testWatij() {
	try {
		IE ie = new IE();
		ie.start("http://www.google.co.jp/");
		assertEquals("Google", ie.title());
		ie.windowCapture("src/test/resources/watijtest1.png");		// 画像1
		ie.textField(0).set("ヨシオリ ネタ");
		Thread.sleep(1000L);
		ie.windowCapture("src/test/resources/watijtest2.png");		// 画像2
		ie.button(0).click();
		assertEquals("ヨシオリ ネタ - Google 検索", ie.title());
		Thread.sleep(1000L);
		ie.windowCapture("src/test/resources/watijtest3.png");		// 画像3
		ie.link(FinderFactory.href("http://d.hatena.ne.jp/Yoshiori/20080630/1214814814")).click();
		assertEquals("存在がネタなだけに - YoshioriのBlog", ie.title());
		Thread.sleep(1000L);
		ie.windowCapture("src/test/resources/watijtest4.png");		// 画像4
	} catch (Exception e) {
		e.printStackTrace();
		fail();
	}
}


感激したのが、windowCaptureのメソッドで作られるスクリーンショットですね。

上記コードにコメントで書いたような画像はこちら。

画像1

		ie.start("http://www.google.co.jp/");
		assertEquals("Google", ie.title());
		ie.windowCapture("src/test/resources/watijtest1.png");		// 画像1


画像2

		ie.textField(0).set("ヨシオリ ネタ");
		Thread.sleep(1000L);
		ie.windowCapture("src/test/resources/watijtest2.png");		// 画像2


画像3

		ie.button(0).click();
		assertEquals("ヨシオリ ネタ - Google 検索", ie.title());
		Thread.sleep(1000L);
		ie.windowCapture("src/test/resources/watijtest3.png");		// 画像3


画像4

		ie.link(FinderFactory.href("http://d.hatena.ne.jp/Yoshiori/20080630/1214814814")).click();
		assertEquals("存在がネタなだけに - YoshioriのBlog", ie.title());
		Thread.sleep(1000L);
		ie.windowCapture("src/test/resources/watijtest4.png");		// 画像4

すばらしいですね!


ライセンスとかまだ詳しく調べていませんが、とりあえずExcelCSVSelenium風のテストケースを作ってそれを使ってIEを動かしていくツールを作ってます。(目先の仕事で多分使うのでとりあえずあまり凝らない方向で・・・)