WatijでJavaScriptアラートを何とかする。
ほんと僕って馬鹿ですね><
こうするとちゃんと動きました。
まず、こういう奴を作って・・・
public class JsButtonClicker implements Runnable { private Button button; public JsButtonClicker(Button button) { this.button = button; } @Override public void run() { try { this.button.click(); } catch (Exception e) { e.printStackTrace(); } } }
んで、テストコードはこのように。
@Test public void test() { try { IE ie = new IE(); ie.start("ごにょごにょごにょ\test1.html"); Thread.sleep(1000L); assertEquals("Test page 1", ie.title()); Thread.sleep(1000L); ie.button(0).click(); assertEquals("aaa", ie.textField(0).value()); ie.button(1).click(); ie.textField(0).set(""); Thread t = new Thread(new JsButtonClicker(ie.button(1))); t.start(); ie.alertDialog().ok(); ie.button(0).click(); t = new Thread(new JsButtonClicker(ie.button(1))); t.start(); try { if (ie.alertDialog().exists()) { ie.alertDialog().ok(); } } catch (Exception e) { if (e.getMessage().equals("Timeout exceeded. Reason= Could not find Dialog with title = Microsoft Internet Explorer")) { System.err.println("実はエラー出てます"); } else { e.printStackTrace(); } } ie.close(); } catch (Exception e) { e.printStackTrace(); fail(); }
前回失敗してたのは、IE ie = new IE();をしちゃっていたので、IEをJNI経由で呼び出すIEのコントローラ(だと思うんですが。。。)へのポインタに見当違いな事をしていたせい。(まるちすれっどとかよく分かってないですねorz)
なので、Runnableを実装したクラスを作ってあげれば良いのですが、これで万事解決という訳ではないです。
テストコードにもあるネストしたキモイtry-catchがそれなのですが、必ずAlertDialogが出るという前提であればこんなtry-catchは無くても大丈夫なんですが、時と場合によってはAlertDialogが出ないぞ、というケースも同じようなコードでテストしようとすると、AlertDialogが見つからず以下のような例外が返されます><
watij.time.TimeException: Timeout exceeded. Reason= Could not find Dialog with title = Microsoft Internet Explorer
なので、このメッセージをピンポイントで潰す為のネストしたtry-catchを置いたというワケです。(他にやりようはなかったんか、とも思ったのですがまぁそれは次のステップで。。。)でも、そうすると以下のようなログも出るようになっちゃったり。
Exception c0000005, at 000BE571
Access violation: attempting to read memory at address FFFFFFFF
Native function stack data: e3504,b30,20005,80198
ERROR 2008-09-25 01:12:22,884 [JNIWrapper.ShutdownHook]
com.jniwrapper.FunctionExecutionException: c0000005
at com.jniwrapper.Function.invokeVirtualFunc(Native Method)
at com.jniwrapper.FunctionCall.a(SourceFile:130)
at com.jniwrapper.FunctionCall.callVirtual(SourceFile:57)
at com.jniwrapper.win32.com.impl.b.a(SourceFile:411)
at com.jniwrapper.win32.com.impl.b.release(SourceFile:379)
at com.jniwrapper.T.a(SourceFile:242)
at com.jniwrapper.T.b(SourceFile:234)
at com.jniwrapper.NativeResourceCollector.d(SourceFile:194)
at com.jniwrapper.NativeResourceCollector.a(SourceFile:76)
at com.jniwrapper.NativeResourceCollector.c(SourceFile:21)
at com.jniwrapper.i.run(SourceFile:120)
これはJNIのJavaじゃない方を勉強しろ、って事なんでしょうか><